diff options
author | Ben Burwell <ben@benburwell.com> | 2019-08-13 15:49:47 -0400 |
---|---|---|
committer | Ben Burwell <ben@benburwell.com> | 2019-08-13 15:49:47 -0400 |
commit | f66905f36427aa7b066186d722a8af8d3e2c0275 (patch) | |
tree | 40cab23108da4a98291aa0d768fb729561ef15d8 | |
parent | e4839bdff00f09ccb4e0a8b12d634d26d172dfc9 (diff) |
remove hardcoded paths
-rw-r--r-- | adopt.go | 10 | ||||
-rw-r--r-- | apply.go | 32 | ||||
-rw-r--r-- | main.go | 35 | ||||
-rw-r--r-- | template.go | 8 |
4 files changed, 54 insertions, 31 deletions
@@ -7,21 +7,21 @@ import ( "path/filepath" ) -func adopt(args []string) error { +func adopt(conf *Conf, 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 { + if err := adoptFile(conf, name); err != nil { return err } } return nil } -func adoptFile(name string) error { - fromPath := filepath.Join(dest, name) - toPath := filepath.Join(base, "templates", name) +func adoptFile(conf *Conf, name string) error { + fromPath := filepath.Join(conf.Dest, name) + toPath := filepath.Join(conf.Source, "templates", name) if err := os.MkdirAll(filepath.Dir(toPath), 0700); err != nil { return err } @@ -7,46 +7,46 @@ import ( "text/template" ) -func apply(args []string) error { +func apply(conf *Conf, args []string) error { if len(args) != 0 { - return applyFiles(args) + return applyFiles(conf, args) } - names, err := getFileNames() + names, err := getFileNames(conf) if err != nil { return err } - return applyFiles(names) + return applyFiles(conf, names) } -func applyFiles(names []string) error { +func applyFiles(conf *Conf, names []string) error { for _, name := range names { fmt.Printf("applying %s\n", name) - if err := applyFile(name); err != nil { + if err := applyFile(conf, name); err != nil { return fmt.Errorf("could not apply %s: %v", name, err) } } return nil } -func applyFile(name string) error { - src := filepath.Join(base, "templates", name) +func applyFile(conf *Conf, name string) error { + src := filepath.Join(conf.Source, "templates", name) t, err := template.New(filepath.Base(name)).Funcs(buildFuncMap()).ParseFiles(src) if err != nil { return fmt.Errorf("could not build template: %v", err) } - data, err := getTemplateData() + data, err := getTemplateData(conf) if err != nil { return err } - d := filepath.Join(dest, name) - if err = os.MkdirAll(filepath.Dir(d), 0700); err != nil { + dest := filepath.Join(conf.Dest, name) + if err = os.MkdirAll(filepath.Dir(dest), 0700); err != nil { return err } srcInfo, err := os.Stat(src) if err != nil { return err } - out, err := os.OpenFile(d, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, srcInfo.Mode()) + out, err := os.OpenFile(dest, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, srcInfo.Mode()) if err != nil { return err } @@ -57,17 +57,17 @@ func applyFile(name string) error { return nil } -func getFileNames() ([]string, error) { +func getFileNames(c *Conf) ([]string, error) { var names []string - d := filepath.Join(base, "templates") - err := filepath.Walk(d, func(path string, info os.FileInfo, err error) error { + dir := filepath.Join(c.Source, "templates") + err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error { if err != nil { return err } if info.IsDir() { return nil } - name, err := filepath.Rel(d, path) + name, err := filepath.Rel(dir, path) if err != nil { return err } @@ -5,24 +5,36 @@ import ( "os" ) -const ( - base = "/Users/ben/dotfiles" - dest = "/Users/ben" -) +type Conf struct { + Source string + Dest string +} func main() { + conf := &Conf{ + Source: os.Getenv("CONF_SOURCE"), + Dest: os.Getenv("CONF_DEST"), + } + if err := checkdir(conf.Source); err != nil { + fmt.Printf("could not find $CONF_SOURCE: %v\n", err) + os.Exit(1) + } + if err := checkdir(conf.Dest); err != nil { + fmt.Printf("could not find $CONF_DEST: %v\n", err) + os.Exit(1) + } if len(os.Args) < 2 { fmt.Printf("usage: conf <adopt|apply> [files...]\n") os.Exit(1) } switch os.Args[1] { case "apply": - if err := apply(os.Args[2:]); err != nil { + if err := apply(conf, os.Args[2:]); err != nil { fmt.Printf("%v\n", err) os.Exit(1) } case "adopt": - if err := adopt(os.Args[2:]); err != nil { + if err := adopt(conf, os.Args[2:]); err != nil { fmt.Printf("%v\n", err) os.Exit(1) } @@ -31,3 +43,14 @@ func main() { os.Exit(1) } } + +func checkdir(path string) error { + info, err := os.Stat(path) + if err != nil { + return err + } + if !info.IsDir() { + return fmt.Errorf("%s is not a directory", path) + } + return nil +} diff --git a/template.go b/template.go index 7e933bb..a98abb1 100644 --- a/template.go +++ b/template.go @@ -40,8 +40,8 @@ func lookupPassword(name string) (string, error) { return strings.TrimRight(string(line), "\n"), nil } -func getTemplateData() (*TemplateData, error) { - vars, err := readVars() +func getTemplateData(conf *Conf) (*TemplateData, error) { + vars, err := readVars(conf) if err != nil { return nil, err } @@ -63,9 +63,9 @@ func getTemplateData() (*TemplateData, error) { return data, nil } -func readVars() (map[string]interface{}, error) { +func readVars(conf *Conf) (map[string]interface{}, error) { var vars map[string]interface{} - if _, err := toml.DecodeFile(filepath.Join(base, "vars.toml"), &vars); err != nil { + if _, err := toml.DecodeFile(filepath.Join(conf.Source, "vars.toml"), &vars); err != nil { return nil, err } return vars, nil |