/* Get remote Node.js latest version from url Param: - url: remote Node.js url, e.g. http://npm.taobao.org/mirrors/node Return: - latest: remote Node.js latest version */ func GetLatVer(url string) string { var version string // curl code, res, _ := curl.Get(url) if code != 0 { return "" } // close defer res.Body.Close() latestVersion := func(content string, line int) bool { if content != "" && line == 1 { reg, _ := regexp.Compile(`([0]|[1-9]\d?)(\.([0]|[1-9]\d?)){2}`) version = reg.FindString(content) } return false } if err := curl.ReadLine(res.Body, latestVersion); err != nil && err != io.EOF { P(ERROR, "%v Error: %v\n", "gnvm update latest", err) } return version }
func LsRemote() { // set url url := config.GetConfig(config.REGISTRY) + config.NODELIST // try catch defer func() { if err := recover(); err != nil { msg := fmt.Sprintf("'gnvm ls --remote' an error has occurred. please check registry %v. \nError: ", url) Error(ERROR, msg, err) os.Exit(0) } }() // set exist version isExistVersion := false // print P(DEFAULT, "Read all node.exe version list from %v, please wait.\n", url) // get code, res, _ := curl.Get(url) if code != 0 { return } // close defer res.Body.Close() writeVersion := func(content string, line int) bool { // replace '\n' content = strings.Replace(content, "\n", "", -1) // split 'vx.xx.xx 1.1.0-alpha-2' args := strings.Split(content, " ") if ok := util.VerifyNodeVersion(args[0][1:]); ok { isExistVersion = true P(DEFAULT, args[0], "\n") } return false } if err := curl.ReadLine(res.Body, writeVersion); err != nil && err != io.EOF { P(ERROR, "%v Error: %v\n", "gnvm ls --remote", err) } }
func GetLatestVersion(url string) string { var version string // curl code, res, _ := curl.Get(url) if code != 0 { return "" } // close defer res.Body.Close() latestVersion := func(content string, line int) bool { if content != "" && line == 1 { args1 := strings.Split(content, " ") if len(args1) < 2 { P(ERROR, "URL %v format error, please change registry. See '%v'.\n", url, "gnvm help config") return true } args2 := strings.Split(args1[1], "-") if len(args2) < 2 { P(ERROR, "URL %v format error, please change registry. See '%v'.\n", url, "gnvm help config") return true } if len(args2[1]) < 2 { P(ERROR, "URL %v format error, please change registry. See '%v'.\n", url, "gnvm help config") return true } version = args2[1][1:] } return false } if err := curl.ReadLine(res.Body, latestVersion); err != nil && err != io.EOF { P(ERROR, "%v Error: %v\n", "gnvm update latest", err) } return version }
func Version(remote bool) { // try catch defer func() { if err := recover(); err != nil { Error(ERROR, "'gnvm version --remote' an error has occurred. \nError: ", err) os.Exit(0) } }() localVersion := config.VERSION cp := CP{Red, true, None, true, "Kenshin Wang"} P(DEFAULT, "Current version %v", localVersion, "\n") P(DEFAULT, "Copyright (C) 2014 %v <*****@*****.**>", cp, "\n") cp.FgColor, cp.Value = Blue, "https://github.com/kenshin/gnvm" P(DEFAULT, "See %v for more information.", cp, "\n") if !remote { return } code, res, _ := curl.Get(GNVMHOST) if code != 0 { return } defer res.Body.Close() versionFunc := func(content string, line int) bool { if content != "" && line == 1 { arr := strings.Fields(content) if len(arr) == 2 { cp := CP{Red, true, None, true, arr[0][1:]} P(DEFAULT, "Latest version %v, publish data %v", cp, arr[1], "\n") latestVersion, msg := arr[0][1:], "" localArr, latestArr := strings.Split(localVersion, "."), strings.Split(latestVersion, ".") switch { case latestArr[0] > localArr[0]: msg = "must be upgraded." case latestArr[1] > localArr[1]: msg = "suggest to upgrade." case latestArr[2] > localArr[2]: msg = "optional upgrade." } if msg != "" { P(NOTICE, msg+" Please download latest %v from %v", "gnvm.exe", "https://github.com/kenshin/gnvm", "\n") } } } if line > 1 { P(DEFAULT, content) } return false } if err := curl.ReadLine(res.Body, versionFunc); err != nil && err != io.EOF { P(ERROR, "gnvm version --remote Error: %v\n", err) } }
func InstallNpm() { // try catch defer func() { if err := recover(); err != nil { if strings.HasPrefix(err.(string), "CURL Error:") { fmt.Printf("\n") } Error(ERROR, "'gnvm install npm' an error has occurred. \nError: ", err) os.Exit(0) } }() out, err := exec.Command(rootPath+"npm", "--version").Output() if err == nil { P(WARING, "current path %v exist npm, version is %v", rootPath, string(out[:]), "\n") return } url := config.GetConfig(config.REGISTRY) + "npm" // get code, res, _ := curl.Get(url) if code != 0 { return } // close defer res.Body.Close() maxTime, _ := time.Parse(TIMEFORMART, TIMEFORMART) var maxVersion string getNpmVersion := func(content string, line int) bool { if strings.Index(content, `<a href="`) == 0 && strings.Contains(content, ".zip") { // parse newLine := strings.Replace(content, `<a href="`, "", -1) newLine = strings.Replace(newLine, `</a`, "", -1) newLine = strings.Replace(newLine, `">`, " ", -1) // e.g. npm-1.3.9.zip npm-1.3.9.zip> 23-Aug-2013 21:14 1535885 orgArr := strings.Fields(newLine) // e.g. npm-1.3.9.zip version := orgArr[0:1][0] // e.g. 23-Aug-2013 21:14 sTime := strings.Join(orgArr[2:len(orgArr)-1], " ") // bubble sort if t, err := time.Parse(TIMEFORMART, sTime); err == nil { if t.Sub(maxTime).Seconds() > 0 { maxTime = t maxVersion = version } } } return false } if err := curl.ReadLine(res.Body, getNpmVersion); err != nil && err != io.EOF { P(ERROR, "parse npm version Error: %v, from %v\n", err, url) return } if maxVersion == "" { P(ERROR, "get npm version fail from %v, please check. See '%v'.\n", url, "gnvm help config") return } P(NOTICE, "the latest version is %v from %v.\n", maxVersion, config.GetConfig(config.REGISTRY)) // download zip zipPath := os.TempDir() + DIVIDE + maxVersion if code := downloadNpm(maxVersion); code == 0 { P(DEFAULT, "Start unarchive file %v.\n", maxVersion) //unzip(maxVersion) if err := zip.UnarchiveFile(zipPath, config.GetConfig(config.NODEROOT), nil); err != nil { panic(err) } P(DEFAULT, "End unarchive.\n") } // remove temp zip file if err := os.RemoveAll(zipPath); err != nil { P(ERROR, "remove zip file fail from %v, Error: %v.\n", zipPath, err.Error()) } }
/* Print gnvm.exe version Param: - remote: when remote == true, print CHANGELOG */ func Version(remote, detail bool) { defer func() { if err := recover(); err != nil { msg := fmt.Sprintf("'%v' an error has occurred. please check. \nError: ", "gnvm version -r") Error(ERROR, msg, err) os.Exit(0) } }() localVersion, arch := config.VERSION, "32 bit" if runtime.GOARCH == "amd64" { arch = "64 bit" } cp := CP{Red, true, None, true, "Kenshin Wang"} P(DEFAULT, "Current version %v %v.", localVersion, arch, "\n") P(DEFAULT, "Copyright (C) 2014-2016 %v <*****@*****.**>", cp, "\n") cp.FgColor, cp.Value = Blue, "https://github.com/kenshin/gnvm" P(DEFAULT, "See %v for more information.", cp, "\n") if !remote { return } code, res, err := curl.Get("http://ksria.com/gnvm/CHANGELOG.md") if code != 0 { panic(err) } defer res.Body.Close() versionFunc := func(content string, line int) bool { if content != "" && line == 1 { arr := strings.Fields(content) if len(arr) == 2 { cp := CP{Red, true, None, true, arr[0][1:]} P(DEFAULT, "Latest version %v, publish data %v", cp, arr[1], "\n") latestVersion, msg := arr[0][1:], "" localArr, latestArr := strings.Split(localVersion, "."), strings.Split(latestVersion, ".") switch { case latestArr[0] > localArr[0]: msg = "must be upgraded." case latestArr[1] > localArr[1]: msg = "suggest to upgrade." case latestArr[2] > localArr[2]: msg = "optional upgrade." } if msg != "" { P(NOTICE, msg+" Please download latest %v from %v", "gnvm.exe", "https://github.com/kenshin/gnvm", "\n") } } } if line > 2 && detail { P(DEFAULT, content) } return false } if err := curl.ReadLine(res.Body, versionFunc); err != nil && err != io.EOF { panic(err) } }