/
downloadR.go
73 lines (59 loc) · 1.91 KB
/
downloadR.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package main
import (
"fmt"
"github.com/cheggaaa/pb"
"io"
"net/http"
"os"
"path/filepath"
"strconv"
"strings"
"time"
)
// DownloadR download an R installer from a specified URL
func downloadR(url string, rootPath string) string {
// Parse URL and create filename from last element
tokens := strings.Split(url, "/")
fileName := tokens[len(tokens)-1]
// Check if file has already been downloaded. If not, create the file at the specified path
installerPath := createDownloadPath(rootPath, fileName)
fmt.Println(installerPath)
// Check to see if the installer has already been downloaded
if _, err := os.Stat(installerPath); err == nil {
fmt.Println(fileName, "already exists!")
// return forward-slash installer path
return filepath.ToSlash(installerPath)
}
output, err := os.Create(installerPath)
errCheck(err)
defer output.Close()
// Start download process
fmt.Println("Downloading", url, "to", fileName)
// Download file from URL
response, err := http.Get(url)
errCheck(err)
defer response.Body.Close()
// Print http response status to console
fmt.Println(response.Status)
// Get the response size from the HTTP header for progress bar
responseSize, _ := strconv.Atoi(response.Header.Get("Content-Length"))
// Create progress bar
bar := pb.New(int(responseSize)).SetUnits(pb.U_BYTES).SetRefreshRate(time.Millisecond * 10)
bar.ShowSpeed = true
bar.SetWidth(120)
bar.Start()
// Create multi-writer for output destination and progress bar
writer := io.MultiWriter(output, bar)
// Copy to output
_, err = io.Copy(writer, response.Body)
errCheck(err)
bar.Finish()
fmt.Printf("%s with %v bytes downloaded\n", fileName, responseSize)
// return forward-slash installer path
return filepath.ToSlash(installerPath)
}
func createDownloadPath(rootPath string, fileName string) string {
absPath, err := filepath.Abs(rootPath)
errCheck(err)
return convertToWindowsPath(filepath.Join(absPath, fileName))
}