More cleanup

This commit is contained in:
Alex Yatskov 2015-04-02 18:16:59 +09:00
parent e026f82efd
commit 688fed0482
3 changed files with 57 additions and 24 deletions

View File

@ -28,11 +28,15 @@ type config struct {
Tasks map[string]task Tasks map[string]task
} }
func (this config) install(name, srcDir, dstDir string) error { func (this *config) install(srcDir, dstDir, taskName string, flags int) error {
task, ok := this.Tasks[name] task, ok := this.Tasks[taskName]
if !ok { 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
} }

59
main.go
View File

@ -24,10 +24,19 @@ package main
import ( import (
"flag" "flag"
"fmt"
"github.com/naoina/toml" "github.com/naoina/toml"
"io/ioutil" "io/ioutil"
"log" "log"
"os"
"os/user" "os/user"
"path"
)
const (
optClobber = 1 << 0
optForce = 1 << 1
optVerbose = 1 << 2
) )
func parse(filename string) (*config, error) { func parse(filename string) (*config, error) {
@ -44,12 +53,11 @@ func parse(filename string) (*config, error) {
return conf, nil return conf, nil
} }
func install(conf config, name, target, source string, force, clobber bool) error { func printUsageAndExit() {
return nil _, executable := path.Split(os.Args[0])
} fmt.Errorf("Usage: %s [options] config_file [target_path]", executable)
flag.PrintDefaults()
func uninstall(conf config, name, target string, force, clobber bool) error { os.Exit(1)
return nil
} }
func main() { func main() {
@ -58,28 +66,49 @@ func main() {
log.Fatal(err) log.Fatal(err)
} }
taskName := flag.String("task", "default", "name of task to execute")
action := flag.String("action", "install", "'install' or 'uninstall' symlinks") 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") clobber := flag.Bool("clobber", false, "delete files and directories at target")
force := flag.Bool("force", true, "force creation of parent directories for target") verbose := flag.Bool("verbose", false, "verbose output")
profile := flag.String("profile", "default", "name of profile to execute")
target := flag.String("target", currUsr.HomeDir, "target directory for symlinks")
flag.Parse() flag.Parse()
confPath := flag.Arg(0) flags := 0
source := flag.Arg(1) 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 { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
switch *action { switch *action {
case "install": 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": case "uninstall":
uninstall(*conf, *profile, *target, *force, *clobber) if err := conf.uninstall(*dstDir, *taskName, flags); err != nil {
log.Fatal(err)
}
default: default:
log.Fatalf("Unrecognized action: '%s'", action) printUsageAndExit()
} }
} }

10
task.go
View File

@ -29,14 +29,14 @@ type task struct {
Links []link Links []link
} }
func (this task) install(srcDir, dstDir string, conf config) error { func (this task) install(srcDir, dstDir string, conf *config, flags int) error {
for _, name := range this.Deps { for _, depName := range this.Deps {
depTask, ok := conf.Tasks[name] depTask, ok := conf.Tasks[depName]
if !ok { 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 return err
} }
} }