/* Search Node.js version and Print Param: - s: Node.js version, inlcude: *.*.* 0.*.* 0.10.* /<regexp>/ latest 0.10.10 */ func Search(s string) { regex, err := util.FormatWildcard(s, latURL) if err != nil { P(ERROR, "%v not an %v Node.js version.\n", s, "valid") return } // set url url := config.GetConfig(config.REGISTRY) if arr := strings.Split(s, "."); len(arr) == 3 { if ver, _ := strconv.Atoi(arr[0]); ver >= 1 && ver <= 3 { url = config.GetIOURL(url) } } url += util.NODELIST // try catch defer func() { if err := recover(); err != nil { msg := fmt.Sprintf("'%v' an error has occurred. please check your input.\nError: ", "gnvm search") Error(ERROR, msg, err) os.Exit(0) } }() // print P(DEFAULT, "Search Node.js version rules [%v] from %v, please wait.\n", s, url) // generate nodist nodist, err, code := New(url, regex) if err != nil { if code == -1 { P(ERROR, "'%v' get url %v error, Error: %v\n", "gnvm search", url, err) } else { P(ERROR, "%v an error has occurred. please check. Error: %v\n", "gnvm search", err) } return } if len(nodist.nl) > 0 { nodist.Detail(0) } else { P(WARING, "not search any Node.js version details, use rules [%v] from %v.\n", s, url) } }
/* Get npm version by global( local ) node version Return: - string: npm version */ func getNodeNpmVer() string { ver, err := util.GetNodeVer(rootPath) if err != nil { panic(errors.New("not exist global node.exe. please usage 'gnvm install latest -g' frist.")) } url := config.GetConfig(config.REGISTRY) if level := util.GetNodeVerLev(util.FormatNodeVer(ver)); level == 3 { url = config.GetIOURL(url) } url += util.NODELIST nd, err := FindNodeDetailByVer(url, ver) if err != nil { panic(err) } return nd.NPM.Version }
/* Print remote Node.js version list Param: - limit: print max line - io: when io == true, print iojs */ func LsRemote(limit int, io bool) { // set url url := config.GetConfig(config.REGISTRY) if io { url = config.GetIOURL(url) } url += util.NODELIST // try catch defer func() { if err := recover(); err != nil { msg := fmt.Sprintf("'gnvm ls --remote' an error has occurred. please check your input %v. \nError: ", url) Error(ERROR, msg, err) os.Exit(0) } }() // print P(DEFAULT, "Read all Node.js version list from %v, please wait.\n", url) // generate nodist nodist, err, code := New(url, nil) if err != nil { if code == -1 { P(ERROR, "'%v' get url %v error, Error: %v\n", "gnvm ls -r -d", url, err) } else { P(ERROR, "%v an error has occurred. please check your input. Error: %v\n", "gnvm ls -r -d", err) } return } if limit != -1 { nodist.Detail(limit) } else { for _, v := range nodist.Sorts { fmt.Println(v) } } }
/* Install node Param: - args : install Node.js versions, include: x.xx.xx latest x.xx.xx-io-x86 x.xx.xx-x86 - global: when global == true, call Use func. Return: - code: dl[0].Code, usage 'gnvm update latest' */ func InstallNode(args []string, global bool) int { localVersion, isLatest, code, dl, ts := "", false, 0, new(curl.Download), new(curl.Task) // try catch defer func() { if err := recover(); err != nil { if strings.HasPrefix(err.(string), "CURL Error:") { fmt.Printf("\n") } msg := fmt.Sprintf("'gnvm install %v' an error has occurred. \nError: ", strings.Join(args, " ")) Error(ERROR, msg, err) os.Exit(0) } }() for _, v := range args { ver, io, arch, suffix, err := util.ParseNodeVer(v) if err != nil { switch err.Error() { case "1": P(ERROR, "%v not node.exe download.\n", v) case "2": P(ERROR, "%v format error, suffix only must be '%v' or '%v'.\n", v, "x86", "x64") case "3": P(ERROR, "%v format error, parameter must be '%v' or '%v'.\n", v, "x.xx.xx", "x.xx.xx-x86|x64") case "4": P(ERROR, "%v not an %v Node.js version.\n", v, "valid") case "5": P(WARING, "'%v' command is no longer supported. See '%v'.\n", "gnvm install npm", "gnvm help npm") } continue } // when os is 386, not download 64 bit node.exe if runtime.GOARCH == "386" && suffix == "x64" { P(WARING, "current operating system is %v, not support %v suffix.\n", "32-bit", "x64") continue } // check local latest and get remote latest v = util.EqualAbs("latest", v) if ver == util.LATEST { localVersion = config.GetConfig(config.LATEST_VERSION) P(NOTICE, "local latest version is %v.\n", localVersion) version := util.GetLatVer(latURL) if version == "" { P(ERROR, "get latest version error, please check. See '%v'.\n", "gnvm config help") break } isLatest = true ver = version P(NOTICE, "remote latest version is %v.\n", version) } else { isLatest = false } // ture folder name if suffix != "" { ver += "-" + suffix } // verify <root>/folder is exist folder := rootPath + ver if _, err := util.GetNodeVer(folder); err == nil { P(WARING, "%v folder exist.\n", ver) continue } // get and set url( include iojs) url := config.GetConfig(config.REGISTRY) if io { url = config.GetIOURL(url) } // add task if url, err := util.GetRemoteNodePath(url, ver, arch); err == nil { dl.AddTask(ts.New(url, ver, util.NODE, folder)) } } // downlaod if len(*dl) > 0 { curl.Options.Header = false arr := (*dl).GetValues("Title") P(DEFAULT, "Start download Node.js versions [%v].\n", strings.Join(arr, ", ")) newDL, errs := curl.New(*dl) for _, task := range newDL { v := strings.Replace(task.Dst, rootPath, "", -1) if v != localVersion && isLatest { config.SetConfig(config.LATEST_VERSION, v) P(DEFAULT, "Set success, %v new value is %v\n", config.LATEST_VERSION, v) } if global && len(args) == 1 { if ok := Use(v); ok { config.SetConfig(config.GLOBAL_VERSION, v) } } } if len(errs) > 0 { code = (*dl)[0].Code s := "" for _, v := range errs { s += v.Error() } P(WARING, s) } } return code }