-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
148 lines (132 loc) · 3.56 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
143
144
145
146
147
148
package main
import (
"bufio"
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"mime/multipart"
"net/http"
"net/url"
"os"
"os/exec"
"strings"
"golang.org/x/crypto/ssh/terminal"
)
type HarborConfigs struct {
HarborBaseUrl string `json:"harborBaseUrl"`
HarborLogin string `json:"harborLogin"`
HarborPassword string `json:"harborPassword"`
HarborRepo string `json:"harborRepo"`
HarborChart string `json:"harborChart"`
}
func validThenGetURL(reader *bufio.Reader) (rawurl string) {
fmt.Print("Enter service URL: ")
rawurl, _ = reader.ReadString('\n')
rawurl = strings.TrimSpace(rawurl)
if _, err := url.ParseRequestURI(rawurl); err != nil {
fmt.Println("Invalid URL!")
rawurl = validThenGetURL(reader)
}
return
}
func NewfileUploadRequest(uri string, params map[string]string, paramName, path string) (*http.Request, error) {
file, err := os.Open(path)
if err != nil {
return nil, err
}
fileContents, err := ioutil.ReadAll(file)
if err != nil {
return nil, err
}
fi, err := file.Stat()
if err != nil {
return nil, err
}
file.Close()
body := new(bytes.Buffer)
writer := multipart.NewWriter(body)
part, err := writer.CreateFormFile(paramName, fi.Name())
if err != nil {
return nil, err
}
part.Write(fileContents)
for key, val := range params {
_ = writer.WriteField(key, val)
}
err = writer.Close()
if err != nil {
return nil, err
}
request, err := http.NewRequest("POST", uri, body)
request.Header.Add("Content-Type", writer.FormDataContentType())
return request, err
}
func Push(configs *HarborConfigs) {
path, _ := os.Getwd()
path += "/" + configs.HarborChart + ".gz"
urlBase := configs.HarborBaseUrl + "/api/chartrepo/" + configs.HarborRepo + "/charts"
extraParams := map[string]string{
"repo": configs.HarborChart,
}
request, err := NewfileUploadRequest(
urlBase,
extraParams,
"chart",
path,
)
if err != nil {
log.Println(err)
}
request.SetBasicAuth(configs.HarborLogin, configs.HarborPassword)
client := &http.Client{}
resp, err := client.Do(request)
if err != nil {
log.Fatal(err)
} else {
var bodyContent []byte
resp.Body.Read(bodyContent)
resp.Body.Close()
fmt.Println(bodyContent)
if resp.StatusCode == 201 {
fmt.Println("Saved")
}
}
}
func TarFolder(configs *HarborConfigs) {
cmd := exec.Command("/bin/sh", "-c", "tar -zcvf "+configs.HarborChart+".gz "+configs.HarborChart)
err := cmd.Run()
fmt.Println("Error GZ: ", err)
}
func main() {
args := os.Args[1:]
if len(args) < 2 {
fmt.Printf("No valid args found. Usage: %q\r\n", "harborpush CHART_NAME REPO_NAME")
os.Exit(0)
}
defaultPath := os.Getenv("HOME") + "/.harbor_config.json"
configs := HarborConfigs{}
if _, err := os.Stat(defaultPath); os.IsNotExist(err) {
reader := bufio.NewReader(os.Stdin)
configs.HarborBaseUrl = strings.TrimSpace(validThenGetURL(reader))
fmt.Print("Enter your harbor login: ")
harborLogin, _ := reader.ReadString('\n')
configs.HarborLogin = strings.TrimSpace(harborLogin)
fmt.Print("Enter your harbor password: ")
harborPassword, _ := terminal.ReadPassword(0)
configs.HarborPassword = strings.TrimSpace(string(harborPassword))
file, _ := json.MarshalIndent(configs, "", " ")
if err = ioutil.WriteFile(defaultPath, file, 0644); err != nil {
fmt.Println("Error trying to write config file. Err: ", err.Error())
}
} else {
file, _ := ioutil.ReadFile(defaultPath)
_ = json.Unmarshal([]byte(file), &configs)
}
configs.HarborChart = strings.TrimSpace(args[0])
configs.HarborRepo = strings.TrimSpace(args[1])
TarFolder(&configs)
Push(&configs)
os.Remove(configs.HarborChart + ".gz")
}