/* Create .gnvmrc file */ 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.GetNodeVer(util.GlobalNodePath) if err != nil { P(WARING, "not found %v node.exe, please use '%v'. See '%v'.\n", "global", "gnvm install x.xx.xx -g", "gnvm help install") globalversion = GLOBAL_VERSION_VAL } else { globalversion = version // add suffix if runtime.GOARCH == "amd64" { if bit, err := util.Arch(util.GlobalNodePath); err == nil && bit == "x86" { globalversion += "-" + bit } } } //write init config _, fileErr := file.WriteString(REGISTRY_KEY + util.ORIGIN_DEFAULT + 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(NOTICE, "Config file %v create success.\n", configPath) }
/* Init config property value from .gnvmrc file */ func ReSetConfig() { if newValue := SetConfig(REGISTRY, util.ORIGIN_DEFAULT); newValue != "" { P(NOTICE, "%v init success, new value is %v\n", REGISTRY, newValue) } if newValue := SetConfig(NODEROOT, util.GlobalNodePath); newValue != "" { P(NOTICE, "%v init success, new value is %v\n", NODEROOT, newValue) } version, err := util.GetNodeVer(util.GlobalNodePath) if err != nil { P(WARING, "not found %v node.exe, please use '%v'. See '%v'.\n", "global", "gnvm install x.xx.xx -g", "gnvm help install") globalversion = GLOBAL_VERSION_VAL } else { globalversion = version // add suffix if runtime.GOARCH == "amd64" { if bit, err := util.Arch(util.GlobalNodePath); err == nil && bit == "x86" { globalversion += "-" + bit } } } if newValue := SetConfig(GLOBAL_VERSION, globalversion); newValue != "" { P(NOTICE, "%v init success, new value is %v\n", GLOBAL_VERSION, newValue) } }
/** * rootPath : node.exe global path, e.g. x:\xxx\xx\xx\ * * global : global node.exe version num, e.g. x.xx.xx-x86 ( only rumtime.GOARCH == "amd64", suffix include: 'x86' and 'x64' ) * globalPath : global node.exe version path, e.g. x:\xxx\xx\xx\x.xx.xx-x86 * * newer : newer node.exe version num, e.g. x.xx.xx * newerPath : newer node.exe version path, e.g. <rootPath>\x.xx.xx\ * */ func Use(newer string) bool { // try catch defer func() { if err := recover(); err != nil { msg := fmt.Sprintf("'gnvm use %v' an error has occurred. please check. \nError: ", newer) Error(ERROR, msg, err) os.Exit(0) } }() // get true folder, e.g. folder is latest return x.xx.xx util.FormatLatVer(&newer, config.GetConfig(config.LATEST_VERSION), true) if newer == util.UNKNOWN { P(WARING, "current latest is %v, please usage '%v' first. See '%v'.\n", newer, "gnvm update latest", "gnvm help update") return false } // set newerPath and verify newerPath is exist? newerPath := rootPath + newer if _, err := util.GetNodeVer(newerPath); err != nil { P(WARING, "%v folder is not exist %v, use '%v' get local Node.js version list. See '%v'.\n", newer, "node.exe", "gnvm ls", "gnvm help ls") return false } // get <root>/node.exe version, when exist, get full version, e.g. x.xx.xx-x86 global, err := util.GetNodeVer(rootPath) if err != nil { P(WARING, "not found %v Node.js version.\n", "global") } else { if bit, err := util.Arch(rootPath); err == nil { if bit == "x86" && runtime.GOARCH == "amd64" { global += "-" + bit } } } // check newer is global if newer == global { P(WARING, "current Node.js version is %v, not re-use. See '%v'.\n", newer, "gnvm node-version") return false } // set globalPath globalPath := rootPath + global // <root>/global is exist? when not exist, create global folder if !util.IsDirExist(globalPath) { if err := os.Mkdir(globalPath, 0777); err != nil { P(ERROR, "create %v folder Error: %v.\n", global, err.Error()) return false } } // backup copy <root>/node.exe to <root>/global/node.exe if global != "" { if err := util.Copy(rootPath, globalPath, util.NODE); err != nil { P(ERROR, "copy %v to %v folder Error: %v.\n", rootPath, globalPath, err.Error()) return false } } // copy <root>/newer/node.exe to <root>/node.exe if err := util.Copy(newerPath, rootPath, util.NODE); err != nil { P(ERROR, "copy %v to %v folder Error: %v.\n", newerPath, rootPath, err.Error()) return false } P(DEFAULT, "Set success, global Node.js version is %v.\n", newer) return true }
func testArch() { bit32, _ := util.Arch(`C:\Users\Kenshin\Documents\DevTools\nodejs\5.1.1-x86\node.exe`) fmt.Println(bit32) bit64, _ := util.Arch(`C:\Users\Kenshin\Documents\DevTools\nodejs\5.1.1\node.exe`) fmt.Println(bit64) }