예제 #1
0
파일: config.go 프로젝트: Kenshin/gnvm
/*
 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)
}
예제 #2
0
파일: npm.go 프로젝트: Kenshin/gnvm
/*
 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
}
예제 #3
0
파일: session.go 프로젝트: Kenshin/gnvm
/*
 Regedit

 Param:
 	- action: olny support 'start' and 'close'

*/
func Run(action string) {

	// try catch
	defer func() {
		if err := recover(); err != nil {
			msg := fmt.Sprintf("'gnvm session' an error has occurred. please check. \nError: ")
			Error(ERROR, msg, err)
			os.Exit(0)
		}
	}()

	if _, err := util.GetNodeVer(util.GlobalNodePath); err != nil {
		P(ERROR, "not found %v node.exe, not use %v. please use '%v'. See '%v'.\n", "global", "gnvm session "+action, "gnvm install x.xx.xx -g", "gnvm help install")
		return
	}

	if action == "start" {
		start()
	} else {
		close()
	}
}
예제 #4
0
파일: config.go 프로젝트: Kenshin/gnvm
/*
 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)
	}
}
예제 #5
0
파일: nodehandle.go 프로젝트: Kenshin/gnvm
/*
 Show local / global Node.js version

 Param:
 	- args:   include: latest global

*/
func NodeVersion(args []string) {

	// try catch
	defer func() {
		if err := recover(); err != nil {
			msg := fmt.Sprintf("'gnvm node-version %v' an error has occurred. please check. \nError: ", strings.Join(args, " "))
			Error(ERROR, msg, err)
			os.Exit(0)
		}
	}()

	/*
		if len(args) == 0 {
			P(DEFAULT, "Node.js %v version is %v.\n", "latest", latest)
			P(DEFAULT, "Node.js %v version is %v.\n", "global", global)
			if latest == util.UNKNOWN {
				P(WARING, "latest version is %v, please use '%v'. See '%v'.\n", util.UNKNOWN, "gnvm node-version latest -r", "gnvm help node-version")
			}
			if global == util.UNKNOWN {
				P(WARING, "global version is %v, please use '%v' or '%v'. See '%v'.\n", util.UNKNOWN, "gnvm install latest -g", "gnvm install x.xx.xx -g", "gnvm help install")
			}
		} else {
			switch {
			case args[0] == "global":
				P(DEFAULT, "Node.js global version is %v.\n", global)
				if global == util.UNKNOWN {
					P(WARING, "global version is %v, please use '%v' or '%v'. See '%v'.\n", util.UNKNOWN, "gnvm install latest -g", "gnvm install x.xx.xx -g", "gnvm help install")
				}
			case args[0] == "latest" && !remote:
				P(DEFAULT, "Node.js latest version is %v.\n", latest)
				if latest == util.UNKNOWN {
					P(WARING, "latest version is %v, please use '%v'. See '%v'.\n", util.UNKNOWN, "gnvm node-version latest -r", "gnvm help node-version")
				}
			case args[0] == "latest" && remote:
				remoteVersion := util.GetLatVer(latURL)
				if remoteVersion == "" {
					P(ERROR, "get remote %v Node.js %v error, please check your input. See '%v'.\n", config.GetConfig(config.REGISTRY), "latest version", "gnvm help config")
					return
				}
				P(DEFAULT, "Local  Node.js latest version is %v.\n", latest)
				P(DEFAULT, "Remote Node.js latest version is %v from %v.\n", remoteVersion, config.GetConfig(config.REGISTRY))
				if latest == util.UNKNOWN {
					config.SetConfig(config.LATEST_VERSION, remoteVersion)
					P(DEFAULT, "Set success, local Node.js %v version is %v.\n", util.LATEST, remoteVersion)
					return
				}
				v1 := util.FormatNodeVer(latest)
				v2 := util.FormatNodeVer(remoteVersion)
				if v1 < v2 {
					cp := CP{Red, false, None, false, ">"}
					P(WARING, "remote Node.js latest version %v %v local Node.js latest version %v, suggest to upgrade, usage '%v'.\n", remoteVersion, cp, latest, "gnvm update latest")
				}
			}
		}
	*/

	isLatest, isGlobal := false, false
	latest, global := config.GetConfig(config.LATEST_VERSION), config.GetConfig(config.GLOBAL_VERSION)
	if len(args) == 0 {
		isLatest = true
		isGlobal = true
	} else {
		if args[0] == util.LATEST {
			isLatest = true
		} else {
			isGlobal = true
		}
	}

	if isGlobal {
		if global == util.UNKNOWN {
			P(WARING, "global Node.js version is %v.\n", util.UNKNOWN)
			if global, err := util.GetNodeVer(rootPath); err == nil {
				config.SetConfig(config.GLOBAL_VERSION, global)
				P(DEFAULT, "Set success, %v new value is %v.\n", config.GLOBAL_VERSION, global)
			} else {
				P(WARING, "global Node.js version is %v, please use %v or %v. See '%v'.\n", util.UNKNOWN, "gnvm install latest -g", "gnvm install x.xx.xx -g", "gnvm help install")
			}
		} else {
			P(DEFAULT, "Node.js %v version is %v.\n", "global", global)
		}
	}

	if isLatest {
		if latest == util.UNKNOWN {
			P(WARING, "latest Node.js version is %v, please use %v or %v. See '%v'.\n", util.UNKNOWN, "gnvm install latest -g", "gnvm update latest", "gnvm help node-version")
		} else {
			P(DEFAULT, "Node.js %v version is %v.\n", "latest", latest)
		}
		remoteVersion := util.GetLatVer(latURL)
		if remoteVersion == "" {
			P(ERROR, "get remote %v Node.js %v error, please check your input. See '%v'.\n", config.GetConfig(config.REGISTRY), "latest version", "gnvm help config")
			return
		}
		if latest == util.UNKNOWN {
			P(NOTICE, "remote Node.js %v version is %v from %v.\n", "latest", remoteVersion, config.GetConfig(config.REGISTRY))
			//config.SetConfig(config.LATEST_VERSION, remoteVersion)
			//P(DEFAULT, "Set success, local Node.js %v version is %v.\n", util.LATEST, remoteVersion)
			return
		}
		v1 := util.FormatNodeVer(latest)
		v2 := util.FormatNodeVer(remoteVersion)
		if v1 < v2 {
			cp := CP{Red, false, None, false, ">"}
			P(WARING, "remote Node.js latest version %v %v local Node.js latest version %v, suggest to upgrade, usage '%v'.\n", remoteVersion, cp, latest, "gnvm update latest")
		}
	}
}
예제 #6
0
파일: nodehandle.go 프로젝트: Kenshin/gnvm
/**
 * 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
}
예제 #7
0
파일: nodehandle.go 프로젝트: Kenshin/gnvm
/*
 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
}