-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcommand.go
266 lines (245 loc) · 7.03 KB
/
command.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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
package main
import (
"bytes"
"context"
"encoding/json"
"flag"
"fmt"
"io"
"log"
"net/http"
"os"
"path/filepath"
"strings"
packagecloud "github.com/atotto/packagecloud/api/v1"
"github.com/google/subcommands"
"github.com/mattn/go-zglob"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
type commandBase struct {
name string
synopsis string
usage string
examples []string
setFlagsFunc func(f *flag.FlagSet)
executeFunc func(ctx context.Context, f *flag.FlagSet, args ...interface{}) subcommands.ExitStatus
}
func (c *commandBase) Name() string { return c.name }
func (c *commandBase) Synopsis() string {
return fmt.Sprintf("packagecloud %s", c.synopsis)
}
func (c *commandBase) Usage() string {
w := bytes.NewBufferString(c.usage)
fmt.Fprintln(w)
if len(c.examples) > 0 {
fmt.Fprintln(w, "\nexample:")
for _, ex := range c.examples {
fmt.Fprintf(w, " %s\n", ex)
}
}
return w.String()
}
func (c *commandBase) SetFlags(f *flag.FlagSet) {
if c.setFlagsFunc == nil {
return
}
c.setFlagsFunc(f)
}
func (c *commandBase) Execute(ctx context.Context, f *flag.FlagSet, args ...interface{}) subcommands.ExitStatus {
return c.executeFunc(ctx, f, args)
}
var pushPackageCommandVerifyExist bool
var pushPackageCommand = &commandBase{
"push",
"pushing a package",
"push name/repo/distro/version filepath",
[]string{"packagecloud push example-user/example-repository/ubuntu/xenial /tmp/example.deb"},
func(f *flag.FlagSet) {
f.BoolVar(&pushPackageCommandVerifyExist, "verify exist package", false, "ignore already pushed error")
},
func(ctx context.Context, f *flag.FlagSet, args ...interface{}) subcommands.ExitStatus {
repos, distro, version, n := splitPackageTarget(f.Arg(0))
if n < 3 {
return subcommands.ExitUsageError
}
var files []string
for _, file := range f.Args()[1:] {
fs, err := zglob.Glob(file)
if err != nil {
return subcommands.ExitFailure
}
files = append(files, fs...)
}
fmt.Printf("pushing %d files...\n", len(files))
for _, file := range files {
_, fname := filepath.Split(file)
log.Printf("push: %s", fname)
if err := packagecloud.PushPackage(ctx, repos, distro, version, file); err != nil {
if !pushPackageCommandVerifyExist && status.Code(err) == codes.AlreadyExists {
log.Printf("%s already exist", fname)
continue
}
log.Printf("%s %s", fname, err)
return subcommands.ExitFailure
}
}
return subcommands.ExitSuccess
},
}
var searchPackageCommand = &commandBase{
"list",
"list package",
"list name/repo query [version]",
[]string{"packagecloud list example-user/example-repository example 1.0.0"},
nil,
func(ctx context.Context, f *flag.FlagSet, args ...interface{}) subcommands.ExitStatus {
repos, distro, _, n := splitPackageTarget(f.Arg(0))
if n < 2 {
return subcommands.ExitUsageError
}
query := f.Arg(1)
details, err := packagecloud.SearchPackage(ctx, repos, distro, query, "")
if err != nil {
log.Println(err)
return subcommands.ExitFailure
}
version := f.Arg(2)
for _, detail := range details {
if version == "" || detail.Version == version {
fmt.Printf("%s %s %s %s\n", detail.DistroVersion, detail.Filename, detail.Name, detail.Version)
}
}
return subcommands.ExitSuccess
},
}
var pullPackageCommand = &commandBase{
"pull",
"pull package",
"pull name/repo[/distro/version] filename",
[]string{"packagecloud pull example-user/example-repository example_1.0.0_all.deb"},
nil,
func(ctx context.Context, f *flag.FlagSet, args ...interface{}) subcommands.ExitStatus {
repos, distro, version, n := splitPackageTarget(f.Arg(0))
if n < 2 {
return subcommands.ExitUsageError
}
query := f.Arg(1)
details, err := packagecloud.SearchPackage(ctx, repos, distro, query, "")
if err != nil {
log.Println(err)
return subcommands.ExitFailure
}
for _, detail := range details {
if version == "" || detail.Version == version {
fmt.Printf("%s %s %s %s\n", detail.Filename, detail.Name, detail.Version, detail.DistroVersion)
f, err := os.OpenFile(detail.Filename, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
if err != nil {
log.Println(err)
return subcommands.ExitFailure
}
defer f.Close()
resp, err := http.Get(detail.DownloadURL)
if err != nil {
log.Println(err)
return subcommands.ExitFailure
}
defer resp.Body.Close()
io.Copy(f, resp.Body)
}
}
return subcommands.ExitSuccess
},
}
var deletePackageCommand = &commandBase{
"rm",
"deleting a package",
"rm name/repo/distro/version filepath",
[]string{"packagecloud rm example-user/example-repository/ubuntu/xenial example_1.0.1-1_amd64.deb"},
nil,
func(ctx context.Context, f *flag.FlagSet, args ...interface{}) subcommands.ExitStatus {
repos, distro, version, n := splitPackageTarget(f.Arg(0))
if n != 4 {
return subcommands.ExitUsageError
}
fpath := f.Arg(1)
if err := packagecloud.DeletePackage(ctx, repos, distro, version, fpath); err != nil {
log.Println(err)
return subcommands.ExitFailure
}
return subcommands.ExitSuccess
},
}
var promotePackageCommand = &commandBase{
"promote",
"promote package",
"promote name/src_repo/distro/version filepath name/dst_repo",
[]string{"packagecloud promote example-user/repo1/ubuntu/xenial example_1.0-1_amd64.deb example-user/repo2"},
nil,
func(ctx context.Context, f *flag.FlagSet, args ...interface{}) subcommands.ExitStatus {
srcRepos, distro, version, n := splitPackageTarget(f.Arg(0))
if n != 4 {
return subcommands.ExitUsageError
}
fpath := f.Arg(1)
dstRepos := f.Arg(2)
if err := packagecloud.PromotePackage(ctx, dstRepos, srcRepos, distro, version, fpath); err != nil {
log.Println(err)
return subcommands.ExitFailure
}
return subcommands.ExitSuccess
},
}
func splitPackageTarget(target string) (repos, distro, version string, n int) {
ss := strings.SplitN(target, "/", 4)
n = len(ss)
if n >= 2 {
repos = fmt.Sprintf("%s/%s", ss[0], ss[1])
}
if n >= 3 {
distro = ss[2]
}
if n >= 4 {
version = ss[3]
}
return
}
var helpDistroCommand = &commandBase{
"distro",
"list supported distributions",
"distro [deb/py]",
[]string{"packagecloud distro", "packagecloud distro deb", "packagecloud distro deb ubuntu", "packagecloud distro | jq .deb"},
nil,
func(ctx context.Context, f *flag.FlagSet, args ...interface{}) subcommands.ExitStatus {
var v interface{}
distributions, err := packagecloud.GetDistributions(ctx)
if err != nil {
log.Println(err)
return subcommands.ExitUsageError
}
switch typ := f.Arg(0); typ {
case "deb", "debian":
if name := f.Arg(1); name != "" {
for _, distros := range distributions.Deb {
if distros.IndexName == name {
v = distros.Versions
break
}
}
} else {
v = distributions.Deb
}
case "py", "python":
v = distributions.Py
case "":
v = distributions
default:
log.Printf("not supported type:%s", typ)
return subcommands.ExitUsageError
}
if err := json.NewEncoder(os.Stdout).Encode(v); err != nil {
return subcommands.ExitFailure
}
return subcommands.ExitSuccess
},
}