This repository has been archived by the owner on Aug 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
namespace.go
122 lines (103 loc) · 2.96 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
package main
import (
"fmt"
"time"
"github.com/kraudcloud/cli/completions"
"github.com/spf13/cobra"
)
func namespacesCMD() *cobra.Command {
c := &cobra.Command{
Use: "namespaces",
Aliases: []string{"ns", "namespace"},
Short: "Manage namespaces",
}
c.AddCommand(
namespacesListCMD(),
namespacesDeleteCMD(),
namespacesInspectCMD(),
namespacesCreateCMD(),
)
return c
}
func namespacesListCMD() *cobra.Command {
c := &cobra.Command{
Use: "list",
Short: "List namespaces",
Aliases: []string{"ls"},
RunE: func(cmd *cobra.Command, args []string) error {
ns, err := API().ListNamespaces(cmd.Context())
if err != nil {
fmt.Fprintf(cmd.ErrOrStderr(), "error listing namespaces: %v\n", err)
return nil
}
t := NewTable("NAME", "CREATED")
for _, n := range ns.Items {
t.AddRow(*n.Metadata.Name, time.Time(*n.Metadata.CreationTimestamp).Format(time.DateTime))
}
t.Print()
return nil
},
}
return c
}
func namespacesDeleteCMD() *cobra.Command {
force := false
c := &cobra.Command{
Use: "delete <namespace>",
Aliases: []string{"del", "rm"},
Short: "delete a namespace and all of its deployments",
Args: cobra.ExactArgs(1),
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return completions.NamespaceOptions(API(), cmd, args, toComplete)
},
RunE: func(cmd *cobra.Command, args []string) error {
err := API().DeleteNamespace(cmd.Context(), args[0], force)
if err != nil {
fmt.Fprintf(cmd.ErrOrStderr(), "error deleting namespace: %v\n", err)
return nil
}
fmt.Fprintf(cmd.OutOrStdout(), "namespace %q deleted\n", args[0])
return nil
},
}
c.Flags().BoolVar(&force, "force", false, "force delete")
return c
}
func namespacesInspectCMD() *cobra.Command {
c := &cobra.Command{
Use: "inspect <namespace>",
Aliases: []string{"in", "show"},
Short: "Inspect a namespace",
Args: cobra.ExactArgs(1),
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return completions.NamespaceOptions(API(), cmd, args, toComplete)
},
RunE: func(cmd *cobra.Command, args []string) error {
ns, err := API().NamespaceOverview(cmd.Context(), args[0])
if err != nil {
fmt.Fprintf(cmd.ErrOrStderr(), "error inspecting namespace: %v\n", err)
return nil
}
return identJSONEncoder(cmd.OutOrStdout(), ns)
},
}
return c
}
func namespacesCreateCMD() *cobra.Command {
c := &cobra.Command{
Use: "create <namespace>",
Aliases: []string{"new", "add", "mk"},
Short: "Create a namespace",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
ns, err := API().CreateNamespace(cmd.Context(), args[0])
if err != nil {
fmt.Fprintf(cmd.ErrOrStderr(), "error creating namespace: %v\n", err)
return nil
}
fmt.Fprintf(cmd.OutOrStdout(), "%s\n", *ns.ID)
return nil
},
}
return c
}