func buildMain(src string) string { mustBeMain(src) exeFile := "godobin-" + godo.Version if isWindows { exeFile += ".exe" } dir := filepath.Dir(src) exe := filepath.Join(dir, exeFile) reasonFormat := "Godofile changed. Rebuilding %s...\n" argm := minimist.Parse() rebuild := argm.ZeroBool("rebuild") if rebuild { os.Remove(exe) } // see if last run exists .godoinfo fiExe, err := os.Stat(exe) build := os.IsNotExist(err) if build { reasonFormat = "Building %s...\n" } fiGodofile, err := os.Stat(src) if os.IsNotExist(err) { log.Fatalln(err) os.Exit(1) } build = build || fiExe.ModTime().Before(fiGodofile.ModTime()) if build { util.Debug("godo", reasonFormat, exe) err := godo.Run("go build -a -o "+exeFile, godo.In{dir}) if err != nil { panic(err) } } if rebuild { util.Info("godo", "ok") os.Exit(0) } return exe }
func handleCLI() (proto, domain string, port, health int) { args := minimist.Parse() if args.MayBool(false, "help", "h", "?") { fmt.Println(usage) os.Exit(0) } if args.MayBool(false, "v", "version") { fmt.Println(version) os.Exit(0) } port = args.MayInt(8080, "port", "p") health = args.AsInt("health") domain = args.MayString("127.0.0.1", "domain", "d") proto = args.MayString("http", "proto") return proto, domain, port, health }
func main() { argm := minimist.Parse() fmt.Printf("%q\n", os.Args) // cmd --help || cmd --h || cmd -? if argm.MayBool(false, "help", "h", "?") { fmt.Println(usage) } // cmd -v || cmd --version if argm.AsBool("v", "version") { fmt.Println("1.0") } // cmd foo -- ... // argm.SubCommand("foo", func(a *ArgMap) { // }) // argm.SubExec("talk", "echo") }
func main() { // v2 ONLY uses Gododir/main.go godoFiles := []string{"Gododir/main.go", "Gododir/Godofile.go", "tasks/Godofile.go"} src := "" for _, filename := range godoFiles { src = util.FindUp(".", filename) if src != "" { break } } if src == "" { fmt.Printf("\n\n%s not found\n", src) os.Exit(1) } wd, err := os.Getwd() if err != nil { util.Error("godo", "Could not get working directory: %s\n", err.Error()) } // parent of Gododir/main.go absParentDir, err := filepath.Abs(filepath.Dir(filepath.Dir(src))) if err != nil { util.Error("godo", "Could not get absolute parent of %s: %s\n", src, err.Error()) } if wd != absParentDir { relDir, _ := filepath.Rel(wd, src) os.Chdir(absParentDir) util.Info("godo", "Using %s\n", relDir) } os.Setenv("GODOFILE", src) argm := minimist.Parse() isRebuild = argm.AsBool("rebuild") isWatch = argm.AsBool("w", "watch") isVerbose = argm.AsBool("v", "verbose") hasTasks = len(argm.NonFlags()) > 0 run(src) }
func godo(tasksFunc func(*Project), argv []string) { if argv == nil { argm = minimist.Parse() } else { argm = minimist.ParseArgv(argv) } help = argm.ZeroBool("help", "h", "?") verbose = argm.ZeroBool("verbose", "v") version = argm.ZeroBool("version", "V") watching = argm.ZeroBool("watch", "w") deprecatedWarnings = argm.ZeroBool("D") contextArgm = minimist.ParseArgv(argm.Unparsed()) project := NewProject(tasksFunc) if help { Usage(project.usage()) os.Exit(0) } if version { fmt.Printf("godo %s\n", Version) os.Exit(0) } // Run each task including their dependencies. args := []string{} for _, v := range argm.Leftover() { args = append(args, fmt.Sprintf("%v", v)) } if len(args) == 0 { if project.Tasks["default"] != nil { args = append(args, "default") } else { Usage(project.usage()) os.Exit(0) } } // quick fix to make cascading watch work on default task if len(args) == 1 && args[0] == "default" { args = project.Tasks["default"].Dependencies } for _, name := range args { err := project.Run(name) if err != nil { util.Error("ERR", "%s\n", err.Error()) os.Exit(1) } } if watching { if project.Watch(args, true) { waitgroup.Add(1) waitExit = true } else { fmt.Println("Nothing to watch. Use W{} or Watch{} to specify glob patterns") os.Exit(0) } } if waitExit { waitgroup.Wait() } }
// used for testing to switch out exitFn func godoExit(tasksFunc func(*Project), argv []string, exitFn func(int)) { if argv == nil { argm = minimist.Parse() } else { argm = minimist.ParseArgv(argv) } dump := argm.AsBool("dump") help = argm.AsBool("help", "h", "?") verbose = argm.AsBool("verbose", "v") version = argm.AsBool("version", "V") watching = argm.AsBool("watch", "w") deprecatedWarnings = argm.AsBool("D") contextArgm := minimist.ParseArgv(argm.Unparsed()) project := NewProject(tasksFunc, exitFn, contextArgm) if help { Usage(project.usage()) exitFn(0) } if version { fmt.Printf("godo %s\n", Version) exitFn(0) } if dump { project.dump(os.Stdout, "", " ") exitFn(0) } // env vars are any nonflag key=value pair addToOSEnviron(argm.NonFlags()) // Run each task including their dependencies. args := []string{} for _, s := range argm.NonFlags() { // skip env vars if !strings.Contains(s, "=") { args = append(args, s) } } if len(args) == 0 { if project.Tasks["default"] != nil { args = append(args, "default") } else { Usage(project.usage()) exitFn(0) } } for _, name := range args { err := project.Run(name) if err != nil { util.Error("ERR", "%s\n", err.Error()) exitFn(1) } } if watching { if project.Watch(args, true) { runnerWaitGroup.Add(1) waitExit = true } else { fmt.Println("Nothing to watch. Use Task#Src() to specify watch patterns") exitFn(0) } } if waitExit { // Ctrl+C handler csig := make(chan os.Signal, 1) signal.Notify(csig, syscall.SIGQUIT) go func() { for sig := range csig { fmt.Println("SIG caught") if sig == syscall.SIGQUIT { fmt.Println("SIG caught B") project.Exit(0) break } } }() runnerWaitGroup.Wait() } exitFn(0) }