Skip to content

Commit a70278e

Browse files
authored
feat: make flags in one place (coder#4452)
1 parent b1a095e commit a70278e

File tree

10 files changed

+246
-201
lines changed

10 files changed

+246
-201
lines changed

cli/deployment/flags.go

Lines changed: 105 additions & 56 deletions
Large diffs are not rendered by default.

cli/deployment/flags_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package deployment_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/spf13/pflag"
7+
"github.com/stretchr/testify/require"
8+
9+
"github.com/coder/coder/cli/deployment"
10+
)
11+
12+
func TestFlags(t *testing.T) {
13+
t.Parallel()
14+
15+
df := deployment.Flags()
16+
fs := pflag.NewFlagSet("test", pflag.ContinueOnError)
17+
deployment.AttachFlags(fs, df, false)
18+
19+
require.NotNil(t, fs.Lookup("access-url"))
20+
require.False(t, fs.Lookup("access-url").Hidden)
21+
require.True(t, fs.Lookup("telemetry-url").Hidden)
22+
require.NotEmpty(t, fs.Lookup("telemetry-url").DefValue)
23+
require.Nil(t, fs.Lookup("audit-logging"))
24+
25+
df = deployment.Flags()
26+
fs = pflag.NewFlagSet("test-enterprise", pflag.ContinueOnError)
27+
deployment.AttachFlags(fs, df, true)
28+
29+
require.Nil(t, fs.Lookup("access-url"))
30+
require.NotNil(t, fs.Lookup("audit-logging"))
31+
require.Contains(t, fs.Lookup("audit-logging").Usage, "This is an Enterprise feature")
32+
}

cli/root.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,7 @@ func Core() []*cobra.Command {
9999
}
100100

101101
func AGPL() []*cobra.Command {
102-
df := deployment.Flags()
103-
all := append(Core(), Server(df, func(_ context.Context, o *coderd.Options) (*coderd.API, error) {
104-
o.DeploymentFlags = &df
102+
all := append(Core(), Server(deployment.Flags(), func(_ context.Context, o *coderd.Options) (*coderd.API, error) {
105103
return coderd.New(o), nil
106104
}))
107105
return all

cli/server.go

Lines changed: 3 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ import (
6767
)
6868

6969
// nolint:gocyclo
70-
func Server(dflags codersdk.DeploymentFlags, newAPI func(context.Context, *coderd.Options) (*coderd.API, error)) *cobra.Command {
70+
func Server(dflags *codersdk.DeploymentFlags, newAPI func(context.Context, *coderd.Options) (*coderd.API, error)) *cobra.Command {
7171
root := &cobra.Command{
7272
Use: "server",
7373
Short: "Start a Coder server",
@@ -318,7 +318,7 @@ func Server(dflags codersdk.DeploymentFlags, newAPI func(context.Context, *coder
318318
MetricsCacheRefreshInterval: dflags.MetricsCacheRefreshInterval.Value,
319319
AgentStatsRefreshInterval: dflags.AgentStatRefreshInterval.Value,
320320
Experimental: ExperimentalEnabled(cmd),
321-
DeploymentFlags: &dflags,
321+
DeploymentFlags: dflags,
322322
}
323323

324324
if dflags.OAuth2GithubClientSecret.Value != "" {
@@ -712,58 +712,7 @@ func Server(dflags codersdk.DeploymentFlags, newAPI func(context.Context, *coder
712712
},
713713
})
714714

715-
deployment.StringFlag(root.Flags(), &dflags.AccessURL)
716-
deployment.StringFlag(root.Flags(), &dflags.WildcardAccessURL)
717-
deployment.StringFlag(root.Flags(), &dflags.Address)
718-
deployment.DurationFlag(root.Flags(), &dflags.AutobuildPollInterval)
719-
_ = root.Flags().MarkHidden(dflags.AutobuildPollInterval.Flag)
720-
deployment.BoolFlag(root.Flags(), &dflags.DerpServerEnable)
721-
deployment.IntFlag(root.Flags(), &dflags.DerpServerRegionID)
722-
deployment.StringFlag(root.Flags(), &dflags.DerpServerRegionCode)
723-
deployment.StringFlag(root.Flags(), &dflags.DerpServerRegionName)
724-
deployment.StringArrayFlag(root.Flags(), &dflags.DerpServerSTUNAddresses)
725-
deployment.StringFlag(root.Flags(), &dflags.DerpConfigURL)
726-
deployment.StringFlag(root.Flags(), &dflags.DerpConfigPath)
727-
deployment.BoolFlag(root.Flags(), &dflags.PromEnabled)
728-
deployment.StringFlag(root.Flags(), &dflags.PromAddress)
729-
deployment.BoolFlag(root.Flags(), &dflags.PprofEnabled)
730-
deployment.StringFlag(root.Flags(), &dflags.CacheDir)
731-
deployment.BoolFlag(root.Flags(), &dflags.InMemoryDatabase)
732-
_ = root.Flags().MarkHidden(dflags.InMemoryDatabase.Flag)
733-
deployment.IntFlag(root.Flags(), &dflags.ProvisionerDaemonCount)
734-
deployment.StringFlag(root.Flags(), &dflags.PostgresURL)
735-
deployment.StringFlag(root.Flags(), &dflags.OAuth2GithubClientID)
736-
deployment.StringFlag(root.Flags(), &dflags.OAuth2GithubClientSecret)
737-
deployment.StringArrayFlag(root.Flags(), &dflags.OAuth2GithubAllowedOrganizations)
738-
deployment.StringArrayFlag(root.Flags(), &dflags.OAuth2GithubAllowedTeams)
739-
deployment.BoolFlag(root.Flags(), &dflags.OAuth2GithubAllowSignups)
740-
deployment.StringFlag(root.Flags(), &dflags.OAuth2GithubEnterpriseBaseURL)
741-
deployment.BoolFlag(root.Flags(), &dflags.OIDCAllowSignups)
742-
deployment.StringFlag(root.Flags(), &dflags.OIDCClientID)
743-
deployment.StringFlag(root.Flags(), &dflags.OIDCClientSecret)
744-
deployment.StringFlag(root.Flags(), &dflags.OIDCEmailDomain)
745-
deployment.StringFlag(root.Flags(), &dflags.OIDCIssuerURL)
746-
deployment.StringArrayFlag(root.Flags(), &dflags.OIDCScopes)
747-
deployment.BoolFlag(root.Flags(), &dflags.TelemetryEnable)
748-
deployment.BoolFlag(root.Flags(), &dflags.TelemetryTraceEnable)
749-
deployment.StringFlag(root.Flags(), &dflags.TelemetryURL)
750-
_ = root.Flags().MarkHidden(dflags.TelemetryURL.Flag)
751-
deployment.BoolFlag(root.Flags(), &dflags.TLSEnable)
752-
deployment.StringArrayFlag(root.Flags(), &dflags.TLSCertFiles)
753-
deployment.StringFlag(root.Flags(), &dflags.TLSClientCAFile)
754-
deployment.StringFlag(root.Flags(), &dflags.TLSClientAuth)
755-
deployment.StringArrayFlag(root.Flags(), &dflags.TLSKeyFiles)
756-
deployment.StringFlag(root.Flags(), &dflags.TLSMinVersion)
757-
deployment.BoolFlag(root.Flags(), &dflags.TraceEnable)
758-
deployment.BoolFlag(root.Flags(), &dflags.SecureAuthCookie)
759-
deployment.StringFlag(root.Flags(), &dflags.SSHKeygenAlgorithm)
760-
deployment.StringArrayFlag(root.Flags(), &dflags.AutoImportTemplates)
761-
_ = root.Flags().MarkHidden(dflags.AutoImportTemplates.Flag)
762-
deployment.DurationFlag(root.Flags(), &dflags.MetricsCacheRefreshInterval)
763-
_ = root.Flags().MarkHidden(dflags.MetricsCacheRefreshInterval.Flag)
764-
deployment.DurationFlag(root.Flags(), &dflags.AgentStatRefreshInterval)
765-
_ = root.Flags().MarkHidden(dflags.AgentStatRefreshInterval.Flag)
766-
deployment.BoolFlag(root.Flags(), &dflags.Verbose)
715+
deployment.AttachFlags(root.Flags(), dflags, false)
767716

768717
return root
769718
}

coderd/flags_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func TestDeploymentFlagSecrets(t *testing.T) {
3030
df.SCIMAuthHeader.Value = hi
3131

3232
client := coderdtest.New(t, &coderdtest.Options{
33-
DeploymentFlags: &df,
33+
DeploymentFlags: df,
3434
})
3535
_ = coderdtest.CreateFirstUser(t, client)
3636
scrubbed, err := client.DeploymentFlags(ctx)

codersdk/flags.go

Lines changed: 56 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -10,57 +10,57 @@ import (
1010
)
1111

1212
type DeploymentFlags struct {
13-
AccessURL StringFlag `json:"access_url"`
14-
WildcardAccessURL StringFlag `json:"wildcard_access_url"`
15-
Address StringFlag `json:"address"`
16-
AutobuildPollInterval DurationFlag `json:"autobuild_poll_interval"`
17-
DerpServerEnable BoolFlag `json:"derp_server_enabled"`
18-
DerpServerRegionID IntFlag `json:"derp_server_region_id"`
19-
DerpServerRegionCode StringFlag `json:"derp_server_region_code"`
20-
DerpServerRegionName StringFlag `json:"derp_server_region_name"`
21-
DerpServerSTUNAddresses StringArrayFlag `json:"derp_server_stun_address"`
22-
DerpConfigURL StringFlag `json:"derp_config_url"`
23-
DerpConfigPath StringFlag `json:"derp_config_path"`
24-
PromEnabled BoolFlag `json:"prom_enabled"`
25-
PromAddress StringFlag `json:"prom_address"`
26-
PprofEnabled BoolFlag `json:"pprof_enabled"`
27-
PprofAddress StringFlag `json:"pprof_address"`
28-
CacheDir StringFlag `json:"cache_dir"`
29-
InMemoryDatabase BoolFlag `json:"in_memory_database"`
30-
ProvisionerDaemonCount IntFlag `json:"provisioner_daemon_count"`
31-
PostgresURL StringFlag `json:"postgres_url"`
32-
OAuth2GithubClientID StringFlag `json:"oauth2_github_client_id"`
33-
OAuth2GithubClientSecret StringFlag `json:"oauth2_github_client_secret"`
34-
OAuth2GithubAllowedOrganizations StringArrayFlag `json:"oauth2_github_allowed_organizations"`
35-
OAuth2GithubAllowedTeams StringArrayFlag `json:"oauth2_github_allowed_teams"`
36-
OAuth2GithubAllowSignups BoolFlag `json:"oauth2_github_allow_signups"`
37-
OAuth2GithubEnterpriseBaseURL StringFlag `json:"oauth2_github_enterprise_base_url"`
38-
OIDCAllowSignups BoolFlag `json:"oidc_allow_signups"`
39-
OIDCClientID StringFlag `json:"oidc_client_id"`
40-
OIDCClientSecret StringFlag `json:"oidc_cliet_secret"`
41-
OIDCEmailDomain StringFlag `json:"oidc_email_domain"`
42-
OIDCIssuerURL StringFlag `json:"oidc_issuer_url"`
43-
OIDCScopes StringArrayFlag `json:"oidc_scopes"`
44-
TelemetryEnable BoolFlag `json:"telemetry_enable"`
45-
TelemetryTraceEnable BoolFlag `json:"telemetry_trace_enable"`
46-
TelemetryURL StringFlag `json:"telemetry_url"`
47-
TLSEnable BoolFlag `json:"tls_enable"`
48-
TLSCertFiles StringArrayFlag `json:"tls_cert_files"`
49-
TLSClientCAFile StringFlag `json:"tls_client_ca_file"`
50-
TLSClientAuth StringFlag `json:"tls_client_auth"`
51-
TLSKeyFiles StringArrayFlag `json:"tls_key_tiles"`
52-
TLSMinVersion StringFlag `json:"tls_min_version"`
53-
TraceEnable BoolFlag `json:"trace_enable"`
54-
SecureAuthCookie BoolFlag `json:"secure_auth_cookie"`
55-
SSHKeygenAlgorithm StringFlag `json:"ssh_keygen_algorithm"`
56-
AutoImportTemplates StringArrayFlag `json:"auto_import_templates"`
57-
MetricsCacheRefreshInterval DurationFlag `json:"metrics_cache_refresh_interval"`
58-
AgentStatRefreshInterval DurationFlag `json:"agent_stat_refresh_interval"`
59-
Verbose BoolFlag `json:"verbose"`
60-
AuditLogging BoolFlag `json:"audit_logging"`
61-
BrowserOnly BoolFlag `json:"browser_only"`
62-
SCIMAuthHeader StringFlag `json:"scim_auth_header"`
63-
UserWorkspaceQuota IntFlag `json:"user_workspace_quota"`
13+
AccessURL *StringFlag `json:"access_url" typescript:",notnull"`
14+
WildcardAccessURL *StringFlag `json:"wildcard_access_url" typescript:",notnull"`
15+
Address *StringFlag `json:"address" typescript:",notnull"`
16+
AutobuildPollInterval *DurationFlag `json:"autobuild_poll_interval" typescript:",notnull"`
17+
DerpServerEnable *BoolFlag `json:"derp_server_enabled" typescript:",notnull"`
18+
DerpServerRegionID *IntFlag `json:"derp_server_region_id" typescript:",notnull"`
19+
DerpServerRegionCode *StringFlag `json:"derp_server_region_code" typescript:",notnull"`
20+
DerpServerRegionName *StringFlag `json:"derp_server_region_name" typescript:",notnull"`
21+
DerpServerSTUNAddresses *StringArrayFlag `json:"derp_server_stun_address" typescript:",notnull"`
22+
DerpConfigURL *StringFlag `json:"derp_config_url" typescript:",notnull"`
23+
DerpConfigPath *StringFlag `json:"derp_config_path" typescript:",notnull"`
24+
PromEnabled *BoolFlag `json:"prom_enabled" typescript:",notnull"`
25+
PromAddress *StringFlag `json:"prom_address" typescript:",notnull"`
26+
PprofEnabled *BoolFlag `json:"pprof_enabled" typescript:",notnull"`
27+
PprofAddress *StringFlag `json:"pprof_address" typescript:",notnull"`
28+
CacheDir *StringFlag `json:"cache_dir" typescript:",notnull"`
29+
InMemoryDatabase *BoolFlag `json:"in_memory_database" typescript:",notnull"`
30+
ProvisionerDaemonCount *IntFlag `json:"provisioner_daemon_count" typescript:",notnull"`
31+
PostgresURL *StringFlag `json:"postgres_url" typescript:",notnull"`
32+
OAuth2GithubClientID *StringFlag `json:"oauth2_github_client_id" typescript:",notnull"`
33+
OAuth2GithubClientSecret *StringFlag `json:"oauth2_github_client_secret" typescript:",notnull"`
34+
OAuth2GithubAllowedOrganizations *StringArrayFlag `json:"oauth2_github_allowed_organizations" typescript:",notnull"`
35+
OAuth2GithubAllowedTeams *StringArrayFlag `json:"oauth2_github_allowed_teams" typescript:",notnull"`
36+
OAuth2GithubAllowSignups *BoolFlag `json:"oauth2_github_allow_signups" typescript:",notnull"`
37+
OAuth2GithubEnterpriseBaseURL *StringFlag `json:"oauth2_github_enterprise_base_url" typescript:",notnull"`
38+
OIDCAllowSignups *BoolFlag `json:"oidc_allow_signups" typescript:",notnull"`
39+
OIDCClientID *StringFlag `json:"oidc_client_id" typescript:",notnull"`
40+
OIDCClientSecret *StringFlag `json:"oidc_cliet_secret" typescript:",notnull"`
41+
OIDCEmailDomain *StringFlag `json:"oidc_email_domain" typescript:",notnull"`
42+
OIDCIssuerURL *StringFlag `json:"oidc_issuer_url" typescript:",notnull"`
43+
OIDCScopes *StringArrayFlag `json:"oidc_scopes" typescript:",notnull"`
44+
TelemetryEnable *BoolFlag `json:"telemetry_enable" typescript:",notnull"`
45+
TelemetryTraceEnable *BoolFlag `json:"telemetry_trace_enable" typescript:",notnull"`
46+
TelemetryURL *StringFlag `json:"telemetry_url" typescript:",notnull"`
47+
TLSEnable *BoolFlag `json:"tls_enable" typescript:",notnull"`
48+
TLSCertFiles *StringArrayFlag `json:"tls_cert_files" typescript:",notnull"`
49+
TLSClientCAFile *StringFlag `json:"tls_client_ca_file" typescript:",notnull"`
50+
TLSClientAuth *StringFlag `json:"tls_client_auth" typescript:",notnull"`
51+
TLSKeyFiles *StringArrayFlag `json:"tls_key_tiles" typescript:",notnull"`
52+
TLSMinVersion *StringFlag `json:"tls_min_version" typescript:",notnull"`
53+
TraceEnable *BoolFlag `json:"trace_enable" typescript:",notnull"`
54+
SecureAuthCookie *BoolFlag `json:"secure_auth_cookie" typescript:",notnull"`
55+
SSHKeygenAlgorithm *StringFlag `json:"ssh_keygen_algorithm" typescript:",notnull"`
56+
AutoImportTemplates *StringArrayFlag `json:"auto_import_templates" typescript:",notnull"`
57+
MetricsCacheRefreshInterval *DurationFlag `json:"metrics_cache_refresh_interval" typescript:",notnull"`
58+
AgentStatRefreshInterval *DurationFlag `json:"agent_stat_refresh_interval" typescript:",notnull"`
59+
Verbose *BoolFlag `json:"verbose" typescript:",notnull"`
60+
AuditLogging *BoolFlag `json:"audit_logging" typescript:",notnull"`
61+
BrowserOnly *BoolFlag `json:"browser_only" typescript:",notnull"`
62+
SCIMAuthHeader *StringFlag `json:"scim_auth_header" typescript:",notnull"`
63+
UserWorkspaceQuota *IntFlag `json:"user_workspace_quota" typescript:",notnull"`
6464
}
6565

6666
type StringFlag struct {
@@ -71,6 +71,7 @@ type StringFlag struct {
7171
Description string `json:"description"`
7272
Enterprise bool `json:"enterprise"`
7373
Secret bool `json:"secret"`
74+
Hidden bool `json:"hidden"`
7475
Default string `json:"default"`
7576
Value string `json:"value"`
7677
}
@@ -82,6 +83,7 @@ type BoolFlag struct {
8283
Shorthand string `json:"shorthand"`
8384
Description string `json:"description"`
8485
Enterprise bool `json:"enterprise"`
86+
Hidden bool `json:"hidden"`
8587
Default bool `json:"default"`
8688
Value bool `json:"value"`
8789
}
@@ -93,6 +95,7 @@ type IntFlag struct {
9395
Shorthand string `json:"shorthand"`
9496
Description string `json:"description"`
9597
Enterprise bool `json:"enterprise"`
98+
Hidden bool `json:"hidden"`
9699
Default int `json:"default"`
97100
Value int `json:"value"`
98101
}
@@ -104,6 +107,7 @@ type DurationFlag struct {
104107
Shorthand string `json:"shorthand"`
105108
Description string `json:"description"`
106109
Enterprise bool `json:"enterprise"`
110+
Hidden bool `json:"hidden"`
107111
Default time.Duration `json:"default"`
108112
Value time.Duration `json:"value"`
109113
}
@@ -115,6 +119,7 @@ type StringArrayFlag struct {
115119
Shorthand string `json:"shorthand"`
116120
Description string `json:"description"`
117121
Enterprise bool `json:"enterprise"`
122+
Hidden bool `json:"hidden"`
118123
Default []string `json:"default"`
119124
Value []string `json:"value"`
120125
}

codersdk/templateversions.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,8 @@ func (c *Client) TemplateVersionLogsAfter(ctx context.Context, version uuid.UUID
107107
// CreateTemplateVersionDryRunRequest defines the request parameters for
108108
// CreateTemplateVersionDryRun.
109109
type CreateTemplateVersionDryRunRequest struct {
110-
WorkspaceName string
111-
ParameterValues []CreateParameterRequest
110+
WorkspaceName string `json:"workspace_name"`
111+
ParameterValues []CreateParameterRequest `json:"parameter_values"`
112112
}
113113

114114
// CreateTemplateVersionDryRun begins a dry-run provisioner job against the

enterprise/cli/server.go

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55

66
"github.com/spf13/cobra"
77

8-
"github.com/coder/coder/cli/cliui"
98
"github.com/coder/coder/cli/deployment"
109
"github.com/coder/coder/enterprise/coderd"
1110

@@ -16,7 +15,6 @@ import (
1615
func server() *cobra.Command {
1716
dflags := deployment.Flags()
1817
cmd := agpl.Server(dflags, func(ctx context.Context, options *agplcoderd.Options) (*agplcoderd.API, error) {
19-
options.DeploymentFlags = &dflags
2018
o := &coderd.Options{
2119
AuditLogging: dflags.AuditLogging.Value,
2220
BrowserOnly: dflags.BrowserOnly.Value,
@@ -32,17 +30,7 @@ func server() *cobra.Command {
3230
return api.AGPL, nil
3331
})
3432

35-
// append enterprise description to flags
36-
enterpriseOnly := cliui.Styles.Keyword.Render(" This is an Enterprise feature. Contact sales@coder.com for licensing")
37-
dflags.AuditLogging.Description += enterpriseOnly
38-
dflags.BrowserOnly.Description += enterpriseOnly
39-
dflags.SCIMAuthHeader.Description += enterpriseOnly
40-
dflags.UserWorkspaceQuota.Description += enterpriseOnly
41-
42-
deployment.BoolFlag(cmd.Flags(), &dflags.AuditLogging)
43-
deployment.BoolFlag(cmd.Flags(), &dflags.BrowserOnly)
44-
deployment.StringFlag(cmd.Flags(), &dflags.SCIMAuthHeader)
45-
deployment.IntFlag(cmd.Flags(), &dflags.UserWorkspaceQuota)
33+
deployment.AttachFlags(cmd.Flags(), dflags, true)
4634

4735
return cmd
4836
}

0 commit comments

Comments
 (0)