// downloadPackage downloads package either use version control tools or not. func downloadPackage(ctx *cli.Context, n *doc.Node) (*doc.Node, []string, error) { // fmt.Println(n.VerString()) log.Info("Downloading package: %s", n.VerString()) downloadCache.Set(n.VerString()) vendor := base.GetTempDir() defer os.RemoveAll(vendor) var ( err error imports []string srcPath string ) // Check if only need to use VCS tools. vcs := doc.GetVcsName(n.InstallGopath) // If update, gopath and VCS tools set then use VCS tools to update the package. if ctx.Bool("update") && (ctx.Bool("gopath") || ctx.Bool("local")) && len(vcs) > 0 { if err = n.UpdateByVcs(vcs); err != nil { return nil, nil, fmt.Errorf("fail to update by VCS(%s): %v", n.ImportPath, err) } srcPath = n.InstallGopath } else { if !n.IsGetDepsOnly || !n.IsExist() { // Get revision value from local records. n.Revision = setting.LocalNodes.MustValue(n.RootPath, "value") if err = n.DownloadGopm(ctx); err != nil { errors.AppendError(errors.NewErrDownload(n.ImportPath + ": " + err.Error())) failConut++ os.RemoveAll(n.InstallPath) return nil, nil, nil } } srcPath = n.InstallPath } if n.IsGetDeps { imports, err = getDepList(ctx, n.ImportPath, srcPath, vendor) if err != nil { return nil, nil, fmt.Errorf("fail to list imports(%s): %v", n.ImportPath, err) } if setting.Debug { log.Debug("New imports: %v", imports) } } return n, imports, err }
func runUpdate(ctx *cli.Context) { if setting.LibraryMode { errors.SetError(fmt.Errorf("Library mode does not support update command")) return } if err := setup(ctx); err != nil { errors.SetError(err) return } isAnythingUpdated := false localVerInfo := loadLocalVerInfo() // Get remote version info. var remoteVerInfo version if err := base.HttpGetJSON(doc.HttpClient, "http://gopm.io/VERSION.json", &remoteVerInfo); err != nil { log.Fatal("Fail to fetch VERSION.json: %v", err) } // Package name list. if remoteVerInfo.PackageNameList > localVerInfo.PackageNameList { log.Info("Updating pkgname.list...%v > %v", localVerInfo.PackageNameList, remoteVerInfo.PackageNameList) data, err := base.HttpGetBytes(doc.HttpClient, "https://raw.githubusercontent.com/gpmgo/docs/master/pkgname.list", nil) if err != nil { log.Warn("Fail to update pkgname.list: %v", err) } else { if err = ioutil.WriteFile(setting.PkgNameListFile, data, os.ModePerm); err != nil { log.Fatal("Fail to save pkgname.list: %v", err) } log.Info("Update pkgname.list to %v succeed!", remoteVerInfo.PackageNameList) isAnythingUpdated = true } } // Gopm. if remoteVerInfo.Gopm > setting.VERSION { log.Info("Updating gopm...%v > %v", setting.VERSION, remoteVerInfo.Gopm) tmpDir := base.GetTempDir() tmpBinPath := path.Join(tmpDir, "gopm") if runtime.GOOS == "windows" { tmpBinPath += ".exe" } os.MkdirAll(path.Dir(tmpBinPath), os.ModePerm) os.Remove(tmpBinPath) // Fetch code. args := []string{"bin", "-u", "-r", "-d=" + tmpDir} if ctx.Bool("verbose") { args = append(args, "-v") } args = append(args, "github.com/gpmgo/gopm") stdout, stderr, err := base.ExecCmd("gopm", args...) if err != nil { log.Fatal("Fail to execute 'bin -u -r -d=/Users/jiahuachen/.gopm/temp -v github.com/gpmgo/gopm: %s", stderr) } if len(stdout) > 0 { fmt.Print(stdout) } // Check if previous steps were successful. if !base.IsExist(tmpBinPath) { log.Error("Fail to continue command") log.Fatal("Previous steps weren't successful, no binary produced") } movePath, err := execPath() if err != nil { log.Fatal("Fail to get execute path: %v", err) } log.Info("New binary will be replaced for %s", movePath) // Move binary to given directory. if runtime.GOOS != "windows" { err := os.Rename(tmpBinPath, movePath) if err != nil { log.Fatal("Fail to move binary: %v", err) } os.Chmod(movePath+"/"+path.Base(tmpBinPath), os.ModePerm) } else { batPath := path.Join(tmpDir, "update.bat") f, err := os.Create(batPath) if err != nil { log.Error("Update", "Fail to generate bat file") log.Fatal("", err.Error()) } f.WriteString("@echo off\r\n") f.WriteString(fmt.Sprintf("ping -n 1 127.0.0.1>nul\r\ncopy \"%v\" \"%v\" >nul\r\ndel \"%v\" >nul\r\n\r\n", tmpBinPath, movePath, tmpBinPath)) f.Close() attr := &os.ProcAttr{ Dir: setting.WorkDir, Env: os.Environ(), Files: []*os.File{os.Stdin, os.Stdout, os.Stderr}, } if _, err = os.StartProcess(batPath, []string{batPath}, attr); err != nil { log.Error("Update", "Fail to start bat process") log.Fatal("", err.Error()) } } log.Info("Command execute successfully!") isAnythingUpdated = true } if isAnythingUpdated { // Save JSON. verPath := path.Join(setting.HomeDir, setting.VERINFO) os.MkdirAll(path.Dir(verPath), os.ModePerm) f, err := os.Create(verPath) if err != nil { log.Fatal("Fail to create VERSION.json: %v", err) } if err := json.NewEncoder(f).Encode(&remoteVerInfo); err != nil { log.Fatal("Fail to encode VERSION.json: %v", err) } } else { log.Info("Nothing need to be updated") } log.Info("Exit old gopm") }