forked from rancher/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
catalog.go
155 lines (131 loc) · 2.93 KB
/
catalog.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
package cmd
import (
managementClient "github.com/rancher/types/client/management/v3"
"github.com/urfave/cli"
)
const (
addCatalogDescription = `
Add a new catalog to the Rancher server
Example:
# Add a catalog
$ rancher catalog add foo https://my.catalog
# Add a catalog and specify the branch to use
$ rancher catalog add --branch awesomebranch foo https://my.catalog
`
)
type CatalogData struct {
ID string
Catalog managementClient.Catalog
}
func CatalogCommand() cli.Command {
catalogLsFlags := []cli.Flag{
formatFlag,
quietFlag,
}
return cli.Command{
Name: "catalog",
Usage: "Operations with catalogs",
Action: defaultAction(catalogLs),
Flags: catalogLsFlags,
Subcommands: []cli.Command{
cli.Command{
Name: "ls",
Usage: "List catalogs",
Description: "\nList all catalogs in the current Rancher server",
ArgsUsage: "None",
Action: catalogLs,
Flags: catalogLsFlags,
},
cli.Command{
Name: "add",
Usage: "Add a catalog",
Description: addCatalogDescription,
ArgsUsage: "[NAME, URL]",
Action: catalogAdd,
Flags: []cli.Flag{
cli.StringFlag{
Name: "branch",
Usage: "Branch from the url to use",
Value: "master",
},
},
},
cli.Command{
Name: "delete",
Usage: "Delete a catalog",
Description: "\nDelete a catalog from the Rancher server",
ArgsUsage: "[CATALOG_NAME/CATALOG_ID]",
Action: catalogDelete,
},
},
}
}
func catalogLs(ctx *cli.Context) error {
c, err := GetClient(ctx)
if err != nil {
return err
}
collection, err := c.ManagementClient.Catalog.List(defaultListOpts(ctx))
if err != nil {
return err
}
writer := NewTableWriter([][]string{
{"ID", "ID"},
{"NAME", "Catalog.Name"},
{"URL", "Catalog.URL"},
{"BRANCH", "Catalog.Branch"},
{"KIND", "Catalog.Kind"},
}, ctx)
defer writer.Close()
for _, item := range collection.Data {
writer.Write(&CatalogData{
ID: item.ID,
Catalog: item,
})
}
return writer.Err()
}
func catalogAdd(ctx *cli.Context) error {
if len(ctx.Args()) < 2 {
return cli.ShowSubcommandHelp(ctx)
}
c, err := GetClient(ctx)
if err != nil {
return err
}
catalog := &managementClient.Catalog{
Branch: ctx.String("branch"),
Name: ctx.Args().First(),
Kind: "helm",
URL: ctx.Args().Get(1),
}
_, err = c.ManagementClient.Catalog.Create(catalog)
if err != nil {
return err
}
return nil
}
func catalogDelete(ctx *cli.Context) error {
if len(ctx.Args()) < 1 {
return cli.ShowSubcommandHelp(ctx)
}
c, err := GetClient(ctx)
if err != nil {
return err
}
for _, arg := range ctx.Args() {
resource, err := Lookup(c, arg, "catalog")
if err != nil {
return err
}
catalog, err := c.ManagementClient.Catalog.ByID(resource.ID)
if err != nil {
return err
}
err = c.ManagementClient.Catalog.Delete(catalog)
if err != nil {
return err
}
}
return nil
}