diff options
author | Ben Burwell <ben@benburwell.com> | 2019-08-13 13:03:56 -0400 |
---|---|---|
committer | Ben Burwell <ben@benburwell.com> | 2019-08-13 13:03:56 -0400 |
commit | b8dd7dae42e783f2236f889d276e66bedb182837 (patch) | |
tree | 7736579f0868bba69452fc8863ea56bf495a7fc6 | |
parent | 8df0e5aa7d4dc43b1384d91a820c9a748ccbea6b (diff) |
implement adopt
-rw-r--r-- | adopt.go | 42 | ||||
-rw-r--r-- | main.go | 7 |
2 files changed, 48 insertions, 1 deletions
diff --git a/adopt.go b/adopt.go new file mode 100644 index 0000000..9be0a77 --- /dev/null +++ b/adopt.go @@ -0,0 +1,42 @@ +package main + +import ( + "errors" + "io" + "os" + "path/filepath" +) + +func adopt(args []string) error { + if len(args) < 1 { + return errors.New("must specify at least one file to adopt") + } + for _, name := range args { + if err := adoptFile(name); err != nil { + return err + } + } + return nil +} + +func adoptFile(name string) error { + fromPath := filepath.Join(dest, name) + toPath := filepath.Join(base, "templates", name) + if err := os.MkdirAll(filepath.Dir(toPath), 0700); err != nil { + return err + } + from, err := os.Open(fromPath) + if err != nil { + return err + } + defer from.Close() + to, err := os.OpenFile(toPath, os.O_CREATE|os.O_WRONLY, 0600) + if err != nil { + return err + } + defer to.Close() + if _, err = io.Copy(to, from); err != nil { + return err + } + return nil +} @@ -12,7 +12,7 @@ const ( func main() { if len(os.Args) < 2 { - fmt.Printf("usage: conf apply [files...]\n") + fmt.Printf("usage: conf <adopt|apply> [files...]\n") os.Exit(1) } switch os.Args[1] { @@ -21,6 +21,11 @@ func main() { fmt.Printf("%v\n", err) os.Exit(1) } + case "adopt": + if err := adopt(os.Args[2:]); err != nil { + fmt.Printf("%v\n", err) + os.Exit(1) + } default: fmt.Printf("unrecognized command: %s\n", os.Args[1]) os.Exit(1) |