This repository has been archived by the owner on Nov 6, 2022. It is now read-only.
/
artifact.go
109 lines (87 loc) · 2.66 KB
/
artifact.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
package main
// All actions under command artifact
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"mime/multipart"
"net/http"
"net/url"
"os"
"path/filepath"
"strings"
)
// upload
type ArtifactUpload struct {
client *Client
format Formatter
}
func (a ArtifactUpload) Apply(args []string) {
Check(len(args) == 2, "must supply path and file")
path := args[0]
filename := args[1]
body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
part, e := writer.CreateFormFile("file", filepath.Base(path))
Check(e == nil, "failed to create form file", e)
f, e := os.Open(filename)
Check(e == nil, "failed to open file "+filename, e)
_, e = io.Copy(part, f)
Check(e == nil, "failed to get file bytes", e)
e = writer.Close()
Check(e == nil, "failed to close file", e)
request := a.client.POST("/v2/artifacts"+url.QueryEscape(path), body)
request.Header.Set("Content-Type", writer.FormDataContentType())
response, e := a.client.Do(request)
Check(e == nil, "failed to get response", e)
Check(response.StatusCode == 201, "unable to upload file", stringifyResponse(response))
defer response.Body.Close()
fmt.Println(a.format.Format(strings.NewReader(response.Header.Get("Location")), a.Humanize))
}
func (P ArtifactUpload) Humanize(body io.Reader) string {
b, e := ioutil.ReadAll(body)
Check(e == nil, "reading upload response failed", e)
text := "LOCATION\n" + string(b)
return Columnize(text)
}
// get
type ArtifactGet struct {
client *Client
format Formatter
}
func (a ArtifactGet) Apply(args []string) {
Check(len(args) == 1, "must supply id")
request := a.client.GET("/v2/artifacts" + url.QueryEscape(args[0]))
response, e := a.client.Do(request)
Check(e == nil, "failed to get response", e)
Check(response.StatusCode != 404, "artifact not found")
Check(response.StatusCode == 200, "error downloading artifact", stringifyResponse(response))
defer response.Body.Close()
b, e := ioutil.ReadAll(response.Body)
os.Stdout.Write(b)
}
// delete
type ArtifactDelete struct {
client *Client
format Formatter
}
func (a ArtifactDelete) Apply(args []string) {
Check(len(args) == 1, "must supply id")
request := a.client.DELETE("/v2/artifacts" + url.QueryEscape(args[0]))
response, e := a.client.Do(request)
Check(e == nil, "failed to delete artifact", e)
Check(response.StatusCode == 200, "failed to delete artifact", stringifyResponse(response))
defer response.Body.Close()
fmt.Println(a.format.Format(response.Body, a.Humanize))
}
func (a ArtifactDelete) Humanize(body io.Reader) string {
return "DELETED"
}
func stringifyResponse(res *http.Response) string {
cnt, err := ioutil.ReadAll(res.Body)
if err != nil {
return fmt.Sprintf("%v", err)
}
return string(cnt)
}