web.go 3.6 KB
Newer Older
1 2 3 4 5
package web

import(
  "fmt"
  "net/http"
6
  "net/url"
7 8 9 10 11 12
  "os"
  "io"
  "io/ioutil"
  "strings"
  "strconv"
  "../arch"
E
Eddie Huang 已提交
13
  "../file"
14 15
)

16 17 18 19 20 21 22 23 24 25 26
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{}
  }
}

27 28 29 30 31 32 33 34
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()

35
  response, err := client.Get(url)
36 37 38 39 40
  if err != nil {
    fmt.Println("Error while downloading", url, "-", err)
  }
  defer response.Body.Close()

C
Corey Butler 已提交
41
  _, err = io.Copy(output, response.Body)
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
  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)

62 63 64 65
  vpre := ""
  vers := strings.Fields(strings.Replace(v,"."," ",-1))
  main, _ := strconv.ParseInt(vers[0],0,0)

66
  if a == "32" {
67 68 69 70 71 72 73 74 75 76
    if main > 0 {
      vpre = "win-x86/"
    } else {
      vpre = ""
    }
  } else if a == "64" {
    if main > 0 {
      vpre = "win-x64/"
    } else {
      vpre = "x64/"
77 78
    }
  }
79 80
  
  url := getNodeUrl ( v, vpre );
81

82 83 84
  if url == "" {
    //No url should mean this version/arch isn't available
    fmt.Println("Node.js v"+v+" " + a + "bit isn't available right now.")
85
  } else {
86 87 88 89 90 91 92 93 94 95
   fileName := root+"\\v"+v+"\\node"+a+".exe"

    fmt.Printf("Downloading node.js version "+v+" ("+a+"-bit)... ")

    if Download(url,fileName) {
      fmt.Printf("Complete\n")
      return true
    } else {
      return false
    }
96
  }
97
  return false
98 99 100

}

E
Eddie Huang 已提交
101
func GetNpm(root string, v string) bool {
102
  url := "https://github.com/npm/npm/archive/v"+v+".zip"
E
Eddie Huang 已提交
103 104 105 106 107 108 109 110 111 112 113 114 115
  // temp directory to download the .zip file
  tempDir := root+"\\temp"

  // if the temp directory doesn't exist, create it
  if (!file.Exists(tempDir)) {
    fmt.Println("Creating "+tempDir+"\n")
    err := os.Mkdir(tempDir, os.ModePerm)
    if err != nil {
      fmt.Println(err)
      os.Exit(1)
    }
  }
  fileName := tempDir+"\\"+"npm-v"+v+".zip"
116 117 118 119 120 121 122 123 124 125 126

  fmt.Printf("Downloading npm version "+v+"... ")
  if Download(url,fileName) {
    fmt.Printf("Complete\n")
    return true
  } else {
    return false
  }
}

func GetRemoteTextFile(url string) string {
127
  response, httperr := client.Get(url)
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
  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 ""
}

P
Patrick Sullivan 已提交
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
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
  }

  // TODO: fixme. Assume a 64 bit version exists
  return true
}

162 163
func getNodeUrl (v string,  vpre string) string {
  url := "http://nodejs.org/dist/v"+v+"/" + vpre + "/node.exe"
164
  // Check online to see if a 64 bit version exists
165
  res, err := client.Head( url )
166
  if err != nil {
167 168 169 170 171 172
    return ""
  }
  if res.StatusCode == 200 {
    return url
  } else {
    return ""
173 174
  }
}