func createConfig() { // create file file, err := os.Create(configPath) defer file.Close() if err != nil { P(ERROR, "config file create Error: %v\n", err.Error()) return } // get <root>/node.exe version version, err := util.GetNodeVersion(util.GlobalNodePath + "\\") if err != nil { P(WARING, "not found global node.exe version, please use '%v'. See '%v'.\n", "gnvm install x.xx.xx -g", "gnvm help install") globalversion = GLOBAL_VERSION_VAL } else { globalversion = version } //write init config _, fileErr := file.WriteString(REGISTRY_KEY + REGISTRY_VAL + NEWLINE + NODEROOT_KEY + util.GlobalNodePath + NEWLINE + GLOBAL_VERSION_KEY + globalversion + NEWLINE + LATEST_VERSION_KEY + LATEST_VERSION_VAL) if fileErr != nil { P(ERROR, "write config file Error: %v\n", fileErr.Error()) return } P(DEFAULT, "Config file %v create success.\n", configPath) P(NOTICE, "latest version is %v, please use '%v'.\n", UNKNOWN, "gnvm config init") }
/* * return code * 0: success * 1: remove folder error * 2: folder exist * 3: create folder error * */ func download(version string) int { // get current os arch amd64 := "/" if runtime.GOARCH == "amd64" { amd64 = "/x64/" } // rootPath/version/node.exe is exist if _, err := util.GetNodeVersion(rootPath + version + DIVIDE); err == nil { P(WARING, "%v folder exist.\n", version) return 2 } else { if err := os.RemoveAll(rootPath + version); err != nil { P(ERROR, "remove %v fail, Error: %v\n", version, err.Error()) return 1 } //P(DEFAULT, "Remove empty [%v] folder success.\n", version) } // rootPath/version is exist if isDirExist(rootPath+version) != true { if err := os.Mkdir(rootPath+version, 0777); err != nil { P(ERROR, "create %v fail, Error: %v\n", version, err.Error()) return 3 } } // set url url := config.GetConfig(config.REGISTRY) + "v" + version + amd64 + NODE // download if code := curl.New(url, version, rootPath+version+DIVIDE+NODE); code != 0 { if code == -1 { if err := os.RemoveAll(rootPath + version); err != nil { P(ERROR, "remove %v fail, Error: %v\n", version, err.Error()) return 1 } } return code } return 0 }
func ReSetConfig() { SetConfig(REGISTRY, REGISTRY_VAL) SetConfig(NODEROOT, util.GlobalNodePath) version, err := util.GetNodeVersion(util.GlobalNodePath + "\\") if err != nil { P(WARING, "not found global node.exe version, please use '%v'. See '%v'.\n", "gnvm install x.xx.xx -g", "gnvm help install") globalversion = GLOBAL_VERSION_VAL } else { globalversion = version } SetConfig(GLOBAL_VERSION, globalversion) // set url url := REGISTRY_VAL + "latest/" + util.SHASUMS if latest := util.GetLatestVersion(url); latest != "" { latsetversion = latest } else { latsetversion = LATEST_VERSION_VAL } SetConfig(LATEST_VERSION, latsetversion) P(DEFAULT, "Config file %v init success.\n", configPath) }
/** * rootNode is rootPath + "/node.exe", e.g. <root>/node.exe * * usePath is use node version path, e.g. <root>/x.xx.xx * useNode is usePath + "/node.exe", e.g. <root>/x.xx.xx/node.exe * * rootVersion is <root>/node.exe version * rootFolder is <root>/rootVersion */ func Use(folder string) bool { // try catch defer func() { if err := recover(); err != nil { msg := fmt.Sprintf("'gnvm use %v' an error has occurred. please check. \nError: ", folder) Error(ERROR, msg, err) os.Exit(0) } }() rootNodeExist := true // get true folder, e.g. folder is latest return x.xx.xx folder = TransLatestVersion(folder, true) if folder == config.UNKNOWN { P(ERROR, "unassigned node.exe latest version, use %v. See '%v'.\n", "gnvm config INIT", "gnvm help config") return false } // set rootNode rootNode := rootPath + NODE // set usePath and useNode usePath := rootPath + folder + DIVIDE useNode := usePath + NODE // get <root>/node.exe version rootVersion, err := util.GetNodeVersion(rootPath) if err != nil { P(WARING, "not found global node.exe version.\n") rootNodeExist = false } // <root>/folder is exist if isDirExist(usePath) != true { P(WARING, "%v folder is not exist from %v, use '%v' get local node.exe list. See '%v'.\n", folder, rootPath, "gnvm ls", "gnvm help ls") return false } // check folder is rootVersion if folder == rootVersion { P(WARING, "current node.exe version is %v, not re-use. See 'gnvm node-version'.\n", folder) return false } // set rootFolder rootFolder := rootPath + rootVersion // <root>/rootVersion is exist if isDirExist(rootFolder) != true { // create rootVersion folder if err := os.Mkdir(rootFolder, 0777); err != nil { P(ERROR, "create %v folder Error: %v.\n", rootVersion, err.Error()) return false } } if rootNodeExist { // copy rootNode to <root>/rootVersion if err := copy(rootNode, rootFolder); err != nil { P(ERROR, "copy %v to %v folder Error: %v.\n", rootNode, rootFolder, err.Error()) return false } // delete <root>/node.exe /*if err := os.Remove(rootNode); err != nil { P(ERROR, "remove %v folder Error: %v.\n", rootNode, err.Error()) return false }*/ } // copy useNode to rootPath if err := copy(useNode, rootPath); err != nil { P(ERROR, "copy %v to %v folder Error: %v.\n", useNode, rootPath, err.Error()) return false } P(DEFAULT, "Set success, global node.exe version is %v.\n", folder) return true }