// Run the binary in binPath func Run(binPath string, binary string, args []string) error { // If windows add .exe to binary name if runtime.GOOS == "windows" { binary += ".exe" } // Set and check binFilePath binFilePath := filepath.Join(binPath, binary) ok, err := utils.PathExists(binFilePath) if err != nil { return err } // not found if !ok { return fmt.Errorf("Binary %s not found", binFilePath) } // Start the binary if err := utils.CmdRun(binFilePath, args...); err != nil { return err } return nil }
// get install all dependencies and rebuild vendors. func get(c *taskRunner.Context) { // Dependencies for install to GOPATH as string array depends := []string{} // Install all depends log.Println("Install dependencies into GOPATH.") for _, url := range depends { log.Println("Get <- ", url) if err := utils.CmdRun("go", "get", "-u", url); err != nil { log.Fatal(err) } } // Check if manifest exists ok, err := utils.PathExists(filepath.Join(c.Root, "vendor", "manifest")) if err != nil { log.Fatal(err) } // If manifest exists, fetch vendors with gvt if ok { log.Println("Install vendors into project.") if err := utils.CmdRun("gvt", "rebuild", "-connections 4"); err != nil { log.Fatal(err) } } }
// templateWorker create file from template // done channel send nil or error back func templateWorker(tpl *template.Template, ftpl FileTemplates, done chan error) { // check file template path ok, err := utils.PathExists(ftpl.Path) if err != nil { done <- err return } // file template path not exists, create new file template path if !ok { if err := os.MkdirAll(ftpl.Path, 0755); err != nil { done <- err return } } // create new target file f, err := os.OpenFile(filepath.Join(ftpl.Path, ftpl.Target), os.O_WRONLY|os.O_CREATE, 0755) if err != nil { done <- err return } defer f.Close() // generate file content, // send return to channel (nil or err) done <- tpl.ExecuteTemplate(f, ftpl.Tpl, ftpl.Vars) }
// run binary func run(c *taskRunner.Context) { // Overwrite default binary binary := "" // binPath binPath := filepath.Join("_build", "bin") // Start the build task before run binary taskRunner.RunTask("build", []string{}) // Check arguments for binary if len(c.Args) > 0 { // Check whether the first argument is a binary ok, err := utils.PathExists(filepath.Join(binPath, c.Args[0])) if err != nil { log.Fatal(err) } // If argument is binary, // set binary and overwrite arguments if ok { binary = c.Args[0] c.Args = c.Args[1:] } } // Use default binary, when binary not set if binary == "" { binary = c.ProjectName } // Run the binary if err := lxRun.Run(binPath, binary, c.Args); err != nil { log.Fatal(err) } }
func main() { // setup application returns *settings ... settings := setup() // create new cli app app := cli.NewApp() app.Version = lxb.Version app.Name = "lxb" app.Usage = `Scaffolding, vendoring and task runner for Go projects. Apart from the reserved commands all others are forwarded to the Task Runner.` // action of lxb app.Action = func(c *cli.Context) { // show Help when no args were passed. if len(c.Args()) == 0 { cli.ShowAppHelp(c) return } // only in GOPATH start task runner if !isGoPath(settings) { log.Fatal("Error: Project is not in GOPATH.") } // start the task runner // task passed in arguments startTaskRunner(c.Args()) } // sub commands for lxb app.Commands = []cli.Command{ { Name: "create", Usage: "Create a project in working directory.", Flags: []cli.Flag{ cli.BoolTFlag{ Name: "git", Usage: "git initial commit. --git=false for disable.", }, }, Action: func(c *cli.Context) { // Only in GOPATH create a project if !isGoPath(settings) { log.Fatal("Error: Project is not in GOPATH.") } settings.GitFlag = c.Bool("git") // create new project if err := createNewProject(settings); err != nil { log.Fatal(err) } log.Printf("Project %s has been created.", settings.ProjectName) }, }, { Name: "backup", Usage: "Backup glide.yaml and glide.lock files.", Action: func(c *cli.Context) { // glide.yaml and glide.lock paths srcYaml := filepath.Join(settings.Root, "glide.yaml") srcLock := filepath.Join(settings.Root, "glide.lock") dstYaml := filepath.Join(settings.Root, "glide.yaml.bak") dstLock := filepath.Join(settings.Root, "glide.lock.bak") // check if glide.yaml file exists okYaml, err := utils.PathExists(srcYaml) if err != nil { log.Fatal(err) } // check if glide.lock file exists okLock, err := utils.PathExists(srcLock) if err != nil { log.Fatal(err) } // not found glide.yaml if !okYaml { log.Println("No backup created, can not find glide.yaml file.") return } // create backup from glide.yaml if err := fileutils.Copyfile(dstYaml, srcYaml); err != nil { log.Fatal(err) } // if glide.lock exists save this file if okLock { if err := fileutils.Copyfile(dstLock, srcLock); err != nil { log.Fatal(err) } } log.Println("Backup from glide.yaml and glide.lock successfully created.") }, }, { Name: "restore", Usage: "Restore glide.yaml and glide.lock files and install.", Action: func(c *cli.Context) { // glide.yaml and glide.lock paths srcYaml := filepath.Join(settings.Root, "glide.yaml.bak") srcLock := filepath.Join(settings.Root, "glide.lock.bak") dstYaml := filepath.Join(settings.Root, "glide.yaml") dstLock := filepath.Join(settings.Root, "glide.lock") // check if backup files exists okYaml, err := utils.PathExists(srcYaml) if err != nil { log.Fatal(err) } // check if glide.lock file exists okLock, err := utils.PathExists(srcLock) if err != nil { log.Fatal(err) } // not found backup if !okYaml { log.Println("No backup restored, can not find glide.yaml.bak file.") return } // restore backups log.Println("Restore glide.yaml.bak -> glide.yaml") if err := fileutils.Copyfile(dstYaml, srcYaml); err != nil { log.Fatal(err) } // if glide.lock.bak exists restore this file if okLock { if err := fileutils.Copyfile(dstLock, srcLock); err != nil { log.Fatal(err) } } // install vendors log.Println("Install the vendors.") if err := utils.CmdRun("glide", "install"); err != nil { log.Fatal(err) } // delete old backups log.Println("Delete backup files") if err := os.Remove(srcYaml); err != nil { log.Fatal(err) } // if glide.lock.bak exists delete this file if okLock { if err := os.Remove(srcLock); err != nil { log.Fatal(err) } } log.Println("Restore vendors successfully.") }, }, } app.Run(os.Args) }