Skip to content

Commit 748cf4b

Browse files
authored
feat: implement global flag for org selection (#12276)
* feat: implement global flag for org selection Any command can use '-z' to override org context
1 parent 5a41385 commit 748cf4b

File tree

5 files changed

+89
-30
lines changed

5 files changed

+89
-30
lines changed

cli/organization_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,35 @@ func TestCurrentOrganization(t *testing.T) {
4242
require.NoError(t, <-errC)
4343
pty.ExpectMatch(first.OrganizationID.String())
4444
})
45+
46+
t.Run("UsingFlag", func(t *testing.T) {
47+
t.Parallel()
48+
ownerClient := coderdtest.New(t, nil)
49+
first := coderdtest.CreateFirstUser(t, ownerClient)
50+
// Owner is required to make orgs
51+
client, _ := coderdtest.CreateAnotherUser(t, ownerClient, first.OrganizationID, rbac.RoleOwner())
52+
53+
ctx := testutil.Context(t, testutil.WaitMedium)
54+
orgs := map[string]codersdk.Organization{
55+
"foo": {},
56+
"bar": {},
57+
}
58+
for orgName := range orgs {
59+
org, err := client.CreateOrganization(ctx, codersdk.CreateOrganizationRequest{
60+
Name: orgName,
61+
})
62+
require.NoError(t, err)
63+
orgs[orgName] = org
64+
}
65+
66+
inv, root := clitest.New(t, "organizations", "show", "current", "--only-id", "-z=bar")
67+
clitest.SetupConfig(t, client, root)
68+
pty := ptytest.New(t).Attach(inv)
69+
errC := make(chan error)
70+
go func() {
71+
errC <- inv.Run()
72+
}()
73+
require.NoError(t, <-errC)
74+
pty.ExpectMatch(orgs["bar"].ID.String())
75+
})
4576
}

cli/root.go

Lines changed: 40 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -51,20 +51,21 @@ var (
5151
)
5252

5353
const (
54-
varURL = "url"
55-
varToken = "token"
56-
varAgentToken = "agent-token"
57-
varAgentTokenFile = "agent-token-file"
58-
varAgentURL = "agent-url"
59-
varHeader = "header"
60-
varHeaderCommand = "header-command"
61-
varNoOpen = "no-open"
62-
varNoVersionCheck = "no-version-warning"
63-
varNoFeatureWarning = "no-feature-warning"
64-
varForceTty = "force-tty"
65-
varVerbose = "verbose"
66-
varDisableDirect = "disable-direct-connections"
67-
notLoggedInMessage = "You are not logged in. Try logging in using 'coder login <url>'."
54+
varURL = "url"
55+
varToken = "token"
56+
varAgentToken = "agent-token"
57+
varAgentTokenFile = "agent-token-file"
58+
varAgentURL = "agent-url"
59+
varHeader = "header"
60+
varHeaderCommand = "header-command"
61+
varNoOpen = "no-open"
62+
varNoVersionCheck = "no-version-warning"
63+
varNoFeatureWarning = "no-feature-warning"
64+
varForceTty = "force-tty"
65+
varVerbose = "verbose"
66+
varOrganizationSelect = "organization"
67+
varDisableDirect = "disable-direct-connections"
68+
notLoggedInMessage = "You are not logged in. Try logging in using 'coder login <url>'."
6869

6970
envNoVersionCheck = "CODER_NO_VERSION_WARNING"
7071
envNoFeatureWarning = "CODER_NO_FEATURE_WARNING"
@@ -434,6 +435,14 @@ func (r *RootCmd) Command(subcommands []*clibase.Cmd) (*clibase.Cmd, error) {
434435
Value: clibase.StringOf(&r.globalConfig),
435436
Group: globalGroup,
436437
},
438+
{
439+
Flag: varOrganizationSelect,
440+
FlagShorthand: "z",
441+
Env: "CODER_ORGANIZATION",
442+
Description: "Select which organization (uuid or name) to use This overrides what is present in the config file.",
443+
Value: clibase.StringOf(&r.organizationSelect),
444+
Group: globalGroup,
445+
},
437446
{
438447
Flag: "version",
439448
// 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) {
455464

456465
// RootCmd contains parameters and helpers useful to all commands.
457466
type RootCmd struct {
458-
clientURL *url.URL
459-
token string
460-
globalConfig string
461-
header []string
462-
headerCommand string
463-
agentToken string
464-
agentTokenFile string
465-
agentURL *url.URL
466-
forceTTY bool
467-
noOpen bool
468-
verbose bool
469-
versionFlag bool
470-
disableDirect bool
471-
debugHTTP bool
467+
clientURL *url.URL
468+
token string
469+
globalConfig string
470+
header []string
471+
headerCommand string
472+
agentToken string
473+
agentTokenFile string
474+
agentURL *url.URL
475+
forceTTY bool
476+
noOpen bool
477+
verbose bool
478+
organizationSelect string
479+
versionFlag bool
480+
disableDirect bool
481+
debugHTTP bool
472482

473483
noVersionCheck bool
474484
noFeatureWarning bool
@@ -701,8 +711,8 @@ func (r *RootCmd) createAgentClient() (*agentsdk.Client, error) {
701711
// CurrentOrganization returns the currently active organization for the authenticated user.
702712
func CurrentOrganization(r *RootCmd, inv *clibase.Invocation, client *codersdk.Client) (codersdk.Organization, error) {
703713
conf := r.createConfig()
704-
selected := ""
705-
if conf.Organization().Exists() {
714+
selected := r.organizationSelect
715+
if selected == "" && conf.Organization().Exists() {
706716
org, err := conf.Organization().Read()
707717
if err != nil {
708718
return codersdk.Organization{}, fmt.Errorf("read selected organization from config file %q: %w", conf.Organization(), err)

cli/testdata/coder_--help.golden

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,10 @@ variables or flags.
8282
--no-version-warning bool, $CODER_NO_VERSION_WARNING
8383
Suppress warning when client and server versions do not match.
8484

85+
-z, --organization string, $CODER_ORGANIZATION
86+
Select which organization (uuid or name) to use This overrides what is
87+
present in the config file.
88+
8589
--token string, $CODER_SESSION_TOKEN
8690
Specify an authentication token. For security reasons setting
8791
CODER_SESSION_TOKEN is preferred.

docs/cli.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,16 @@ Suppress warnings about unlicensed features.
130130

131131
Suppress warning when client and server versions do not match.
132132

133+
### -z, --organization
134+
135+
| | |
136+
| ----------- | -------------------------------- |
137+
| Type | <code>string</code> |
138+
| Environment | <code>$CODER_ORGANIZATION</code> |
139+
140+
Select which organization (uuid or name) to use This overrides what is present
141+
in the config file.
142+
133143
### --token
134144

135145
| | |

enterprise/cli/testdata/coder_--help.golden

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ variables or flags.
4848
--no-version-warning bool, $CODER_NO_VERSION_WARNING
4949
Suppress warning when client and server versions do not match.
5050

51+
-z, --organization string, $CODER_ORGANIZATION
52+
Select which organization (uuid or name) to use This overrides what is
53+
present in the config file.
54+
5155
--token string, $CODER_SESSION_TOKEN
5256
Specify an authentication token. For security reasons setting
5357
CODER_SESSION_TOKEN is preferred.

0 commit comments

Comments
 (0)