/
bucket.go
72 lines (61 loc) · 1.87 KB
/
bucket.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
package edge_storage
import (
"context"
"fmt"
"github.com/MakeNowJust/heredoc"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
msg "github.com/aziontech/azion-cli/messages/edge_storage"
"github.com/aziontech/azion-cli/messages/general"
api "github.com/aziontech/azion-cli/pkg/api/storage"
"github.com/aziontech/azion-cli/pkg/cmdutil"
"github.com/aziontech/azion-cli/pkg/contracts"
"github.com/aziontech/azion-cli/pkg/output"
)
func NewBucket(f *cmdutil.Factory) *cobra.Command {
bucket := &Bucket{
Factory: f,
Options: &contracts.ListOptions{},
}
cmd := &cobra.Command{
Use: msg.USAGE_BUCKET,
Short: msg.SHORT_DESCRIPTION_LIST_BUCKET,
Long: msg.LONG_DESCRIPTION_LIST_BUCKET,
SilenceUsage: true,
SilenceErrors: true,
RunE: bucket.RunE,
Example: heredoc.Doc(msg.EXAMPLE_LIST_BUCKET),
}
bucket.AddFlags(cmd.Flags())
return cmd
}
func (b *Bucket) RunE(cmd *cobra.Command, args []string) error {
client := api.NewClient(
b.Factory.HttpClient,
b.Factory.Config.GetString("storage_url"),
b.Factory.Config.GetString("token"))
return b.PrintTable(client)
}
func (b *Bucket) PrintTable(client *api.Client) error {
c := context.Background()
resp, err := client.ListBucket(c, b.Options)
if err != nil {
return fmt.Errorf(msg.ERROR_LIST_BUCKET, err)
}
listOut := output.ListOutput{}
listOut.Columns = []string{"NAME", "EDGE ACCESS"}
listOut.Out = b.Factory.IOStreams.Out
for _, v := range resp.Results {
ln := []string{
v.GetName(),
string(v.GetEdgeAccess()),
}
listOut.Lines = append(listOut.Lines, ln)
}
return output.Print(&listOut)
}
func (b *Bucket) AddFlags(flags *pflag.FlagSet) {
flags.Int64Var(&b.Options.Page, "page", 1, general.ApiListFlagPage)
flags.Int64Var(&b.Options.PageSize, "page-size", 10, general.ApiListFlagPageSize)
flags.BoolP("help", "h", false, msg.FLAG_HELP_LIST_BUCKET)
}