Skip to content

Commit f0aa643

Browse files
author
paul
committed
fix flag selection
1 parent 8c0bcb8 commit f0aa643

File tree

2 files changed

+22
-54
lines changed

2 files changed

+22
-54
lines changed

pkg/cli/prerun/prerun.go

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ type LoadNamespaceListMode string
2525

2626
const (
2727
TemporarySetNamespace LoadNamespaceListMode = ""
28-
RunNamespaceSelectionAndPersist LoadNamespaceListMode = "don't load namespaces"
28+
RunNamespaceSelectionAndPersist LoadNamespaceListMode = "run namespace selection and persist"
2929
)
3030

3131
func (mode LoadNamespaceListMode) String() string {
@@ -136,31 +136,42 @@ func PreRun(ctx *context.Context, optional ...Config) error {
136136
if !ok {
137137
return chkitErrors.FatalString("you have no namespaces")
138138
}
139+
ctx.SetNamespace(context.NamespaceFromModel(ns))
139140
case "":
140141
if nsList.Len() == 0 {
141142
return chkitErrors.FatalString("you have no namespaces")
142143
}
143144
(&activekit.Menu{
144145
Items: activekit.StringSelector(nsList.OwnersAndLabels(), func(s string) error {
145146
ns, _ = nsList.GetByUserFriendlyID(s)
147+
ctx.SetNamespace(context.NamespaceFromModel(ns))
146148
return nil
147149
}),
148150
}).Run()
149151
default:
150152
var tokens = str.SplitS(config.Namespace, "/", 2).Map(strings.TrimSpace)
151-
if !ctx.GetNamespace().Match(tokens.GetDefault(0, ""), tokens.GetDefault(1, "")) {
153+
var owner, label string
154+
if tokens.Len() == 2 {
155+
owner, label = tokens[0], tokens[1]
156+
} else {
157+
label = tokens[0]
158+
}
159+
logger.Debugf("owner=%q label=%q", owner, label)
160+
if !ctx.GetNamespace().Match(owner, label) || ctx.GetNamespace().IsEmpty() {
161+
logger.Debugf("getting namespace list")
152162
var nsList, err = ctx.Client.GetNamespaceList()
153163
if err != nil {
154164
return chkitErrors.Fatal(err)
155165
}
166+
logger.Debugf("searching namespace %q", tokens.Join("/"))
156167
var ns, ok = nsList.GetByUserFriendlyID(tokens.Join("/"))
157168
if !ok {
158-
return chkitErrors.FatalString("you have no namespaces")
169+
return chkitErrors.FatalString("namespace %s not found", tokens.Join("/"))
159170
}
160-
ctx.SetTemporaryNamespace(ns)
171+
logger.Debugf("%v", ns.OwnerAndLabel())
172+
ctx.SetNamespace(context.NamespaceFromModel(ns))
161173
}
162174
}
163-
ctx.SetNamespace(context.NamespaceFromModel(ns))
164175
case TemporarySetNamespace:
165176
var tokens = str.SplitS(config.Namespace, "/", 2).Map(strings.TrimSpace)
166177
if config.Namespace != "" && !ctx.GetNamespace().Match(tokens.GetDefault(0, ""), tokens.GetDefault(1, "")) {

pkg/cli/set/defaultns.go

Lines changed: 6 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@ import (
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

Comments
 (0)