提交 404bb7ba 编写于 作者: C Corey Butler

Added proxy support. Closes issue #5

上级 dbb7a10f
......@@ -25,6 +25,7 @@ type Environment struct {
root string
symlink string
arch string
proxy string
}
var env = &Environment{
......@@ -32,6 +33,7 @@ var env = &Environment{
root: "",
symlink: "",
arch: os.Getenv("PROCESSOR_ARCHITECTURE"),
proxy: "none",
}
func main() {
......@@ -72,6 +74,13 @@ func main() {
case "arch":
_, a := node.GetCurrentVersion()
fmt.Println(a+"-bit")
case "proxy":
if detail == "" {
fmt.Println("Current proxy: "+env.proxy)
} else {
env.proxy = detail
saveSettings()
}
default: help()
}
}
......@@ -375,10 +384,11 @@ func help() {
fmt.Println(" nvm install <version> [arch] : The version can be a node.js version or \"latest\" for the latest stable version.")
fmt.Println(" Optionally specify whether to install the 32 or 64 bit version (defaults to system arch).")
fmt.Println(" Set [arch] to \"all\" to install 32 AND 64 bit versions.")
fmt.Println(" nvm list : List what is currently installed.")
fmt.Println(" nvm list : List the node.js installations.")
fmt.Println(" nvm on : Enable node.js version management.")
fmt.Println(" nvm off : Disable node.js version management.")
fmt.Println(" nvm proxy [url] : Set a proxy to use for downloads. Leave [url] blank to see the current proxy.")
fmt.Println(" Set [url] to \"none\" to remove the proxy.")
fmt.Println(" nvm uninstall <version> : The version must be a specific version.")
fmt.Println(" nvm use <version> [arch] : Switch to use the specified version. Optionally specify 32/64bit architecture.")
fmt.Println(" nvm use <arch> will continue using the selected version, but switch to 32/64 bit mode.")
......@@ -416,7 +426,7 @@ func updateRootDir(path string) {
}
func saveSettings() {
content := "root: "+strings.Trim(env.root," \n\r")+"\r\npath: "+strings.Trim(env.symlink," \n\r")+"\r\narch: "+strings.Trim(env.arch," \n\r")
content := "root: "+strings.Trim(env.root," \n\r")+"\r\npath: "+strings.Trim(env.symlink," \n\r")+"\r\narch: "+strings.Trim(env.arch," \n\r")+"\r\nproxy: "+strings.Trim(env.proxy," \n\r")
ioutil.WriteFile(env.settings, []byte(content), 0644)
}
......@@ -435,6 +445,14 @@ func Setup() {
env.symlink = strings.Trim(regexp.MustCompile("path:").ReplaceAllString(line,"")," \r\n")
} else if strings.Contains(line,"arch:"){
env.arch = strings.Trim(regexp.MustCompile("arch:").ReplaceAllString(line,"")," \r\n")
} else if strings.Contains(line,"proxy:"){
env.proxy = strings.Trim(regexp.MustCompile("proxy:").ReplaceAllString(line,"")," \r\n")
if env.proxy != "none" && env.proxy != "" {
if strings.ToLower(env.proxy[0:4]) != "http" {
env.proxy = "http://"+env.proxy
}
web.SetProxy(env.proxy)
}
}
}
......
package web
import(
"fmt"
"net/http"
"os"
"io"
"io/ioutil"
"strings"
"strconv"
"../arch"
)
func Download(url string, target string) bool {
output, err := os.Create(target)
if err != nil {
fmt.Println("Error while creating", target, "-", err)
}
defer output.Close()
response, err := http.Get(url)
if err != nil {
fmt.Println("Error while downloading", url, "-", err)
}
defer response.Body.Close()
n, err := io.Copy(output, response.Body)
n=n
if err != nil {
fmt.Println("Error while downloading", url, "-", err)
}
if response.Status[0:3] != "200" {
fmt.Println("Download failed. Rolling Back.")
err := os.Remove(target)
if err != nil {
fmt.Println("Rollback failed.",err)
}
return false
}
return true
}
func GetNodeJS(root string, v string, a string) bool {
a = arch.Validate(a)
url := ""
if a == "32" {
url = "http://nodejs.org/dist/v"+v+"/node.exe"
} else {
if !IsNode64bitAvailable(v) {
fmt.Println("Node.js v"+v+" is only available in 32-bit.")
return false
}
url = "http://nodejs.org/dist/v"+v+"/x64/node.exe"
}
fileName := root+"\\v"+v+"\\node"+a+".exe"
fmt.Printf("Downloading node.js version "+v+" ("+a+"-bit)... ")
if Download(url,fileName) {
fmt.Printf("Complete")
return true
} else {
return false
}
}
func GetNpm(v string) bool {
url := "https://github.com/npm/npm/archive/v"+v+".zip"
fileName := os.TempDir()+"\\"+"npm-v"+v+".zip"
fmt.Printf("Downloading npm version "+v+"... ")
if Download(url,fileName) {
fmt.Printf("Complete")
return true
} else {
return false
}
}
func GetRemoteTextFile(url string) string {
response, httperr := http.Get(url)
if httperr != nil {
fmt.Println("\nCould not retrieve "+url+".\n\n")
fmt.Printf("%s", httperr)
os.Exit(1)
} else {
defer response.Body.Close()
contents, readerr := ioutil.ReadAll(response.Body)
if readerr != nil {
fmt.Printf("%s", readerr)
os.Exit(1)
}
return string(contents)
}
os.Exit(1)
return ""
}
func IsNode64bitAvailable(v string) bool {
if v == "latest" {
return true
}
// Anything below version 8 doesn't have a 64 bit version
vers := strings.Fields(strings.Replace(v,"."," ",-1))
main, _ := strconv.ParseInt(vers[0],0,0)
minor, _ := strconv.ParseInt(vers[1],0,0)
if main == 0 && minor < 8 {
return false
}
// Check online to see if a 64 bit version exists
res, err := http.Head("http://nodejs.org/dist/v"+v+"/x64/node.exe")
if err != nil {
return false
}
return res.StatusCode == 200
}
......@@ -3,6 +3,7 @@ package web
import(
"fmt"
"net/http"
"net/url"
"os"
"io"
"io/ioutil"
......@@ -11,6 +12,17 @@ import(
"../arch"
)
var client = &http.Client{}
func SetProxy(p string){
if p != "" && p != "none" {
proxyUrl, _ := url.Parse(p)
client = &http.Client{Transport: &http.Transport{Proxy: http.ProxyURL(proxyUrl)}}
} else {
client = &http.Client{}
}
}
func Download(url string, target string) bool {
output, err := os.Create(target)
......@@ -19,7 +31,7 @@ func Download(url string, target string) bool {
}
defer output.Close()
response, err := http.Get(url)
response, err := client.Get(url)
if err != nil {
fmt.Println("Error while downloading", url, "-", err)
}
......@@ -84,7 +96,7 @@ func GetNpm(v string) bool {
}
func GetRemoteTextFile(url string) string {
response, httperr := http.Get(url)
response, httperr := client.Get(url)
if httperr != nil {
fmt.Println("\nCould not retrieve "+url+".\n\n")
fmt.Printf("%s", httperr)
......@@ -116,7 +128,7 @@ func IsNode64bitAvailable(v string) bool {
}
// Check online to see if a 64 bit version exists
res, err := http.Head("http://nodejs.org/dist/v"+v+"/x64/node.exe")
res, err := client.Head("http://nodejs.org/dist/v"+v+"/x64/node.exe")
if err != nil {
return false
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册