-
Notifications
You must be signed in to change notification settings - Fork 34
/
namespace.go
141 lines (116 loc) · 2.97 KB
/
namespace.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
package cmd
import (
"fmt"
"os"
"github.com/MakeNowJust/heredoc"
frontierv1beta1 "github.com/raystack/frontier/proto/v1beta1"
"github.com/raystack/salt/printer"
cli "github.com/spf13/cobra"
)
func NamespaceCommand(cliConfig *Config) *cli.Command {
cmd := &cli.Command{
Use: "namespace",
Aliases: []string{"namespaces"},
Short: "Manage namespaces",
Long: heredoc.Doc(`
Work with namespaces.
`),
Example: heredoc.Doc(`
$ frontier namespace list
$ frontier namespace view
`),
Annotations: map[string]string{
"group": "core",
"client": "true",
},
}
cmd.AddCommand(listNamespaceCommand(cliConfig))
cmd.AddCommand(viewNamespaceCommand(cliConfig))
bindFlagsFromClientConfig(cmd)
return cmd
}
func viewNamespaceCommand(cliConfig *Config) *cli.Command {
cmd := &cli.Command{
Use: "view",
Short: "View a namespace",
Args: cli.ExactArgs(1),
Example: heredoc.Doc(`
$ frontier namespace view <namespace-id>
`),
Annotations: map[string]string{
"group": "core",
},
RunE: func(cmd *cli.Command, args []string) error {
spinner := printer.Spin("")
defer spinner.Stop()
client, cancel, err := createClient(cmd.Context(), cliConfig.Host)
if err != nil {
return err
}
defer cancel()
namespaceID := args[0]
res, err := client.GetNamespace(cmd.Context(), &frontierv1beta1.GetNamespaceRequest{
Id: namespaceID,
})
if err != nil {
return err
}
report := [][]string{}
namespace := res.GetNamespace()
spinner.Stop()
report = append(report, []string{"ID", "NAME", "CREATED AT", "UPDATED AT"})
report = append(report, []string{
namespace.GetId(),
namespace.GetName(),
namespace.GetCreatedAt().AsTime().String(),
namespace.GetUpdatedAt().AsTime().String(),
})
printer.Table(os.Stdout, report)
spinner.Stop()
return nil
},
}
return cmd
}
func listNamespaceCommand(cliConfig *Config) *cli.Command {
cmd := &cli.Command{
Use: "list",
Short: "List all namespaces",
Args: cli.NoArgs,
Example: heredoc.Doc(`
$ frontier namespace list
`),
Annotations: map[string]string{
"group": "core",
},
RunE: func(cmd *cli.Command, args []string) error {
spinner := printer.Spin("")
defer spinner.Stop()
client, cancel, err := createClient(cmd.Context(), cliConfig.Host)
if err != nil {
return err
}
defer cancel()
res, err := client.ListNamespaces(cmd.Context(), &frontierv1beta1.ListNamespacesRequest{})
if err != nil {
return err
}
report := [][]string{}
namespaces := res.GetNamespaces()
spinner.Stop()
fmt.Printf(" \nShowing %d namespaces\n \n", len(namespaces))
report = append(report, []string{"ID", "NAME", "CREATED AT", "UPDATED AT"})
for _, n := range namespaces {
report = append(report, []string{
n.GetId(),
n.GetName(),
n.GetCreatedAt().AsTime().String(),
n.GetUpdatedAt().AsTime().String(),
})
}
printer.Table(os.Stdout, report)
return nil
},
}
return cmd
}