func runRun(ctx *cli.Context) { setup(ctx) // Get GOPATH. installGopath = com.GetGOPATHs()[0] if com.IsDir(installGopath) { isHasGopath = true log.Log("Indicated GOPATH: %s", installGopath) installGopath += "/src" } genNewGoPath(ctx, false) log.Trace("Running...") cmdArgs := []string{"go", "run"} cmdArgs = append(cmdArgs, ctx.Args()...) err := execCmd(newGoPath, newCurPath, cmdArgs...) if err != nil { log.Error("run", "Fail to run program:") log.Fatal("", "\t"+err.Error()) } log.Success("SUCC", "run", "Command executed successfully!") }
// GetGOPATH returns best matched GOPATH. func GetBestMatchGOPATH(appPath string) string { paths := com.GetGOPATHs() for _, p := range paths { if strings.HasPrefix(p, appPath) { return strings.Replace(p, "\\", "/", -1) } } return paths[0] }
// CheckIsExistInGOPATH checks if given package import path exists in any path in GOPATH/src, // and returns corresponding GOPATH. func CheckIsExistInGOPATH(importPath string) (string, bool) { paths := com.GetGOPATHs() for _, p := range paths { if CheckIsExistWithVCS(p + "/src/" + importPath + "/") { return p, true } } return "", false }
func runBuild(ctx *cli.Context) { setup(ctx) // Get GOPATH. installGopath = com.GetGOPATHs()[0] if com.IsDir(installGopath) { isHasGopath = true log.Log("Indicated GOPATH: %s", installGopath) installGopath += "/src" } buildBinary(ctx, ctx.Args()...) log.Success("SUCC", "build", "Command executed successfully!") }
func runGet(ctx *cli.Context) { setup(ctx) // Check conflicts. if ctx.Bool("gopath") && ctx.Bool("remote") { log.Error("get", "Command options have conflicts") log.Error("", "Following options are not supposed to use at same time:") log.Error("", "\t'--gopath, -g' '--remote, -r'") log.Help("Try 'gopm help get' to get more information") } if !ctx.Bool("remote") { // Get GOPATH. installGopath = com.GetGOPATHs()[0] if com.IsDir(installGopath) { isHasGopath = true log.Log("Indicated GOPATH: %s", installGopath) installGopath += "/src" } else { if ctx.Bool("gopath") { log.Error("get", "Invalid GOPATH path") log.Error("", "GOPATH does not exist or is not a directory:") log.Error("", "\t"+installGopath) log.Help("Try 'go help gopath' to get more information") } else { // It's OK that no GOPATH setting // when user does not specify to use. log.Warn("No GOPATH setting available") } } } // The gopm local repository. installRepoPath = doc.HomeDir + "/repos" log.Log("Local repository path: %s", installRepoPath) // Check number of arguments to decide which function to call. switch len(ctx.Args()) { case 0: getByGopmfile(ctx) default: getByPath(ctx) } }
func main() { flag.Parse() if len(*to) == 0 { log.Fatalln("Option '-to' cannot be empty") } else if len(*from) == 0 { log.Fatalln("Option '-from' cannot be empty") } if (*to)[len(*to)-1] != '/' { *to += "/" } if (*from)[len(*from)-1] != '/' { *from += "/" } gopath := com.GetGOPATHs()[0] rootPath := path.Join(gopath, "src", *to) if !com.IsDir(rootPath) { log.Fatalf("Given path(%s) does not exist or not a directory", rootPath) } if err := filepath.Walk(rootPath, func(path string, info os.FileInfo, err error) error { if info.IsDir() || (!strings.HasSuffix(path, ".go") && !strings.HasSuffix(path, "Dockerfile")) { return nil } fmt.Println(path) data, err := ioutil.ReadFile(path) if err != nil { return err } data = bytes.Replace(data, []byte(*from), []byte(*to), -1) if err = ioutil.WriteFile(path, data, info.Mode()); err != nil { return err } return nil }); err != nil { log.Fatalf("Fail to walk: %v", err) } }
func runInstall(ctx *cli.Context) { setup(ctx) var target string switch len(ctx.Args()) { case 0: if !com.IsFile(".gopmfile") { break } gf := doc.NewGopmfile(".") target = gf.MustValue("target", "path") case 1: target = ctx.Args()[0] default: log.Fatal("install", "Too many arguments") } // Get GOPATH. installGopath = com.GetGOPATHs()[0] if com.IsDir(installGopath) { isHasGopath = true log.Log("Indicated GOPATH: %s", installGopath) installGopath += "/src" } else { if ctx.Bool("gopath") { log.Error("get", "Invalid GOPATH path") log.Error("", "GOPATH does not exist or is not a directory:") log.Error("", "\t"+installGopath) log.Help("Try 'go help gopath' to get more information") } else { // It's OK that no GOPATH setting // when user does not specify to use. log.Warn("No GOPATH setting available") } } genNewGoPath(ctx, false) var installRepos []string if ctx.Bool("pkg") { curPath, _ := filepath.Abs(".") installRepos = doc.GetAllImports([]string{curPath}, ".", ctx.Bool("example")) } else { if len(target) == 0 { target = pkgName } installRepos = []string{target} } log.Trace("Installing...") for _, repo := range installRepos { cmdArgs := []string{"go", "install"} if ctx.Bool("verbose") { cmdArgs = append(cmdArgs, "-v") } cmdArgs = append(cmdArgs, repo) err := execCmd(newGoPath, newCurPath, cmdArgs...) if err != nil { log.Error("install", "Fail to install program:") log.Fatal("", "\t"+err.Error()) } } log.Success("SUCC", "install", "Command executed successfully!") }
func runRun(ctx *cli.Context) { setup(ctx) //support unix only if ctx.Bool("local") { var localPath string var err error var wd string var gf *goconfig.ConfigFile wd, _ = os.Getwd() for wd != "/" { gf, _ = goconfig.LoadConfigFile(".gopmfile") if gf != nil { localPath = gf.MustValue("project", "localPath") } if localPath != "" { break } os.Chdir("..") wd, _ = os.Getwd() } if wd == "/" { log.Fatal("run", "no gopmfile in the directory or parent directory") } argss := gf.MustValue("run", "cmd") if localPath == "" { log.Fatal("run", "No localPath set") } args := strings.Split(argss, " ") argsLen := len(args) for i := 0; i < argsLen; i++ { strings.Trim(args[i], " ") } if len(args) < 2 { log.Fatal("run", "cmd arguments less than 2") } err = execCmd(localPath, localPath, args...) if err != nil { log.Error("run", "Fail to run program:") log.Fatal("", "\t"+err.Error()) } return } // Get GOPATH. installGopath = com.GetGOPATHs()[0] if com.IsDir(installGopath) { isHasGopath = true log.Log("Indicated GOPATH: %s", installGopath) installGopath += "/src" } // run command with gopm repos context // need version control , auto link to GOPATH/src repos genNewGoPath(ctx, false) defer os.RemoveAll(doc.VENDOR) log.Trace("Running...") cmdArgs := []string{"go", "run"} cmdArgs = append(cmdArgs, ctx.Args()...) err := execCmd(newGoPath, newCurPath, cmdArgs...) if err != nil { log.Error("run", "Fail to run program:") log.Fatal("", "\t"+err.Error()) } log.Success("SUCC", "run", "Command executed successfully!") }
// UnpackPath replaces special path variables and returns full path. func UnpackPath(path string) string { path = strings.Replace(path, "$WORKDIR", WorkDir, 1) path = strings.Replace(path, "$GOPATH", com.GetGOPATHs()[0], 1) return path }
func ExampleGetGOPATHs() { gps := com.GetGOPATHs() fmt.Println(gps) }
func runGet(ctx *cli.Context) { setup(ctx) // Check conflicts. if ctx.Bool("gopath") && ctx.Bool("remote") || ctx.Bool("local") && ctx.Bool("remote") || ctx.Bool("gopath") && ctx.Bool("local") { e := " " if ctx.Bool("gopath") { e += "--gopth,-g " } if ctx.Bool("remote") { e += "--remote,-r " } if ctx.Bool("local") { e += "--local,-l " } log.Error("get", "Command options have conflicts") log.Error("", "Following options are not supposed to use at same time:") log.Error("", "\t"+e) log.Help("Try 'gopm help get' to get more information") } if !ctx.Bool("remote") { // Get GOPATH. installGopath = com.GetGOPATHs()[0] if com.IsDir(installGopath) { isHasGopath = true log.Log("Indicated GOPATH: %s", installGopath) installGopath += "/src" } else { if ctx.Bool("gopath") { log.Error("get", "Invalid GOPATH path") log.Error("", "GOPATH does not exist or is not a directory:") log.Error("", "\t"+installGopath) log.Help("Try 'go help gopath' to get more information") } else { // It's OK that no GOPATH setting // when user does not specify to use. log.Warn("No GOPATH setting available") } } // if flag local set use localPath as GOPATH if ctx.Bool("local") { if !com.IsExist(".gopmfile") { runGen(ctx) } gf, err := goconfig.LoadConfigFile(".gopmfile") if err != nil { log.Fatal("get", err.Error()) } else { installGopath = gf.MustValue("project", "localPath") if installGopath == "" { os.Remove(".gopmfile") log.Fatal("get", "unexpected localPath or no localPath exists") } installGopath += "/src" } } } // The gopm local repository. installRepoPath = path.Join(doc.HomeDir, "repos") log.Log("Local repository path: %s", installRepoPath) // Check number of arguments to decide which function to call. switch len(ctx.Args()) { case 0: getByGopmfile(ctx) case 1: getByPath(ctx) default: log.Error("get", "too many arguments") log.Help("Try 'gopm help get' to get more information") } }