From da8aa52b5ce6d948a346d7dbf1254ca3faaeac80 Mon Sep 17 00:00:00 2001 From: Ben Burwell Date: Sat, 6 Jun 2020 17:02:38 -0400 Subject: Add status subcommand --- apply.go | 25 +------------------------ conf.go | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ main.go | 29 ++++++----------------------- status.go | 30 ++++++++++++++++++++++++++++++ 4 files changed, 88 insertions(+), 47 deletions(-) create mode 100644 conf.go create mode 100644 status.go diff --git a/apply.go b/apply.go index ddf12a5..904ffef 100644 --- a/apply.go +++ b/apply.go @@ -11,7 +11,7 @@ func apply(conf *Conf, args []string) error { if len(args) != 0 { return applyFiles(conf, args) } - names, err := getFileNames(conf) + names, err := conf.FileNames() if err != nil { return err } @@ -56,26 +56,3 @@ func applyFile(conf *Conf, name string) error { } return nil } - -func getFileNames(c *Conf) ([]string, error) { - var names []string - 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(dir, path) - if err != nil { - return err - } - names = append(names, name) - return nil - }) - if err != nil { - return nil, err - } - return names, nil -} diff --git a/conf.go b/conf.go new file mode 100644 index 0000000..71b2919 --- /dev/null +++ b/conf.go @@ -0,0 +1,51 @@ +package main + +import ( + "errors" + "os" + "path/filepath" +) + +type Conf struct { + Source string + Dest string +} + +func loadConf() (*Conf, error) { + source := os.Getenv("CONF_SOURCE") + info, err := os.Stat(source) + if err != nil { + return nil, err + } + if !info.IsDir() { + return nil, errors.New("CONF_SOURCE is not a directory") + } + home, err := os.UserHomeDir() + if err != nil { + return nil, err + } + return &Conf{Source: source, Dest: home}, nil +} + +func (c *Conf) FileNames() ([]string, error) { + var names []string + 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(dir, path) + if err != nil { + return err + } + names = append(names, name) + return nil + }) + if err != nil { + return nil, err + } + return names, nil +} diff --git a/main.go b/main.go index 8c49ce3..52a47ba 100644 --- a/main.go +++ b/main.go @@ -1,16 +1,10 @@ package main import ( - "errors" "fmt" "os" ) -type Conf struct { - Source string - Dest string -} - func main() { conf, err := loadConf() if err != nil { @@ -18,7 +12,7 @@ func main() { os.Exit(1) } if len(os.Args) < 2 { - fmt.Printf("usage: conf [files...]\n") + fmt.Printf("usage: conf [files...]\n") os.Exit(1) } switch os.Args[1] { @@ -32,24 +26,13 @@ func main() { fmt.Printf("%v\n", err) os.Exit(1) } + case "status": + if err := status(conf, 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) } } - -func loadConf() (*Conf, error) { - source := os.Getenv("CONF_SOURCE") - info, err := os.Stat(source) - if err != nil { - return nil, err - } - if !info.IsDir() { - return nil, errors.New("CONF_SOURCE is not a directory") - } - home, err := os.UserHomeDir() - if err != nil { - return nil, err - } - return &Conf{Source: source, Dest: home}, nil -} diff --git a/status.go b/status.go new file mode 100644 index 0000000..b9c7aa1 --- /dev/null +++ b/status.go @@ -0,0 +1,30 @@ +package main + +import ( + "fmt" +) + +func status(conf *Conf, args []string) error { + if len(args) != 0 { + return statusFiles(conf, args) + } + names, err := conf.FileNames() + if err != nil { + return err + } + return statusFiles(conf, names) +} + +func statusFiles(conf *Conf, names []string) error { + for _, name := range names { + if err := statusFile(conf, name); err != nil { + return fmt.Errorf("could not check status of %s: %v", name, err) + } + } + return nil +} + +func statusFile(conf *Conf, name string) error { + fmt.Printf("%s: OK\n", name) + return nil +} -- cgit v1.2.3