From 688fed04829b3f8f26d315d3f2a0a67fadd51814 Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Thu, 2 Apr 2015 18:16:59 +0900 Subject: [PATCH] More cleanup --- config.go | 12 +++++++---- main.go | 59 +++++++++++++++++++++++++++++++++++++++++-------------- task.go | 10 +++++----- 3 files changed, 57 insertions(+), 24 deletions(-) diff --git a/config.go b/config.go index 427de7e..cd2040d 100644 --- a/config.go +++ b/config.go @@ -28,11 +28,15 @@ type config struct { Tasks map[string]task } -func (this config) install(name, srcDir, dstDir string) error { - task, ok := this.Tasks[name] +func (this *config) install(srcDir, dstDir, taskName string, flags int) error { + task, ok := this.Tasks[taskName] if !ok { - return fmt.Errorf("Profile not found: '%s'", name) + return fmt.Errorf("Profile not found: '%s'", taskName) } - return task.install(srcDir, dstDir, this) + return task.install(srcDir, dstDir, this, flags) +} + +func (this *config) uninstall(dstDir, taskName string, flags int) error { + return nil } diff --git a/main.go b/main.go index df3189c..2b34e16 100644 --- a/main.go +++ b/main.go @@ -24,10 +24,19 @@ package main import ( "flag" + "fmt" "github.com/naoina/toml" "io/ioutil" "log" + "os" "os/user" + "path" +) + +const ( + optClobber = 1 << 0 + optForce = 1 << 1 + optVerbose = 1 << 2 ) func parse(filename string) (*config, error) { @@ -44,12 +53,11 @@ func parse(filename string) (*config, error) { return conf, nil } -func install(conf config, name, target, source string, force, clobber bool) error { - return nil -} - -func uninstall(conf config, name, target string, force, clobber bool) error { - return nil +func printUsageAndExit() { + _, executable := path.Split(os.Args[0]) + fmt.Errorf("Usage: %s [options] config_file [target_path]", executable) + flag.PrintDefaults() + os.Exit(1) } func main() { @@ -58,28 +66,49 @@ func main() { log.Fatal(err) } + taskName := flag.String("task", "default", "name of task to execute") action := flag.String("action", "install", "'install' or 'uninstall' symlinks") + dstDir := flag.String("target", currUsr.HomeDir, "target directory for symlinks") + force := flag.Bool("force", true, "create parent directories to target") clobber := flag.Bool("clobber", false, "delete files and directories at target") - force := flag.Bool("force", true, "force creation of parent directories for target") - profile := flag.String("profile", "default", "name of profile to execute") - target := flag.String("target", currUsr.HomeDir, "target directory for symlinks") + verbose := flag.Bool("verbose", false, "verbose output") flag.Parse() - confPath := flag.Arg(0) - source := flag.Arg(1) + flags := 0 + if *clobber { + flags |= optClobber + } + if *force { + flags |= optForce + } + if *verbose { + flags |= optVerbose + } - conf, err := parse(confPath) + if flag.NArg() == 0 { + printUsageAndExit() + } + + conf, err := parse(flag.Arg(0)) if err != nil { log.Fatal(err) } switch *action { case "install": - install(*conf, *profile, *target, source, *force, *clobber) + if flag.NArg() >= 2 { + if err := conf.install(flag.Arg(1), *dstDir, *taskName, flags); err != nil { + log.Fatal(err) + } + } else { + printUsageAndExit() + } case "uninstall": - uninstall(*conf, *profile, *target, *force, *clobber) + if err := conf.uninstall(*dstDir, *taskName, flags); err != nil { + log.Fatal(err) + } default: - log.Fatalf("Unrecognized action: '%s'", action) + printUsageAndExit() } } diff --git a/task.go b/task.go index aedd75f..66476a8 100644 --- a/task.go +++ b/task.go @@ -29,14 +29,14 @@ type task struct { Links []link } -func (this task) install(srcDir, dstDir string, conf config) error { - for _, name := range this.Deps { - depTask, ok := conf.Tasks[name] +func (this task) install(srcDir, dstDir string, conf *config, flags int) error { + for _, depName := range this.Deps { + depTask, ok := conf.Tasks[depName] if !ok { - return fmt.Errorf("Task dependency not found: '%s'", name) + return fmt.Errorf("Task dependency not found: '%s'", depName) } - if err := depTask.install(srcDir, dstDir, conf); err != nil { + if err := depTask.install(srcDir, dstDir, conf, flags); err != nil { return err } }