Skip to content

Commit 7de8978

Browse files
Merge pull request #1520 from sttts/sttts-cli-short
cli: promote '--short' option to be usable also in 'kubectl ws --short' syntax
2 parents b42b037 + d642357 commit 7de8978

File tree

4 files changed

+51
-21
lines changed

4 files changed

+51
-21
lines changed

pkg/cliplugins/workspace/cmd/cmd.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,6 @@ func New(streams genericclioptions.IOStreams) (*cobra.Command, error) {
111111
},
112112
}
113113

114-
var shortWorkspaceOutput bool
115114
currentCmd := &cobra.Command{
116115
Use: "current [--short]",
117116
Short: "Print the current workspace. Same as 'kubectl ws .'.",
@@ -129,10 +128,9 @@ func New(streams genericclioptions.IOStreams) (*cobra.Command, error) {
129128
if len(args) != 0 {
130129
return cmd.Help()
131130
}
132-
return kubeconfig.CurrentWorkspace(c.Context(), shortWorkspaceOutput)
131+
return kubeconfig.CurrentWorkspace(c.Context())
133132
},
134133
}
135-
currentCmd.Flags().BoolVar(&shortWorkspaceOutput, "short", shortWorkspaceOutput, "Print only the name of the workspace, e.g. for integration into the shell prompt")
136134

137135
listCmd := &cobra.Command{
138136
Use: "list",

pkg/cliplugins/workspace/plugin/kubeconfig.go

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,10 @@ const (
5353
// and allows modifications on it through workspace-related
5454
// actions
5555
type KubeConfig struct {
56-
startingConfig *clientcmdapi.Config
57-
overrides *clientcmd.ConfigOverrides
58-
currentContext string // including override
56+
startingConfig *clientcmdapi.Config
57+
overrides *clientcmd.ConfigOverrides
58+
currentContext string // including override
59+
shortWorkspaceOutput bool
5960

6061
clusterClient tenancyclient.ClusterInterface
6162
personalClient tenancyclient.ClusterInterface
@@ -97,9 +98,10 @@ func NewKubeConfig(opts *Options) (*KubeConfig, error) {
9798
}
9899

99100
return &KubeConfig{
100-
startingConfig: startingConfig,
101-
overrides: opts.KubectlOverrides,
102-
currentContext: currentContext,
101+
startingConfig: startingConfig,
102+
overrides: opts.KubectlOverrides,
103+
currentContext: currentContext,
104+
shortWorkspaceOutput: opts.ShortWorkspaceOutput,
103105

104106
clusterClient: clusterClient,
105107
personalClient: &personalClusterClient{clusterConfig},
@@ -160,7 +162,7 @@ func (kc *KubeConfig) UseWorkspace(ctx context.Context, name string) (err error)
160162
return err
161163
}
162164

163-
return kc.currentWorkspace(ctx, newKubeConfig.Clusters[newKubeConfig.Contexts[kcpCurrentWorkspaceContextKey].Cluster].Server, nil, false)
165+
return kc.currentWorkspace(ctx, newKubeConfig.Clusters[newKubeConfig.Contexts[kcpCurrentWorkspaceContextKey].Cluster].Server, nil)
164166

165167
case "..":
166168
config, err := clientcmd.NewDefaultClientConfig(*kc.startingConfig, kc.overrides).ClientConfig()
@@ -197,7 +199,7 @@ func (kc *KubeConfig) UseWorkspace(ctx context.Context, name string) (err error)
197199
newServerHost = homeWorkspace.Status.URL
198200

199201
case ".":
200-
return kc.CurrentWorkspace(ctx, false)
202+
return kc.CurrentWorkspace(ctx)
201203

202204
default:
203205
cluster := logicalcluster.New(name)
@@ -268,30 +270,30 @@ func (kc *KubeConfig) UseWorkspace(ctx context.Context, name string) (err error)
268270
return err
269271
}
270272

271-
return kc.currentWorkspace(ctx, newServerHost, workspaceType, false)
273+
return kc.currentWorkspace(ctx, newServerHost, workspaceType)
272274
}
273275

274276
// CurrentWorkspace outputs the current workspace.
275-
func (kc *KubeConfig) CurrentWorkspace(ctx context.Context, shortWorkspaceOutput bool) error {
277+
func (kc *KubeConfig) CurrentWorkspace(ctx context.Context) error {
276278
config, err := clientcmd.NewDefaultClientConfig(*kc.startingConfig, kc.overrides).ClientConfig()
277279
if err != nil {
278280
return err
279281
}
280282

281-
return kc.currentWorkspace(ctx, config.Host, nil, shortWorkspaceOutput)
283+
return kc.currentWorkspace(ctx, config.Host, nil)
282284
}
283285

284-
func (kc *KubeConfig) currentWorkspace(ctx context.Context, host string, workspaceType *tenancyv1alpha1.ClusterWorkspaceTypeReference, shortWorkspaceOutput bool) error {
286+
func (kc *KubeConfig) currentWorkspace(ctx context.Context, host string, workspaceType *tenancyv1alpha1.ClusterWorkspaceTypeReference) error {
285287
_, clusterName, err := pluginhelpers.ParseClusterURL(host)
286288
if err != nil {
287-
if shortWorkspaceOutput {
289+
if kc.shortWorkspaceOutput {
288290
return nil
289291
}
290292
_, err = fmt.Fprintf(kc.Out, "Current workspace is the URL %q.\n", host)
291293
return err
292294
}
293295

294-
if shortWorkspaceOutput {
296+
if kc.shortWorkspaceOutput {
295297
fmt.Fprintf(kc.Out, "%s\n", clusterName) // nolint: errcheck
296298
return nil
297299
}

pkg/cliplugins/workspace/plugin/kubeconfig_test.go

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,7 @@ func TestUse(t *testing.T) {
242242
existingObjects map[logicalcluster.Name][]string
243243
prettyNames map[logicalcluster.Name]map[string]string
244244
unready map[logicalcluster.Name]map[string]bool // unready workspaces
245+
short bool
245246

246247
param string
247248

@@ -324,6 +325,31 @@ func TestUse(t *testing.T) {
324325
},
325326
wantStdout: []string{"Current workspace is \"root:foo:bar\""},
326327
},
328+
{
329+
name: "workspace name, short output",
330+
config: clientcmdapi.Config{CurrentContext: "workspace.kcp.dev/current",
331+
Contexts: map[string]*clientcmdapi.Context{"workspace.kcp.dev/current": {Cluster: "workspace.kcp.dev/current", AuthInfo: "test"}},
332+
Clusters: map[string]*clientcmdapi.Cluster{"workspace.kcp.dev/current": {Server: "https://test/clusters/root:foo"}},
333+
AuthInfos: map[string]*clientcmdapi.AuthInfo{"test": {Token: "test"}},
334+
},
335+
existingObjects: map[logicalcluster.Name][]string{
336+
logicalcluster.New("root:foo"): {"bar"},
337+
},
338+
param: "bar",
339+
short: true,
340+
expected: &clientcmdapi.Config{CurrentContext: "workspace.kcp.dev/current",
341+
Contexts: map[string]*clientcmdapi.Context{
342+
"workspace.kcp.dev/current": {Cluster: "workspace.kcp.dev/current", AuthInfo: "test"},
343+
"workspace.kcp.dev/previous": {Cluster: "workspace.kcp.dev/previous", AuthInfo: "test"},
344+
},
345+
Clusters: map[string]*clientcmdapi.Cluster{
346+
"workspace.kcp.dev/current": {Server: "https://test/clusters/root:foo:bar"},
347+
"workspace.kcp.dev/previous": {Server: "https://test/clusters/root:foo"},
348+
},
349+
AuthInfos: map[string]*clientcmdapi.AuthInfo{"test": {Token: "test"}},
350+
},
351+
wantStdout: []string{"root:foo:bar"},
352+
},
327353
{
328354
name: "workspace name, no cluster URL",
329355
config: clientcmdapi.Config{CurrentContext: "workspace.kcp.dev/current",
@@ -765,8 +791,9 @@ func TestUse(t *testing.T) {
765791
streams, _, stdout, stderr := genericclioptions.NewTestIOStreams()
766792

767793
kc := &KubeConfig{
768-
startingConfig: tt.config.DeepCopy(),
769-
currentContext: tt.config.CurrentContext,
794+
startingConfig: tt.config.DeepCopy(),
795+
currentContext: tt.config.CurrentContext,
796+
shortWorkspaceOutput: tt.short,
770797

771798
clusterClient: fakeTenancyClient{
772799
t: t,

pkg/cliplugins/workspace/plugin/options.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,9 @@ import (
2828
// Options provides options that will drive the update of the current context
2929
// on a user's KUBECONFIG based on actions done on KCP workspaces
3030
type Options struct {
31-
KubectlOverrides *clientcmd.ConfigOverrides
32-
Scope string
31+
KubectlOverrides *clientcmd.ConfigOverrides
32+
Scope string
33+
ShortWorkspaceOutput bool
3334

3435
genericclioptions.IOStreams
3536
}
@@ -63,6 +64,8 @@ func (o *Options) BindFlags(cmd *cobra.Command) {
6364

6465
cmd.PersistentFlags().StringVar(&o.Scope, "scope", o.Scope, `The 'personal' scope shows only the workspaces you personally own, with the name you gave them at creation.
6566
The 'all' scope returns all the workspaces you are allowed to see in the organization, with the disambiguated names they have inside the whole organization.`)
67+
68+
cmd.PersistentFlags().BoolVar(&o.ShortWorkspaceOutput, "short", o.ShortWorkspaceOutput, "Print only the name of the workspace, e.g. for integration into the shell prompt")
6669
}
6770

6871
func (o *Options) Validate() error {

0 commit comments

Comments
 (0)