// downloadPackage downloads package either use version control tools or not. func downloadPackage(ctx *cli.Context, nod *doc.Node) (*doc.Node, []string) { log.Message("Downloading", fmt.Sprintf("package: %s@%s:%s", nod.ImportPath, nod.Type, doc.CheckNodeValue(nod.Value))) // Mark as donwloaded. downloadCache[nod.RootPath] = true // Check if only need to use VCS tools. var imports []string var err error gopathDir := path.Join(installGopath, nod.RootPath) vcs := getVcsName(gopathDir) if ctx.Bool("update") && ctx.Bool("gopath") && len(vcs) > 0 { err = updateByVcs(vcs, gopathDir) imports = doc.GetAllImports([]string{gopathDir}, nod.RootPath, false) } else { // If package has revision and exist, then just check dependencies. if nod.IsGetDepsOnly { return nod, doc.GetAllImports([]string{path.Join(installRepoPath, nod.RootPath) + versionSuffix(nod.Value)}, nod.RootPath, ctx.Bool("example")) } nod.Revision = doc.LocalNodes.MustValue(nod.RootPath, "value") imports, err = doc.PureDownload(nod, installRepoPath, ctx) //CmdGet.Flags) } if err != nil { log.Error("get", "Fail to download pakage: "+nod.ImportPath) log.Error("", "\t"+err.Error()) failConut++ os.RemoveAll(installRepoPath + "/" + nod.RootPath) return nil, nil } return nod, imports }
func init() { hd, err := com.HomeDir() if err != nil { log.Error("", "Fail to get current user") log.Fatal("", err.Error()) } HomeDir = strings.Replace(RawHomeDir, "~", hd, -1) cfgPath := path.Join(HomeDir, GOPM_CONFIG_FILE) if !com.IsExist(cfgPath) { os.MkdirAll(path.Dir(cfgPath), os.ModePerm) if _, err = os.Create(cfgPath); err != nil { log.Error("", "Fail to create gopm config file") log.Fatal("", err.Error()) } } Cfg, err = goconfig.LoadConfigFile(cfgPath) if err != nil { log.Error("", "Fail to load gopm config file") log.Fatal("", err.Error()) } LoadLocalNodes() LoadPkgNameList(path.Join(HomeDir, PKG_NAME_LIST_PATH)) }
// scan a directory and gen a gopm file func runGen(ctx *cli.Context) { setup(ctx) if !com.IsExist(".gopmfile") { os.Create(".gopmfile") } gf, err := goconfig.LoadConfigFile(".gopmfile") if err != nil { log.Error("gen", "Cannot load gopmfile:") log.Fatal("", "\t"+err.Error()) } // Get dependencies. imports := doc.GetAllImports([]string{workDir}, parseTarget(gf.MustValue("target", "path")), ctx.Bool("example")) for _, p := range imports { p = doc.GetProjectPath(p) if strings.HasSuffix(workDir, p) { continue } if value := gf.MustValue("deps", p); len(value) == 0 { gf.SetValue("deps", p, "") } } err = goconfig.SaveConfigFile(gf, ".gopmfile") if err != nil { log.Error("gen", "Fail to save gopmfile:") log.Fatal("", "\t"+err.Error()) } log.Success("SUCC", "gen", "Generate gopmfile successfully!") }
// validPath checks if the information of the package is valid. func validPath(info string) (string, string) { infos := strings.Split(info, ":") l := len(infos) switch { case l == 1: // for local imports if com.IsFile(infos[0]) { return doc.LOCAL, infos[0] } return doc.BRANCH, "" case l == 2: switch infos[1] { case doc.TRUNK, doc.MASTER, doc.DEFAULT: infos[1] = "" } return infos[0], infos[1] default: log.Error("", "Cannot parse dependency version:") log.Error("", "\t"+info) log.Help("Try 'gopm help get' to get more information") return "", "" } }
func SaveLocalNodes() { if err := goconfig.SaveConfigFile(LocalNodes, path.Join(HomeDir+LocalNodesFile)); err != nil { log.Error("", "Fail to save localnodes.list:") log.Error("", "\t"+err.Error()) } }
func LoadPkgNameList(filePath string) { PackageNameList = make(map[string]string) // If file does not exist, simply ignore. if !com.IsFile(filePath) { return } data, err := ioutil.ReadFile(filePath) if err != nil { log.Error("Package name list", "Fail to read file") log.Fatal("", err.Error()) } pkgs := strings.Split(string(data), "\n") for i, line := range pkgs { infos := strings.Split(line, "=") if len(infos) != 2 { // Last item might be empty line. if i == len(pkgs)-1 { continue } log.Error("", "Fail to parse package name: "+line) log.Fatal("", "Invalid package name information") } PackageNameList[strings.TrimSpace(infos[0])] = strings.TrimSpace(infos[1]) } }
func buildBinary(ctx *cli.Context, args ...string) { genNewGoPath(ctx, false) log.Trace("Building...") cmdArgs := []string{"go", "build"} cmdArgs = append(cmdArgs, args...) err := execCmd(newGoPath, newCurPath, cmdArgs...) if err != nil { log.Error("build", "fail to build program:") log.Fatal("", "\t"+err.Error()) } if isWindowsXP { fName := path.Base(pkgName) binName := fName + ".exe" os.Remove(binName) exePath := filepath.Join(curPath, doc.VENDOR, "src", pkgName, binName) if com.IsFile(exePath) { err = os.Rename(exePath, filepath.Join(curPath, binName)) if err != nil { log.Error("build", "fail to move binary:") log.Fatal("", "\t"+err.Error()) } } else { log.Warn("No binary generated") } } }
func GetPkgFullPath(short string) string { name, ok := PackageNameList[short] if !ok { log.Error("", "Invalid package name") log.Error("", "It's not a invalid import path and no match in the package name list:") log.Fatal("", "\t"+short) } return name }
func execCmd(gopath, curPath string, args ...string) error { cwd, err := os.Getwd() if err != nil { log.Error("", "Fail to get work directory:") log.Fatal("", "\t"+err.Error()) } log.Log("Changing work directory to %s", curPath) err = os.Chdir(curPath) if err != nil { log.Error("", "Fail to change work directory:") log.Fatal("", "\t"+err.Error()) } defer func() { log.Log("Changing work directory back to %s", cwd) os.Chdir(cwd) }() err = os.Chdir(curPath) if err != nil { log.Error("", "Fail to change work directory:") log.Fatal("", "\t"+err.Error()) } oldGoPath = os.Getenv("GOPATH") log.Log("Setting GOPATH to %s", gopath) sep := ":" if runtime.GOOS == "windows" { sep = ";" } err = os.Setenv("GOPATH", gopath+sep+oldGoPath) if err != nil { log.Error("", "Fail to setting GOPATH:") log.Fatal("", "\t"+err.Error()) } defer func() { log.Log("Setting GOPATH back to %s", oldGoPath) os.Setenv("GOPATH", oldGoPath) }() cmd := exec.Command(args[0], args[1:]...) cmd.Stdout = os.Stdout cmd.Stderr = os.Stderr log.Log("===== application outputs start =====\n") err = cmd.Run() log.Log("====== application outputs end ======") return err }
func exePath() string { file, err := exec.LookPath(os.Args[0]) if err != nil { log.Error("Update", "Fail to execute exec.LookPath") log.Fatal("", err.Error()) } path, err := filepath.Abs(file) if err != nil { log.Error("Update", "Fail to get absolutely path") log.Fatal("", err.Error()) } return path }
// CheckIsExistWithVCS returns false if directory only has VCS folder, // or doesn't exist. func CheckIsExistWithVCS(path string) bool { // Check if directory exist. if !com.IsExist(path) { return false } // Check if only has VCS folder. dirs, err := GetDirsInfo(path) if err != nil { log.Error("", "Fail to get directory's information") log.Fatal("", err.Error()) } if len(dirs) > 1 { return true } else if len(dirs) == 0 { return false } switch dirs[0].Name() { case ".git", ".hg", ".svn": return false } return true }
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!") }
// GetDirsInfo returns os.FileInfo of all sub-directories in root path. func GetDirsInfo(rootPath string) []os.FileInfo { rootDir, err := os.Open(rootPath) if err != nil { log.Error("", "Fail to open directory") log.Fatal("", err.Error()) } defer rootDir.Close() dirs, err := rootDir.Readdir(0) if err != nil { log.Error("", "Fail to read directory") log.Fatal("", err.Error()) } return dirs }
// GetImports returns package denpendencies. func GetImports(absPath, importPath string, example bool) []string { pkg, err := build.ImportDir(absPath, build.AllowBinary) if err != nil { if _, ok := err.(*build.NoGoError); !ok { log.Error("", "Fail to get imports") log.Fatal("", err.Error()) } } fis := GetDirsInfo(absPath) absPath += "/" dirs := make([]string, 0) for _, fi := range fis { if fi.IsDir() && !strings.Contains(fi.Name(), VENDOR) { dirs = append(dirs, absPath+fi.Name()) } } imports := make([]string, 0, len(pkg.Imports)) for _, p := range pkg.Imports { if !IsGoRepoPath(p) && !strings.HasPrefix(p, importPath) { imports = append(imports, p) } } if len(dirs) > 0 { imports = append(imports, GetAllImports(dirs, importPath, example)...) } return imports }
func copyToGopath(srcPath, destPath string) { os.RemoveAll(destPath) err := com.CopyDir(srcPath, destPath) if err != nil { log.Error("download", "Fail to copy to GOPATH:") log.Fatal("", "\t"+err.Error()) } }
func runGen(ctx *cli.Context) { setup(ctx) if !com.IsExist(".gopmfile") { os.Create(".gopmfile") } gf, err := goconfig.LoadConfigFile(".gopmfile") if err != nil { log.Error("gen", "Cannot load gopmfile:") log.Fatal("", "\t"+err.Error()) } targetPath := parseTarget(gf.MustValue("target", "path")) // Get and set dependencies. imports := doc.GetAllImports([]string{workDir}, targetPath, ctx.Bool("example"), false) for _, p := range imports { p = doc.GetProjectPath(p) // Skip subpackage(s) of current project. if isSubpackage(p, targetPath) { continue } // Check if user specified the version. if value := gf.MustValue("deps", p); len(value) == 0 { gf.SetValue("deps", p, "") } } // Get and set resources. res := make([]string, 0, len(commonRes)) for _, cr := range commonRes { if com.IsExist(cr) { res = append(res, cr) } } gf.SetValue("res", "include", strings.Join(res, "|")) err = goconfig.SaveConfigFile(gf, ".gopmfile") if err != nil { log.Error("gen", "Fail to save gopmfile:") log.Fatal("", "\t"+err.Error()) } log.Success("SUCC", "gen", "Generate gopmfile successfully!") }
// downloadPackage downloads package either use version control tools or not. func downloadPackage(ctx *cli.Context, nod *doc.Node) (*doc.Node, []string) { log.Message("Downloading", fmt.Sprintf("package: %s@%s:%s", nod.ImportPath, nod.Type, doc.CheckNodeValue(nod.Value))) // Mark as donwloaded. downloadCache[nod.RootPath] = true nod.Revision = doc.LocalNodes.MustValue(nod.RootPath, "value") imports, err := doc.PureDownload(nod, installRepoPath, ctx) //CmdGet.Flags) if err != nil { log.Error("get", "Fail to download pakage: "+nod.ImportPath) log.Error("", "\t"+err.Error()) failConut++ os.RemoveAll(installRepoPath + "/" + nod.RootPath) return nil, nil } return nod, imports }
// NewGopmfile loads gopmgile in given directory. func NewGopmfile(dirPath string) *goconfig.ConfigFile { dirPath, _ = filepath.Abs(dirPath) gf, err := goconfig.LoadConfigFile(path.Join(dirPath, GOPM_FILE_NAME)) if err != nil { log.Error("", "Fail to load gopmfile:") log.Fatal("", "\t"+err.Error()) } return gf }
// setup initialize common environment for commands. func setup(ctx *cli.Context) { var err error workDir, err = os.Getwd() if err != nil { log.Error("setup", "Fail to get work directory:") log.Fatal("", "\t"+err.Error()) } log.PureMode = ctx.GlobalBool("noterm") log.Verbose = ctx.Bool("verbose") }
func loadLocalVerInfo() (ver version) { verPath := path.Join(doc.HomeDir, doc.VER_PATH) // First time run should not exist. if !com.IsExist(verPath) { return ver } f, err := os.Open(verPath) if err != nil { log.Error("Update", "Fail to open VERSION.json") log.Fatal("", err.Error()) } if err := json.NewDecoder(f).Decode(&ver); err != nil { log.Error("Update", "Fail to decode VERSION.json") log.Fatal("", err.Error()) } return ver }
func runConfig(ctx *cli.Context) { setup(ctx) if len(ctx.Args()) == 0 { log.Error("config", "Cannot start command:") log.Fatal("", "\tNo section specified") } switch ctx.Args()[0] { case "github": if len(ctx.Args()) < 3 { log.Error("config", "Cannot config section 'github'") log.Fatal("", "\tNot enough arguments for client_id and client_secret") } doc.Cfg.SetValue("github", "client_id", ctx.Args()[1]) doc.Cfg.SetValue("github", "client_secret", ctx.Args()[2]) goconfig.SaveConfigFile(doc.Cfg, path.Join(doc.HomeDir, doc.GOPM_CONFIG_FILE)) } log.Success("SUCC", "config", "Command executed successfully!") }
func init() { hd, err := com.HomeDir() if err != nil { log.Error("", "Fail to get current user") log.Fatal("", err.Error()) } HomeDir = strings.Replace(RawHomeDir, "~", hd, -1) LoadLocalNodes() LoadPkgNameList(HomeDir + "/data/pkgname.list") }
func getGopmPkgs(dirPath string, isTest bool) (pkgs map[string]*doc.Pkg, err error) { absPath, err := filepath.Abs(dirPath) if err != nil { log.Error("", "Fail to get absolute path of work directory:") log.Fatal("", "\t"+err.Error()) } var builds map[string]string if com.IsFile(absPath + "/" + doc.GOPM_FILE_NAME) { gf := doc.NewGopmfile(absPath) if builds, err = gf.GetSection("deps"); err != nil { builds = nil } } imports := doc.GetAllImports([]string{dirPath}, ".", false, false) pkgs = make(map[string]*doc.Pkg) for _, name := range imports { if name == "C" { continue } if !doc.IsGoRepoPath(name) { if builds != nil { if info, ok := builds[name]; ok { // Check version. there should chek // local first because d:\ contains : if com.IsDir(info) { pkgs[name] = &doc.Pkg{ ImportPath: name, Type: doc.LOCAL, Value: info, } continue } else if i := strings.Index(info, ":"); i > -1 { pkgs[name] = &doc.Pkg{ ImportPath: name, Type: info[:i], Value: info[i+1:], } continue } } } pkgs[name] = doc.NewDefaultPkg(name) } } return pkgs, nil }
func runTest(ctx *cli.Context) { genNewGoPath(ctx, true) log.Trace("Testing...") cmdArgs := []string{"go", "test"} cmdArgs = append(cmdArgs, ctx.Args()...) err := execCmd(newGoPath, newCurPath, cmdArgs...) if err != nil { log.Error("Test", "Fail to test program") log.Fatal("", err.Error()) } log.Success("SUCC", "Test", "Command execute successfully!") }
func copyToGopath(srcPath, destPath string) { importPath := strings.TrimPrefix(destPath, installGopath+"/") if len(getVcsName(destPath)) > 0 { log.Warn("Package in GOPATH has version control: %s", importPath) return } os.RemoveAll(destPath) err := com.CopyDir(srcPath, destPath) if err != nil { log.Error("download", "Fail to copy to GOPATH:") log.Fatal("", "\t"+err.Error()) } log.Log("Package copied to GOPATH: %s", importPath) }
func LoadLocalNodes() { if !com.IsDir(HomeDir + "/data") { os.MkdirAll(HomeDir+"/data", os.ModePerm) } if !com.IsFile(HomeDir + LocalNodesFile) { os.Create(HomeDir + LocalNodesFile) } var err error LocalNodes, err = goconfig.LoadConfigFile(path.Join(HomeDir + LocalNodesFile)) if err != nil { log.Error("load node", "Fail to load localnodes.list") log.Fatal("", err.Error()) } }
func getImports(rootPath string, match map[string]string, cmdFlags map[string]bool, nod *Node) (imports []string) { dirs, err := GetDirsInfo(rootPath) if err != nil { log.Error("", "Fail to get directory's information") log.Fatal("", err.Error()) } for _, d := range dirs { if d.IsDir() && !(!cmdFlags["-e"] && strings.Contains(d.Name(), "example")) { absPath := rootPath + d.Name() + "/" importPkgs, err := CheckImports(absPath, match["importPath"], nod) if err != nil { return nil } imports = append(imports, importPkgs...) } } return imports }
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 genNewGoPath(ctx *cli.Context, isTest bool) { var err error curPath, err = os.Getwd() if err != nil { log.Error("", "Fail to get work directory:") log.Fatal("", "\t"+err.Error()) } log.Trace("Current Path: 0 %s", curPath) installRepoPath = doc.HomeDir + "/repos" if com.IsFile(curPath + "/" + doc.GOPM_FILE_NAME) { log.Trace("Loading gopmfile...") gf := doc.NewGopmfile(curPath) var err error pkgName, err = gf.GetValue("target", "path") if err == nil { log.Log("Target name: %s", pkgName) } } if len(pkgName) == 0 { _, pkgName = filepath.Split(curPath) } cachePkgs := make(map[string]*doc.Pkg) if err = getChildPkgs(ctx, curPath, nil, cachePkgs, isTest); err != nil { log.Error("", "Fail to get child pakcages:") log.Fatal("", "\t"+err.Error()) } newGoPath = filepath.Join(curPath, doc.VENDOR) newGoPathSrc := filepath.Join(newGoPath, "src") os.RemoveAll(newGoPathSrc) os.MkdirAll(newGoPathSrc, os.ModePerm) for name, pkg := range cachePkgs { suf := versionSuffix(pkg.Value) var oldPath string if pkg.Type == doc.LOCAL { oldPath, _ = filepath.Abs(pkg.Value) } else { oldPath = filepath.Join(installRepoPath, name) + suf } newPath := filepath.Join(newGoPathSrc, name) paths := strings.Split(name, "/") var isExistP, isCurChild bool if name == pkgName { continue } for i := 0; i < len(paths)-1; i++ { pName := strings.Join(paths[:len(paths)-1-i], "/") if _, ok := cachePkgs[pName]; ok { isExistP = true break } if pkgName == pName { isCurChild = true break } } if isCurChild { continue } if !isExistP && (len(pkg.Value) > 0 || ctx.Bool("remote") || !com.IsDir(filepath.Join(installGopath, pkg.ImportPath))) { log.Log("Linking %s", name+suf) err = autoLink(oldPath, newPath) if err != nil { log.Error("", "Fail to make link:") log.Fatal("", "\t"+err.Error()) } } } newCurPath = filepath.Join(newGoPathSrc, pkgName) log.Log("Linking %s", pkgName) err = autoLink(curPath, newCurPath) if err != nil { log.Error("", "Fail to make link:") log.Fatal("", "\t"+err.Error()) } }
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!") }