diff options
-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) |