forked from tcolgate/godinstall
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupload_cli.go
174 lines (143 loc) · 3.12 KB
/
upload_cli.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package main
import (
"encoding/json"
"errors"
"io"
"io/ioutil"
"log"
"mime/multipart"
"net/http"
"os"
"path/filepath"
"strings"
"github.com/codegangsta/cli"
)
type request struct {
req *http.Request
err error
}
type passResponse struct {
Complete bool
SessionID string
Expecting map[string]struct {
Received bool
}
}
type failResponse struct {
Message string
}
// CmdUpload is the implementation of the godinstall "upload" command
func CmdUpload(c *cli.Context) {
ret := 0
url := c.String("url")
client := &http.Client{}
for _, a := range c.Args() {
err := cliUploadFile(client, url, a)
if err != nil {
log.Printf("Upload of %s failed, %s", a, err.Error())
ret = 1
}
}
os.Exit(ret)
}
// Streams upload directly from file -> mime/multipart -> pipe -> http-request
func streamingUploadFile(res chan request, uri, paramName, path string) {
file, err := os.Open(path)
if err != nil {
res <- request{nil, err}
return
}
defer file.Close()
r, w := io.Pipe()
defer w.Close()
req, err := http.NewRequest("POST", uri, r)
if err != nil {
res <- request{nil, err}
return
}
writer := multipart.NewWriter(w)
req.Header.Set("Content-Type", writer.FormDataContentType())
res <- request{req, nil}
part, err := writer.CreateFormFile(paramName, filepath.Base(path))
if err != nil {
log.Fatal(err)
return
}
_, err = io.Copy(part, file)
if err != nil {
log.Fatal(err)
return
}
err = writer.Close()
if err != nil {
log.Fatal(err)
return
}
}
// Creates a new file upload http request with optional extra params
func newfileUploadRequest(uri string, paramName, path string) (*http.Request, error) {
req := make(chan request)
go streamingUploadFile(req, uri, paramName, path)
resp := <-req
return resp.req, resp.err
}
func cliUploadFile(c *http.Client, uri, firstfn string) error {
dir := filepath.Dir(firstfn)
switch {
case strings.HasSuffix(firstfn, ".deb"), strings.HasSuffix(firstfn, ".changes"):
fns := []string{firstfn}
sessionid := ""
for {
if len(fns) == 0 {
return nil
}
fn := filepath.Base(fns[0])
fns = fns[1:]
log.Printf("Uploading %s\n", fn)
req, err := newfileUploadRequest(uri, "debfiles", dir+"/"+fn)
if err != nil {
return err
}
resp, err := c.Do(req)
defer resp.Body.Close()
if err != nil {
return err
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
if resp.StatusCode >= 400 {
var status failResponse
err = json.Unmarshal(body, &status)
if err != nil {
return errors.New(string(body))
}
return errors.New(status.Message)
}
var status passResponse
err = json.Unmarshal(body, &status)
if err != nil {
return err
}
if status.Complete {
log.Printf("Completed %s", firstfn)
return nil
}
if sessionid == "" {
sessionid = status.SessionID
uri = uri + "/" + sessionid
}
for k, v := range status.Expecting {
if !v.Received {
fns = append(fns, k)
}
}
if len(fns) == 0 {
return errors.New("Upload incomplete, but no files expected")
}
}
default:
return errors.New("Do not know how to upload ")
}
}