func execCmd(gopath, curPath string, args ...string) error { oldGopath := os.Getenv("GOPATH") log.Info("Setting GOPATH to %s", gopath) sep := ":" if runtime.GOOS == "windows" { sep = ";" } if err := os.Setenv("GOPATH", gopath+sep+oldGopath); err != nil { if setting.LibraryMode { return fmt.Errorf("Fail to setting GOPATH: %v", err) } log.Error("", "Fail to setting GOPATH:") log.Fatal("", "\t"+err.Error()) } if setting.HasGOPATHSetting { defer func() { log.Info("Setting GOPATH back to %s", oldGopath) os.Setenv("GOPATH", oldGopath) }() } cmd := exec.Command(args[0], args[1:]...) cmd.Dir = curPath cmd.Stdout = os.Stdout cmd.Stderr = os.Stderr log.Info("===== application outputs start =====\n") err := cmd.Run() log.Info("====== application outputs end ======") return err }
func main() { setting.LibraryMode = false if err := lib.Run(os.Args); err.HasError { if err.Fatal != nil { log.Fatal("%v", err.Fatal) } for _, e := range err.Errors { log.Error("%v", e) } } }
func loadLocalVerInfo() (ver version) { verPath := path.Join(setting.HomeDir, ".gopm", setting.VERINFO) // First time run should not exist. if !base.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 (t *transport) SetProxy(proxy string) error { if len(proxy) == 0 { return nil } proxyUrl, err := url.Parse(proxy) if err != nil { if setting.LibraryMode { return fmt.Errorf("Fail to set HTTP proxy: %v", err) } log.Error("", "Fail to set HTTP proxy:") log.Fatal("", "\t"+err.Error()) } t.t.Proxy = http.ProxyURL(proxyUrl) return nil }
func (n *Node) CopyToGopath() error { if n.HasVcs() { log.Warn("Package in GOPATH has version control: %s", n.RootPath) return nil } os.RemoveAll(n.InstallGopath) if err := base.CopyDir(n.InstallPath, n.InstallGopath); err != nil { if setting.LibraryMode { return fmt.Errorf("Fail to copy to GOPATH: %v", err) } log.Error("", "Fail to copy to GOPATH:") log.Fatal("", "\t"+err.Error()) } log.Info("Package copied to GOPATH: %s", n.RootPath) return nil }
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, ".gopm", 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") }