-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
268 lines (239 loc) · 6.63 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/*
MIT License
Copyright (c) 2024 Jake Lilly
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
package main
import (
"crypto/md5"
"errors"
"flag"
"fmt"
"io/fs"
"os"
"path/filepath"
)
const (
DEFAULT_SOURCE_DIRECTORY = "dotfiles"
EXIT_ERROR = 1
ABSENT = "ABSENT"
PRESENT = "PRESENT"
MISMATCH = "MISMATCH"
)
func main() {
opts := &Options{
Source: DEFAULT_SOURCE_DIRECTORY,
Target: getHomeDirectory(),
}
if err := opts.Parse(os.Args[1], os.Args[2:]); err != nil {
fmt.Fprintf(os.Stderr, "Error parsing options: %s\n", err)
os.Exit(EXIT_ERROR)
}
var commands = map[string]func([]Package, *Options)error{
"conjure": Conjure,
"expel": Expel,
"peer": Peer,
}
cmd := os.Args[1]
files, err := os.ReadDir(opts.Source)
if err != nil {
fmt.Fprintf(os.Stderr, "Error reading directory %s: %s", opts.Source, err)
os.Exit(EXIT_ERROR)
}
packages := getPackages(files)
for idx, pkg := range packages {
foundPaths := getFiles(fmt.Sprintf("%s/%s", opts.Source, pkg.name))
for _, path := range foundPaths {
pkgFile := PackageFile{
path: path,
}
relativePath := fmt.Sprintf("%s/%s/%s", opts.Source, pkg.name, path)
content, err := os.ReadFile(relativePath)
if err != nil {
fmt.Fprintf(os.Stderr, "Error reading %s: %s\n", relativePath, err)
os.Exit(EXIT_ERROR)
}
pkgFile.md5 = getHash(content)
state, err := determineState(pkgFile.path, pkgFile.md5, opts.Target)
if err != nil {
fmt.Fprintf(os.Stderr, "Error determining current state of file %s in package \"%s\": %s", pkgFile.path, pkg.name, err)
os.Exit(EXIT_ERROR)
}
pkgFile.state = state
packages[idx].addFile(pkgFile)
}
}
if err := runCommand(cmd, commands, packages, opts); err != nil {
fmt.Fprintf(os.Stderr, "%s", err)
os.Exit(EXIT_ERROR)
}
}
// Options struct
type Options struct {
Source string
Target string
}
func (opts *Options) Parse(cmd string, args []string) error {
cli := flag.NewFlagSet(cmd, flag.ExitOnError)
cli.StringVar(&opts.Target, "target", opts.Target, "Target destination for packages")
return cli.Parse(args)
}
// Package struct
type Package struct {
name string
files []PackageFile
state string
}
func (p *Package) addFile(pf PackageFile) []PackageFile {
p.files = append(p.files, pf)
return p.files
}
type PackageFile struct {
path string
md5 string
state string
}
func getHomeDirectory() string {
dir, err := os.UserHomeDir()
if err != nil {
fmt.Fprintf(os.Stderr, "Error determing home directory: %s", err)
os.Exit(EXIT_ERROR)
}
return dir
}
func getPackages(entries []fs.DirEntry) []Package {
pkgs := make([]Package, 0)
for _, entry := range entries {
if !entry.IsDir() {
continue
}
pkg := Package{
name: entry.Name(),
}
pkgs = append(pkgs, pkg)
}
return pkgs
}
func getFiles(root string) []string {
files := make([]string, 0)
filepath.WalkDir(root, func(p string, file fs.DirEntry, err error) error {
if err != nil {
return err
}
if !file.IsDir() {
relativePath, err := filepath.Rel(root, p)
if err != nil {
return err
}
files = append(files, relativePath)
}
return nil
})
return files
}
func getHash(content []byte) string {
hash := md5.New()
hash.Write(content)
return fmt.Sprintf("%x", hash.Sum(nil))
}
func determineState(file string, md5 string, targetPath string) (string, error) {
targetFile := fmt.Sprintf("%s/%s", targetPath, file)
if !checkFileExists(targetFile) {
return ABSENT, nil
}
content, err := os.ReadFile(targetFile)
if err != nil {
return "", err
}
if !(md5 == getHash(content)) {
return MISMATCH, nil
}
return PRESENT, nil
}
func checkFileExists(path string) bool {
_, err := os.Stat(path)
return !errors.Is(err, os.ErrNotExist)
}
func copyFile(pkgFile PackageFile, pkgName string, sourcePath string, targetPath string) error {
fullSourceFilePath := fmt.Sprintf("%s/%s/%s", sourcePath, pkgName, pkgFile.path)
srcContent, err := os.ReadFile(fullSourceFilePath)
if err != nil {
return err
}
fullTargetfilePath := fmt.Sprintf("%s/%s", targetPath, pkgFile.path)
fullTargetFileParents := filepath.Dir(fullTargetfilePath)
if err := os.MkdirAll(fullTargetFileParents, 0777); err != nil {
return err
}
if err := os.WriteFile(fullTargetfilePath, srcContent, 0666); err != nil {
return err
}
return nil
}
func rmFile(pkgFile PackageFile, targetPath string) error {
fullTargetfilePath := fmt.Sprintf("%s/%s", targetPath, pkgFile.path)
if err := os.Remove(fullTargetfilePath); err != nil {
return err
}
return nil
}
// Command runner
func runCommand(command string, commands map[string]func([]Package, *Options)error, pkgs []Package, opts *Options) error {
if fn, exists := commands[command]; exists {
return fn(pkgs, opts)
}
return fmt.Errorf("Unknown command: %s", command)
}
// Conjure subcommand
func Conjure(pkgs []Package, opts *Options) error {
for _, pkg := range pkgs {
fmt.Fprintf(os.Stdout, ".:. Conjuring %s\n", pkg.name)
for _, fn := range pkg.files {
if fn.state == ABSENT || fn.state == MISMATCH {
if err := copyFile(fn, pkg.name, opts.Source, opts.Target); err != nil {
return err
}
}
}
}
return nil
}
// Expel subcommand
func Expel(pkgs []Package, opts *Options) error {
for _, pkg := range pkgs {
fmt.Fprintf(os.Stdout, ".:. Expelling %s\n", pkg.name)
for _, fn := range pkg.files {
if fn.state == PRESENT {
if err := rmFile(fn, opts.Target); err != nil {
return err
}
}
}
}
return nil
}
// Peer subcommand
func Peer(pkgs []Package, opts *Options) error {
fmt.Fprintf(os.Stdout, ".:. Peering Packages\n")
for _, pkg := range pkgs {
fmt.Fprintf(os.Stdout, " %s\n", pkg.name)
for _, fn := range pkg.files {
fmt.Fprintf(os.Stdout, " (%s) %s\n", fn.state, fn.path)
}
}
return nil
}