From f1fcf31de6482f9cb3b699383178909f9f5c1020 Mon Sep 17 00:00:00 2001 From: Steven Masley Date: Thu, 22 Feb 2024 19:44:56 -0600 Subject: [PATCH 1/4] feat: implement global flag for org selection Any command can use '-z' to override org context --- cli/organization_test.go | 31 +++++++++ cli/root.go | 70 ++++++++++++--------- cli/testdata/coder_--help.golden | 4 ++ docs/cli.md | 10 +++ enterprise/cli/testdata/coder_--help.golden | 4 ++ 5 files changed, 89 insertions(+), 30 deletions(-) diff --git a/cli/organization_test.go b/cli/organization_test.go index 658498883ece8..1e9ce206e6bfb 100644 --- a/cli/organization_test.go +++ b/cli/organization_test.go @@ -42,4 +42,35 @@ func TestCurrentOrganization(t *testing.T) { require.NoError(t, <-errC) pty.ExpectMatch(first.OrganizationID.String()) }) + + t.Run("UsingFlag", func(t *testing.T) { + t.Parallel() + ownerClient := coderdtest.New(t, nil) + first := coderdtest.CreateFirstUser(t, ownerClient) + // Owner is required to make orgs + client, _ := coderdtest.CreateAnotherUser(t, ownerClient, first.OrganizationID, rbac.RoleOwner()) + + ctx := testutil.Context(t, testutil.WaitMedium) + orgs := map[string]codersdk.Organization{ + "foo": {}, + "bar": {}, + } + for orgName := range orgs { + org, err := client.CreateOrganization(ctx, codersdk.CreateOrganizationRequest{ + Name: orgName, + }) + require.NoError(t, err) + orgs[orgName] = org + } + + inv, root := clitest.New(t, "organizations", "current", "--only-id", "-z=bar") + clitest.SetupConfig(t, client, root) + pty := ptytest.New(t).Attach(inv) + errC := make(chan error) + go func() { + errC <- inv.Run() + }() + require.NoError(t, <-errC) + pty.ExpectMatch(orgs["bar"].ID.String()) + }) } diff --git a/cli/root.go b/cli/root.go index 4d78575c75c1e..3f97937cd8dcf 100644 --- a/cli/root.go +++ b/cli/root.go @@ -51,20 +51,21 @@ var ( ) const ( - varURL = "url" - varToken = "token" - varAgentToken = "agent-token" - varAgentTokenFile = "agent-token-file" - varAgentURL = "agent-url" - varHeader = "header" - varHeaderCommand = "header-command" - varNoOpen = "no-open" - varNoVersionCheck = "no-version-warning" - varNoFeatureWarning = "no-feature-warning" - varForceTty = "force-tty" - varVerbose = "verbose" - varDisableDirect = "disable-direct-connections" - notLoggedInMessage = "You are not logged in. Try logging in using 'coder login '." + varURL = "url" + varToken = "token" + varAgentToken = "agent-token" + varAgentTokenFile = "agent-token-file" + varAgentURL = "agent-url" + varHeader = "header" + varHeaderCommand = "header-command" + varNoOpen = "no-open" + varNoVersionCheck = "no-version-warning" + varNoFeatureWarning = "no-feature-warning" + varForceTty = "force-tty" + varVerbose = "verbose" + varOrganizationSelect = "organization" + varDisableDirect = "disable-direct-connections" + notLoggedInMessage = "You are not logged in. Try logging in using 'coder login '." envNoVersionCheck = "CODER_NO_VERSION_WARNING" envNoFeatureWarning = "CODER_NO_FEATURE_WARNING" @@ -434,6 +435,14 @@ func (r *RootCmd) Command(subcommands []*clibase.Cmd) (*clibase.Cmd, error) { Value: clibase.StringOf(&r.globalConfig), Group: globalGroup, }, + { + Flag: varOrganizationSelect, + FlagShorthand: "z", + Env: "CODER_ORGANIZATION", + Description: "Select which organization (uuid or name) to use This overrides what is present in the config file.", + Value: clibase.StringOf(&r.organizationSelect), + Group: globalGroup, + }, { Flag: "version", // This was requested by a customer to assist with their migration. @@ -455,20 +464,21 @@ func (r *RootCmd) Command(subcommands []*clibase.Cmd) (*clibase.Cmd, error) { // RootCmd contains parameters and helpers useful to all commands. type RootCmd struct { - clientURL *url.URL - token string - globalConfig string - header []string - headerCommand string - agentToken string - agentTokenFile string - agentURL *url.URL - forceTTY bool - noOpen bool - verbose bool - versionFlag bool - disableDirect bool - debugHTTP bool + clientURL *url.URL + token string + globalConfig string + header []string + headerCommand string + agentToken string + agentTokenFile string + agentURL *url.URL + forceTTY bool + noOpen bool + verbose bool + organizationSelect string + versionFlag bool + disableDirect bool + debugHTTP bool noVersionCheck bool noFeatureWarning bool @@ -701,8 +711,8 @@ func (r *RootCmd) createAgentClient() (*agentsdk.Client, error) { // CurrentOrganization returns the currently active organization for the authenticated user. func CurrentOrganization(r *RootCmd, inv *clibase.Invocation, client *codersdk.Client) (codersdk.Organization, error) { conf := r.createConfig() - selected := "" - if conf.Organization().Exists() { + selected := r.organizationSelect + if selected == "" && conf.Organization().Exists() { org, err := conf.Organization().Read() if err != nil { return codersdk.Organization{}, fmt.Errorf("read selected organization from config file %q: %w", conf.Organization(), err) diff --git a/cli/testdata/coder_--help.golden b/cli/testdata/coder_--help.golden index 501a336915128..7161dd065e4e5 100644 --- a/cli/testdata/coder_--help.golden +++ b/cli/testdata/coder_--help.golden @@ -82,6 +82,10 @@ variables or flags. --no-version-warning bool, $CODER_NO_VERSION_WARNING Suppress warning when client and server versions do not match. + -z, --organization string, $CODER_ORGANIZATION + Select which organization (uuid or name) to use as default. This + overrides what is present in the config file. + --token string, $CODER_SESSION_TOKEN Specify an authentication token. For security reasons setting CODER_SESSION_TOKEN is preferred. diff --git a/docs/cli.md b/docs/cli.md index 8e1d2c56c74c9..7eedd3eb67ba8 100644 --- a/docs/cli.md +++ b/docs/cli.md @@ -130,6 +130,16 @@ Suppress warnings about unlicensed features. Suppress warning when client and server versions do not match. +### -z, --organization + +| | | +| ----------- | -------------------------------- | +| Type | string | +| Environment | $CODER_ORGANIZATION | + +Select which organization (uuid or name) to use as default. This overrides what +is present in the config file. + ### --token | | | diff --git a/enterprise/cli/testdata/coder_--help.golden b/enterprise/cli/testdata/coder_--help.golden index 7c2ff5c835dff..32410c3013ba7 100644 --- a/enterprise/cli/testdata/coder_--help.golden +++ b/enterprise/cli/testdata/coder_--help.golden @@ -48,6 +48,10 @@ variables or flags. --no-version-warning bool, $CODER_NO_VERSION_WARNING Suppress warning when client and server versions do not match. + -z, --organization string, $CODER_ORGANIZATION + Select which organization (uuid or name) to use as default. This + overrides what is present in the config file. + --token string, $CODER_SESSION_TOKEN Specify an authentication token. For security reasons setting CODER_SESSION_TOKEN is preferred. From 1e340384e0a5d02ae33631245f9fde68f66eb114 Mon Sep 17 00:00:00 2001 From: Steven Masley Date: Mon, 26 Feb 2024 10:30:16 -0600 Subject: [PATCH 2/4] make gen --- docs/cli.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/cli.md b/docs/cli.md index 7eedd3eb67ba8..def4fc62bce19 100644 --- a/docs/cli.md +++ b/docs/cli.md @@ -137,8 +137,8 @@ Suppress warning when client and server versions do not match. | Type | string | | Environment | $CODER_ORGANIZATION | -Select which organization (uuid or name) to use as default. This overrides what -is present in the config file. +Select which organization (uuid or name) to use This overrides what is present +in the config file. ### --token From 7f3b17fe89ec6f666702a6c08d533b0aa82f4c9f Mon Sep 17 00:00:00 2001 From: Steven Masley Date: Mon, 26 Feb 2024 10:34:59 -0600 Subject: [PATCH 3/4] fix test --- cli/organization_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cli/organization_test.go b/cli/organization_test.go index 1e9ce206e6bfb..91ecc166d5dcc 100644 --- a/cli/organization_test.go +++ b/cli/organization_test.go @@ -63,7 +63,7 @@ func TestCurrentOrganization(t *testing.T) { orgs[orgName] = org } - inv, root := clitest.New(t, "organizations", "current", "--only-id", "-z=bar") + inv, root := clitest.New(t, "organizations", "show", "current", "--only-id", "-z=bar") clitest.SetupConfig(t, client, root) pty := ptytest.New(t).Attach(inv) errC := make(chan error) From 26aa8f87510e44cf316004221f2ca0e542dfe783 Mon Sep 17 00:00:00 2001 From: Steven Masley Date: Mon, 26 Feb 2024 10:57:24 -0600 Subject: [PATCH 4/4] update golden files --- cli/testdata/coder_--help.golden | 4 ++-- enterprise/cli/testdata/coder_--help.golden | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/cli/testdata/coder_--help.golden b/cli/testdata/coder_--help.golden index 7161dd065e4e5..fc0785ee92814 100644 --- a/cli/testdata/coder_--help.golden +++ b/cli/testdata/coder_--help.golden @@ -83,8 +83,8 @@ variables or flags. Suppress warning when client and server versions do not match. -z, --organization string, $CODER_ORGANIZATION - Select which organization (uuid or name) to use as default. This - overrides what is present in the config file. + Select which organization (uuid or name) to use This overrides what is + present in the config file. --token string, $CODER_SESSION_TOKEN Specify an authentication token. For security reasons setting diff --git a/enterprise/cli/testdata/coder_--help.golden b/enterprise/cli/testdata/coder_--help.golden index 32410c3013ba7..c8612392972f0 100644 --- a/enterprise/cli/testdata/coder_--help.golden +++ b/enterprise/cli/testdata/coder_--help.golden @@ -49,8 +49,8 @@ variables or flags. Suppress warning when client and server versions do not match. -z, --organization string, $CODER_ORGANIZATION - Select which organization (uuid or name) to use as default. This - overrides what is present in the config file. + Select which organization (uuid or name) to use This overrides what is + present in the config file. --token string, $CODER_SESSION_TOKEN Specify an authentication token. For security reasons setting