Ejemplo n.º 1
0
/*
 Update local Node.js latest verion

 - localVersion, remoteVersion: string  Node.js version
 - local, remote:               float64 Node.js version

 Param:
 	- global: when global == true, call Use func.

*/
func Update(global bool) {

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

	localVersion, remoteVersion := config.GetConfig(config.LATEST_VERSION), util.GetLatVer(latURL)

	P(NOTICE, "local  Node.js latest version is %v.\n", localVersion)
	if remoteVersion == "" {
		P(ERROR, "get latest version error, please check. See '%v'.\n", "gnvm help config")
		return
	}
	P(NOTICE, "remote Node.js latest version is %v from %v.\n", remoteVersion, config.GetConfig("registry"))

	local, remote, args := util.FormatNodeVer(localVersion), util.FormatNodeVer(remoteVersion), []string{remoteVersion}

	switch {
	case localVersion == util.UNKNOWN:
		if code := InstallNode(args, global); code == 0 {
			config.SetConfig(config.LATEST_VERSION, remoteVersion)
			P(DEFAULT, "Update Node.js latest success, current latest version is %v.\n", remoteVersion)
		}
	case local == remote:
		if util.IsDirExist(rootPath + localVersion) {
			cp := CP{Red, false, None, false, "="}
			P(DEFAULT, "Remote latest version %v %v latest version %v, don't need to upgrade.\n", remoteVersion, cp, localVersion)
			if global {
				if ok := Use(localVersion); ok {
					config.SetConfig(config.GLOBAL_VERSION, localVersion)
				}
			}
		} else {
			P(WARING, "%v folder is not exist. See '%v'.\n", localVersion, "gnvm ls")
			if code := InstallNode(args, global); code == 0 {
				P(DEFAULT, "Local Node.js latest version is %v.\n", localVersion)
			}
		}
	case local > remote:
		cp := CP{Red, false, None, false, ">"}
		P(WARING, "local latest version %v %v remote latest version %v.\nPlease check your config %v. See '%v'.\n", localVersion, cp, remoteVersion, "registry", "gnvm help config")
	case local < remote:
		cp := CP{Red, false, None, false, ">"}
		P(WARING, "remote latest version %v %v local latest version %v.\n", remoteVersion, cp, localVersion)
		if code := InstallNode(args, global); code == 0 {
			config.SetConfig(config.LATEST_VERSION, remoteVersion)
			P(DEFAULT, "Update success, Node.js latest version is %v.\n", remoteVersion)
		}
	}
}
Ejemplo n.º 2
0
/*
 * return code same as download return code
 */
func Install(args []string, global bool) int {

	var currentLatest string
	var code int

	// 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 {

		if v == config.LATEST {

			version := getLatestVersionByRemote()
			if version == "" {
				P(ERROR, "get latest version error, please check. See '%v'.\n", "gnvm config help")
				break
			}

			// set v
			v = version
			currentLatest = version
			P(NOTICE, "current latest version is %v.\n", version)
		}

		// downlaod
		code = download(v)
		if code == 0 || code == 2 {

			if v == currentLatest {
				config.SetConfig(config.LATEST_VERSION, v)
			}

			if global && len(args) == 1 {
				if ok := Use(v); ok {
					config.SetConfig(config.GLOBAL_VERSION, v)
				}
			}
		}
	}

	return code

}
Ejemplo n.º 3
0
func Update(global bool) {

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

	localVersion := config.GetConfig(config.LATEST_VERSION)
	P(NOTICE, "local latest version is %v.\n", localVersion)

	remoteVersion := getLatestVersionByRemote()
	if remoteVersion == "" {
		P(ERROR, "get latest version error, please check. See '%v'.\n", "gnvm help config")
		return
	}
	P(NOTICE, "remote %v latest version is %v.\n", config.GetConfig("registry"), remoteVersion)

	local, _ := util.ConverFloat(localVersion)
	remote, _ := util.ConverFloat(remoteVersion)

	var args []string
	args = append(args, remoteVersion)

	switch {
	case localVersion == config.UNKNOWN:
		P(WARING, "local latest version is %v.\n", config.UNKNOWN)
		if code := Install(args, global); code == 0 || code == 2 {
			config.SetConfig(config.LATEST_VERSION, remoteVersion)
			P(DEFAULT, "Update latest success, current latest version is %v.\n", remoteVersion)
		}
	case local == remote:

		if isDirExist(rootPath + localVersion) {
			cp := CP{Red, false, None, false, "="}
			P(DEFAULT, "Remote latest version %v %v latest version %v, don't need to upgrade.\n", remoteVersion, cp, localVersion)
			if global {
				if ok := Use(localVersion); ok {
					config.SetConfig(config.GLOBAL_VERSION, localVersion)
				}
			}
		} else if !isDirExist(rootPath + localVersion) {
			P(WARING, "local not exist %v\n", localVersion)
			if code := Install(args, global); code == 0 || code == 2 {
				P(DEFAULT, "Download latest version %v success.\n", localVersion)
			}
		}

	case local > remote:
		cp := CP{Red, false, None, false, ">"}
		P(WARING, "local latest version %v %v remote latest version %v.\nPlease check your registry. See 'gnvm help config'.\n", localVersion, cp, remoteVersion)
	case local < remote:
		cp := CP{Red, false, None, false, ">"}
		P(WARING, "remote latest version %v %v local latest version %v.\n", remoteVersion, cp, localVersion)
		if code := Install(args, global); code == 0 || code == 2 {
			config.SetConfig(config.LATEST_VERSION, remoteVersion)
			P(DEFAULT, "Update latest success, current latest version is %v.\n", remoteVersion)
		}
	}
}
Ejemplo n.º 4
0
	Long: `Use any version of the local already exists e.g.
gnvm use x.xx.xx
gnvm use latest`,
	Run: func(cmd *cobra.Command, args []string) {
		if len(args) == 1 {

			args[0] = util.EqualAbs("latest", args[0])

			if args[0] != "latest" && util.VerifyNodeVersion(args[0]) != true {
				P(ERROR, "use parameter support '%v' or '%v', e.g. %v, please check your input. See '%v'.\n", "latest", "x.xx.xx", "0.10.28", "gnvm help use")
				return
			}

			// set use
			if ok := nodehandle.Use(args[0]); ok == true {
				config.SetConfig(config.GLOBAL_VERSION, nodehandle.TransLatestVersion(args[0], false))
			}
		} else {
			P(ERROR, "use parameter maximum is 1, please check your input. See '%v'.\n", "gnvm help use")
		}
	},
}

// sub cmd
var updateCmd = &cobra.Command{
	Use:   "update",
	Short: "Update latest node.exe",
	Long: `Update latest node.exe e.g.
gnvm update latest
gnvm update latest --global`,
	Run: func(cmd *cobra.Command, args []string) {
Ejemplo n.º 5
0
/*
 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")
		}
	}
}
Ejemplo n.º 6
0
/*
 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
}
Ejemplo n.º 7
0
	Run: func(cmd *cobra.Command, args []string) {
		if _, ok := util.IsSessionEnv("use", true); ok {
			return
		}
		if len(args) == 1 {
			version := args[0]
			version = util.EqualAbs("latest", version)
			if util.VerifyNodeVer(version) != true {
				P(ERROR, "%v param only support [%v] or %v e.g. [%v], please check your input. See '%v'.\n", "gnvm use", "latest", "valid Node.js version", "5.9.1", "gnvm help use")
				return
			}

			// set use
			if ok := nodehandle.Use(version); ok == true {
				util.FormatLatVer(&version, config.GetConfig(config.LATEST_VERSION), false)
				config.SetConfig(config.GLOBAL_VERSION, version)
			}
		} else {
			P(ERROR, "%v must be only %v parameter, please check your input. See '%v'.\n", "gnvm use", "one", "gnvm help use")
		}
	},
}

// sub cmd
var sessionCmd = &cobra.Command{
	Use:   "session",
	Short: "Set any local Node.js version to session Node.js version",
	Long: `
Set any Node.js version of the local already exists to session Node.js version, e.g. :
gnvm session start        :Create gns.cmd.
gnvm session close        :Remove gns.cmd.