package main import ( "flag" "fmt" "io" "log" "os" "runtime/pprof" "golang.flu0r1ne.net/planr" "golang.flu0r1ne.net/planr/adapters/gtest" "golang.flu0r1ne.net/planr/adapters/bash" "golang.flu0r1ne.net/planr/cmd/planr/sub" ) func printUsage(w io.Writer) { fmt.Fprintf (w, "usage: %s command args ... \n", os.Args[0]) fmt.Fprintln(w, " help ") fmt.Fprintln(w, " version ") fmt.Fprintln(w, " build ") fmt.Fprintln(w, " evaluate ") fmt.Fprintln(w, " evaluate ... ") fmt.Fprintln(w, " evaluate -json ") fmt.Fprintln(w, " clean ") fmt.Fprintln(w, " config ") } func dieUsage() { printUsage(os.Stderr) os.Exit(1) } var src = flag.String("srcdir", "", "source directory") var config = flag.String("configdir", "", "config directory") var build = flag.String("builddir", "", "build directory") var cpuprofile = flag.String("cpuprofile", "", "write cpu profile to file") func getConfiguredRunner() planr.Runner { r := planr.ConfigureRunner() r = planr.RegisterAdapter(r, gtest.NewAdapter()) r = planr.RegisterAdapter(r, bash.NewAdapter()) if wd, err := os.Getwd(); err == nil { r = planr.SetConfigDirFromTree(r, wd) } if *src != "" { r = planr.SetSrcDir(r, *src) } if *config != "" { r = planr.SetConfigDir(r, *config) } if *build != "" { r = planr.SetBuildDir(r, *build) } return r.New() } func main() { flag.Parse() if *cpuprofile != "" { f, err := os.Create(*cpuprofile) if err != nil { log.Fatal(err) } pprof.StartCPUProfile(f) defer pprof.StopCPUProfile() } log.SetFlags(log.Llongfile | log.Lmsgprefix) log.SetPrefix("planr: ") if flag.NArg() < 1 { dieUsage() } runner := getConfiguredRunner() cfg := planr.DecodeConfig(runner.ConfigDir()) subcommand := flag.Arg(0) subargs := flag.Args()[1:] switch subcommand { case "version": fmt.Printf("%s\n", planr.VERSION) case "build": sub.Build(runner, subargs, cfg) case "evaluate", "eval": sub.Evaluate(runner, subargs, cfg) case "clean": sub.Clean(runner, subargs) case "config": sub.Config(runner, subargs) case "help", "-h", "-help", "--help": printUsage(os.Stdout) default: fmt.Fprintf(os.Stderr, "unrecognized command %s\n", subcommand) dieUsage() } }