55
66 "github.com/containerum/chkit/pkg/cli/prerun"
77 "github.com/containerum/chkit/pkg/context"
8- "github.com/containerum/chkit/pkg/model/namespace"
98 "github.com/containerum/chkit/pkg/util/activekit"
10- "github.com/containerum/chkit/pkg/util/ferr"
119 "github.com/spf13/cobra"
1210)
1311
@@ -17,58 +15,17 @@ func DefaultNamespace(ctx *context.Context) *cobra.Command {
1715 Short : "Set default namespace" ,
1816 Aliases : []string {"def-ns" , "default-ns" , "defns" , "def-namespace" },
1917 PersistentPreRun : func (cmd * cobra.Command , args []string ) {
20- if err := prerun .PreRun (ctx ); err != nil {
18+ var ns , _ = cmd .Flags ().GetString ("namespace" )
19+ if err := prerun .PreRun (ctx , prerun.Config {
20+ NamespaceSelection : prerun .RunNamespaceSelectionAndPersist ,
21+ Namespace : ns ,
22+ }); err != nil {
2123 activekit .Attention (err .Error ())
2224 ctx .Exit (1 )
2325 }
2426 },
2527 Run : func (cmd * cobra.Command , args []string ) {
26- if len (args ) == 1 {
27- nsList , err := ctx .Client .GetNamespaceList ()
28- if err != nil {
29- ferr .Println (err )
30- ctx .Exit (1 )
31- }
32- var ns , ok = nsList .GetByUserFriendlyID (args [0 ])
33- if ! ok {
34- fmt .Printf ("Namespace %q not found!\n " , args [0 ])
35- ctx .Exit (1 )
36- }
37- ctx .SetNamespace (context .NamespaceFromModel (ns ))
38- fmt .Printf ("Using %q as default namespace!\n " , ctx .GetNamespace ())
39- ctx .Changed = true
40- return
41- }
42- nsList , err := ctx .Client .GetNamespaceList ()
43- if err != nil || len (nsList ) == 0 {
44- fmt .Printf ("You have no namespaces :(\n " )
45- }
46- var menu []* activekit.MenuItem
47- for _ , ns := range nsList {
48- menu = append (menu , & activekit.MenuItem {
49- Label : ns .LabelAndID (),
50- Action : func (ns namespace.Namespace ) func () error {
51- return func () error {
52- ctx .SetNamespace (context .NamespaceFromModel (ns ))
53- fmt .Printf ("Using %q as default namespace\n " , ns .LabelAndID ())
54- return nil
55- }
56- }(ns ),
57- })
58- }
59- menu = append (menu , & activekit.MenuItem {
60- Label : "Exit" ,
61- })
62- var title string
63- if ctx .GetNamespace ().IsEmpty () {
64- title = fmt .Sprintf ("Default namespace isn't defined" )
65- } else {
66- title = fmt .Sprintf ("%q is current default namespace" , ctx .GetNamespace ())
67- }
68- (& activekit.Menu {
69- Title : title ,
70- Items : menu ,
71- }).Run ()
28+ fmt .Printf ("Using %q as default namespace" , ctx .GetNamespace ())
7229 },
7330 }
7431
0 commit comments