-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
142 lines (123 loc) · 2.64 KB
/
main.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package main
import (
"crypto/tls"
"crypto/x509"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"github.com/akerl/timber/v2/log"
"github.com/mholt/archiver/v3"
"github.com/dock0/ducktape/cmd"
)
var logger = log.NewLogger("ducktape")
func usage() {
exampleURL := "https://example.org/download.tar.bz2"
fmt.Printf("Usage: %s %s\n", os.Args[0], exampleURL)
fmt.Printf("Alternate usage: DUCKTAPE_URL=%s %s\n", exampleURL, os.Args[0])
}
func getDirPath() (string, error) {
binPath, err := os.Executable()
if err != nil {
return "", err
}
return filepath.Dir(binPath), nil
}
func getFilePath(name string) (string, error) {
dirPath, err := getDirPath()
if err != nil {
return "", err
}
return filepath.Join(dirPath, name), nil
}
func getTmpFile(extension string) (string, error) {
dirPath, err := getDirPath()
if err != nil {
return "", err
}
pattern := fmt.Sprintf("ducktape-*%s", extension)
file, err := ioutil.TempFile(dirPath, pattern)
if err != nil {
return "", err
}
file.Close()
return file.Name(), nil
}
func getTLSConfig() (tls.Config, error) {
certFile, err := getFilePath("cert")
if err != nil {
return tls.Config{}, err
}
cert, err := ioutil.ReadFile(certFile)
if err != nil {
return tls.Config{}, err
}
pool := x509.NewCertPool()
pool.AppendCertsFromPEM(cert)
return tls.Config{RootCAs: pool}, nil
}
func getTLSClient() (http.Client, error) {
tlsConfig, err := getTLSConfig()
if err != nil {
return http.Client{}, err
}
transport := http.Transport{TLSClientConfig: &tlsConfig}
return http.Client{Transport: &transport}, nil
}
func download(path, url string) error {
file, err := os.Create(path)
if err != nil {
return err
}
defer file.Close()
client, err := getTLSClient()
if err != nil {
return err
}
logger.InfoMsgf("Downloading from %s", url)
response, err := client.Get(url)
if err != nil {
return err
}
defer response.Body.Close()
if response.StatusCode != http.StatusOK {
return fmt.Errorf("failed http response: %s", response.Status)
}
_, err = io.Copy(file, response.Body)
return err
}
func execute(url string) error {
path, err := getTmpFile(".tar.bz2")
if err != nil {
return err
}
logger.InfoMsgf("Created tmp file %s", path)
defer os.Remove(path)
err = download(path, url)
if err != nil {
return err
}
logger.InfoMsgf("Beginning unarchive")
return archiver.Unarchive(path, "/")
}
func main() {
url := os.Getenv("DUCKTAPE_URL")
if len(os.Args) > 1 {
if os.Args[1] == "-v" {
fmt.Println(cmd.Version)
return
}
url = os.Args[1]
}
if len(url) == 0 {
usage()
os.Exit(1)
}
err := execute(url)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}