/
cmd_dl_create.go
242 lines (207 loc) · 5.79 KB
/
cmd_dl_create.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"mime/multipart"
"net/http"
"os"
"path"
"path/filepath"
"github.com/gonuts/commander"
"github.com/gonuts/flag"
"github.com/sbinet/go-github-client/client"
)
type s3_data struct {
Url string `json:"url"`
HtmlUrl string `json:"html_url"`
Id int `json:"id"`
Name string `json:"name"`
Descr string `json:"description"`
Size int `json:"size"`
DlCount int `json:"download_count"`
ContentType string `json:"content_type"`
Policy string `json:"policy"`
Signature string `json:"signature"`
Bucket string `json:"bucket"`
AccessKeyId string `json:"accesskeyid"`
Path string `json:"path"`
Acl string `json:"acl"`
ExpirationDate string `json:"expirationdate"`
Prefix string `json:"prefix"`
MimeType string `json:"mime_type"`
Redirect bool `json:"redirect"`
S3Url string `json:"s3_url"`
}
func get_s3_data(r *client.GithubResult) (s3 s3_data, err error) {
if r.RawHttpResponse.ContentLength == 0 {
// EMPTY OBJECT
err = json.Unmarshal(([]byte)("[]"), &s3)
} else if r.RawHttpResponse.ContentLength != 0 {
defer r.RawHttpResponse.Body.Close()
data := make([]byte, 0, r.RawHttpResponse.ContentLength)
data, err = ioutil.ReadAll(r.RawHttpResponse.Body)
if err != nil {
return s3, err
}
err = json.Unmarshal(data, &s3)
}
return s3, err
}
func git_make_cmd_dl_create() *commander.Command {
cmd := &commander.Command{
Run: git_run_cmd_dl_create,
UsageLine: "dl-create [options] -f=file -repo=repo",
Short: "creates a new download on github",
Long: `
dl-create creates a new download asset on a github repository.
ex:
$ goctogit dl-create -descr "new tarball" -f=foo.tar.gz -repo=mana-core
$ goctogit dl-create -descr "new tarball" -f=foo.tar.gz -repo=mana-core -org my-organyzation
`,
Flag: *flag.NewFlagSet("git-dl-create", flag.ExitOnError),
}
cmd.Flag.String("descr", "", "description of the new github repository")
cmd.Flag.String("f", "", "path to file to upload on github")
cmd.Flag.String("u", "", "github user account")
cmd.Flag.String("repo", "", "name of the github repository")
cmd.Flag.String("org", "", "github organization account")
return cmd
}
func git_run_cmd_dl_create(cmd *commander.Command, args []string) {
n := "github-" + cmd.Name()
if len(args) != 0 {
err := fmt.Errorf("%s: does NOT take any positional parameter", n)
handle_err(err)
}
repo_name := cmd.Flag.Lookup("repo").Value.Get().(string)
fname := cmd.Flag.Lookup("f").Value.Get().(string)
if repo_name == "" {
err := fmt.Errorf("%s: needs a github repository name where to put the download", n)
handle_err(err)
}
if fname == "" {
err := fmt.Errorf("%s: needs a path to a file to upload", n)
handle_err(err)
}
fname, err := filepath.Abs(fname)
if err != nil {
handle_err(err)
}
if !path_exists(fname) {
err := fmt.Errorf("%s: needs a path to an EXISTING file to upload", n)
handle_err(err)
}
fi, err := os.Lstat(fname)
if err != nil {
handle_err(err)
}
user := cmd.Flag.Lookup("u").Value.Get().(string)
org := cmd.Flag.Lookup("org").Value.Get().(string)
descr := cmd.Flag.Lookup("descr").Value.Get().(string)
if user == "" {
v, err := Cfg.String("go-octogit", "username")
handle_err(err)
user = v
}
password, err := Cfg.String("go-octogit", "password")
handle_err(err)
ghc, err := client.NewGithubClient(user, password, client.AUTH_USER_PASSWORD)
handle_err(err)
account := user
// POST /repos/:owner/:repo/downloads
if org != "" {
account = org
}
url := path.Join("repos", account, repo_name, "downloads")
fmt.Printf("%s: uploading [%s] to [%s/%s]...\n",
n, fname, account, repo_name)
data, err := json.Marshal(
map[string]interface{}{
"name": fi.Name(),
"size": fi.Size(),
"description": descr,
//"content_type": "FIXME: TODO",
})
handle_err(err)
req, err := ghc.NewAPIRequest("POST", url, bytes.NewBuffer(data))
handle_err(err)
resp, err := ghc.RunRequest(req, new(http.Client))
handle_err(err)
sc := resp.RawHttpResponse.StatusCode
switch sc {
case 201:
// all good
case 422:
// pre-existing file
err = fmt.Errorf(
"%s: a file with that name is already in the download area!\n",
n)
default:
err = fmt.Errorf(
"%s: request did not succeed. got (status=%d)\n%v\n",
n, sc, resp.RawHttpResponse,
)
}
if err != nil {
handle_err(err)
}
s3, err := get_s3_data(resp)
if err != nil {
handle_err(err)
}
body_buf := bytes.NewBufferString("")
body_writer := multipart.NewWriter(body_buf)
for _, v := range [][2]string{
{"key", s3.Path},
{"acl", "public-read"},
{"success_action_status", "201"},
{"Filename", s3.Name},
{"AWSAccessKeyId", s3.AccessKeyId},
{"Policy", s3.Policy},
{"Signature", s3.Signature},
{"Content-Type", s3.ContentType},
} {
err = body_writer.WriteField(v[0], v[1])
if err != nil {
handle_err(err)
}
}
file_writer, err := body_writer.CreateFormFile("file", fname)
if err != nil {
handle_err(err)
}
fh, err := os.Open(fname)
if err != nil {
handle_err(err)
}
defer fh.Close()
_, err = io.Copy(file_writer, fh)
if err != nil {
handle_err(err)
}
content_type := body_writer.FormDataContentType()
err = body_writer.Close()
if err != nil {
handle_err(err)
}
s3_resp, err := http.Post(s3.S3Url, content_type, body_buf)
if err != nil {
fmt.Printf("\n%s: s3-request failed:\n%v\n%v\n", n, err, s3_resp)
handle_err(err)
}
switch s3_resp.StatusCode {
case 201:
// all good
default:
err = fmt.Errorf("%s: s3-request did not succeed. got (status=%d) %v\n", n, s3_resp.StatusCode, s3_resp)
}
if err != nil {
handle_err(err)
}
fmt.Printf("%s: uploading [%s] to [%s/%s]... [done]\n",
n, fname, account, repo_name)
}
// EOF