/
list.go
78 lines (67 loc) · 1.79 KB
/
list.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
package main
import (
"github.com/juju/cmd"
"github.com/juju/errors"
"github.com/juju/gnuflag"
"github.com/juju/juju/cmd/modelcmd"
jujuversion "github.com/juju/juju/version"
"github.com/juju/version"
)
const listToolsCommandDoc = `
Juju tools list is used to list tools in the Juju model/controller.
`
type listToolsCommand struct {
modelcmd.ModelCommandBase
out cmd.Output
versionMajor int
series string
arch string
}
// Info implements Command.Info.
func (c *listToolsCommand) Info() *cmd.Info {
return &cmd.Info{
Name: "list",
Purpose: "list tools to the controller",
Doc: listToolsCommandDoc,
}
}
func (c *listToolsCommand) SetFlags(f *gnuflag.FlagSet) {
c.out.AddFlags(f, "smart", cmd.DefaultFormatters)
f.IntVar(&c.versionMajor, "major", jujuversion.Current.Major, "filter tools by major version")
f.StringVar(&c.series, "series", "", "filter tools by series")
f.StringVar(&c.arch, "arch", "", "filter tools by architecture")
}
// Init implements Command.Init.
func (c *listToolsCommand) Init(args []string) error {
return nil
}
type toolsInfo struct {
URL string
Size int64
SHA256 string
}
// Run implements Command.Run.
func (c *listToolsCommand) Run(ctx *cmd.Context) error {
conn, err := c.NewAPIRoot()
if err != nil {
return errors.Annotate(err, "connecting to Juju")
}
defer conn.Close()
client := conn.Client()
result, err := client.FindTools(c.versionMajor, -1, c.series, c.arch)
if err != nil {
return err
}
if result.Error != nil {
return result.Error
}
versions := make(map[version.Binary][]toolsInfo)
for _, tools := range result.List {
versions[tools.Version] = append(versions[tools.Version], toolsInfo{
URL: tools.URL,
Size: tools.Size,
SHA256: tools.SHA256,
})
}
return c.out.Write(ctx, versions)
}