Skip to content

Commit 1e23db0

Browse files
committed
Add --write-config
1 parent af2ba00 commit 1e23db0

File tree

9 files changed

+114
-3
lines changed

9 files changed

+114
-3
lines changed

cli/deployment/config.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,12 @@ func newConfig() *codersdk.DeploymentConfig {
167167
Shorthand: "c",
168168
Default: "",
169169
},
170+
WriteConfig: &codersdk.DeploymentConfigField[bool]{
171+
Name: "Write Config",
172+
Usage: "If provided, coder server write out a YAML config to the path provided by --config and then immediately abort.",
173+
Flag: "write-config",
174+
Default: false,
175+
},
170176
InMemoryDatabase: &codersdk.DeploymentConfigField[bool]{
171177
Name: "In Memory Database",
172178
Usage: "Controls whether data will be stored in an in-memory database.",
@@ -597,6 +603,20 @@ func Config(configPath string, vip *viper.Viper) (*codersdk.DeploymentConfig, er
597603
return dc, nil
598604
}
599605

606+
// WriteConfig writes the config to the given path.
607+
func WriteConfig(configPath string, vip *viper.Viper) error {
608+
if _, err := os.Stat(configPath); err == nil {
609+
return xerrors.Errorf("config file already exists at %s", configPath)
610+
}
611+
612+
vip.SetConfigFile(configPath)
613+
err := vip.WriteConfig()
614+
if err != nil {
615+
return xerrors.Errorf("writing deployment config: %w", err)
616+
}
617+
return nil
618+
}
619+
600620
func setConfig(prefix string, vip *viper.Viper, target interface{}) {
601621
val := reflect.Indirect(reflect.ValueOf(target))
602622
typ := val.Type()

cli/server.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,15 @@ func Server(vip *viper.Viper, newAPI func(context.Context, *coderd.Options) (*co
9797
go dumpHandler(ctx)
9898

9999
configPath, _ := cmd.Flags().GetString("config")
100+
101+
justWriteConfig, _ := cmd.Flags().GetBool("write-config")
102+
if justWriteConfig {
103+
if configPath == "" {
104+
return xerrors.Errorf("--config must be set when writing config")
105+
}
106+
return deployment.WriteConfig(configPath, vip)
107+
}
108+
100109
cfg, err := deployment.Config(configPath, vip)
101110
if err != nil {
102111
return xerrors.Errorf("getting deployment config: %w", err)
@@ -968,8 +977,11 @@ func Server(vip *viper.Viper, newAPI func(context.Context, *coderd.Options) (*co
968977
postgresBuiltinServeCmd.Flags().BoolVar(&pgRawURL, "raw-url", false, "Output the raw connection URL instead of a psql command.")
969978

970979
createAdminUserCommand := newCreateAdminUserCommand()
971-
root.AddCommand(postgresBuiltinURLCmd, postgresBuiltinServeCmd, createAdminUserCommand)
972-
980+
root.AddCommand(
981+
postgresBuiltinURLCmd,
982+
postgresBuiltinServeCmd,
983+
createAdminUserCommand,
984+
)
973985
deployment.AttachFlags(root.Flags(), vip, false)
974986

975987
return root

coderd/apidoc/docs.go

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/apidoc/swagger.json

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

codersdk/deployment.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,9 @@ type DeploymentConfig struct {
148148
SessionDuration *DeploymentConfigField[time.Duration] `json:"max_session_expiry" typescript:",notnull"`
149149
DisableSessionExpiryRefresh *DeploymentConfigField[bool] `json:"disable_session_expiry_refresh" typescript:",notnull"`
150150
DisablePasswordAuth *DeploymentConfigField[bool] `json:"disable_password_auth" typescript:",notnull"`
151-
ConfigPath *DeploymentConfigField[string] `json:"config_path" typescript:",notnull"`
151+
152+
WriteConfig *DeploymentConfigField[bool] `json:"write_config" typescript:",notnull"`
153+
ConfigPath *DeploymentConfigField[string] `json:"config_path" typescript:",notnull"`
152154

153155
// DEPRECATED: Use HTTPAddress or TLS.Address instead.
154156
Address *DeploymentConfigField[string] `json:"address" typescript:",notnull"`

docs/api/general.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,17 @@ curl -X GET http://coder-server:8080/api/v2/config/deployment \
171171
"usage": "string",
172172
"value": "string"
173173
},
174+
"config_path": {
175+
"default": "string",
176+
"enterprise": true,
177+
"flag": "string",
178+
"hidden": true,
179+
"name": "string",
180+
"secret": true,
181+
"shorthand": "string",
182+
"usage": "string",
183+
"value": "string"
184+
},
174185
"dangerous": {
175186
"allow_path_app_sharing": {
176187
"default": true,
@@ -1095,6 +1106,17 @@ curl -X GET http://coder-server:8080/api/v2/config/deployment \
10951106
"shorthand": "string",
10961107
"usage": "string",
10971108
"value": "string"
1109+
},
1110+
"write_config": {
1111+
"default": true,
1112+
"enterprise": true,
1113+
"flag": "string",
1114+
"hidden": true,
1115+
"name": "string",
1116+
"secret": true,
1117+
"shorthand": "string",
1118+
"usage": "string",
1119+
"value": true
10981120
}
10991121
}
11001122
```

docs/api/schemas.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1577,6 +1577,17 @@ CreateParameterRequest is a structure used to create a new parameter value for a
15771577
"usage": "string",
15781578
"value": "string"
15791579
},
1580+
"config_path": {
1581+
"default": "string",
1582+
"enterprise": true,
1583+
"flag": "string",
1584+
"hidden": true,
1585+
"name": "string",
1586+
"secret": true,
1587+
"shorthand": "string",
1588+
"usage": "string",
1589+
"value": "string"
1590+
},
15801591
"dangerous": {
15811592
"allow_path_app_sharing": {
15821593
"default": true,
@@ -2501,6 +2512,17 @@ CreateParameterRequest is a structure used to create a new parameter value for a
25012512
"shorthand": "string",
25022513
"usage": "string",
25032514
"value": "string"
2515+
},
2516+
"write_config": {
2517+
"default": true,
2518+
"enterprise": true,
2519+
"flag": "string",
2520+
"hidden": true,
2521+
"name": "string",
2522+
"secret": true,
2523+
"shorthand": "string",
2524+
"usage": "string",
2525+
"value": true
25042526
}
25052527
}
25062528
```
@@ -2517,6 +2539,7 @@ CreateParameterRequest is a structure used to create a new parameter value for a
25172539
| `autobuild_poll_interval` | [codersdk.DeploymentConfigField-time_Duration](#codersdkdeploymentconfigfield-time_duration) | false | | |
25182540
| `browser_only` | [codersdk.DeploymentConfigField-bool](#codersdkdeploymentconfigfield-bool) | false | | |
25192541
| `cache_directory` | [codersdk.DeploymentConfigField-string](#codersdkdeploymentconfigfield-string) | false | | |
2542+
| `config_path` | [codersdk.DeploymentConfigField-string](#codersdkdeploymentconfigfield-string) | false | | |
25202543
| `dangerous` | [codersdk.DangerousConfig](#codersdkdangerousconfig) | false | | |
25212544
| `derp` | [codersdk.DERP](#codersdkderp) | false | | |
25222545
| `disable_password_auth` | [codersdk.DeploymentConfigField-bool](#codersdkdeploymentconfigfield-bool) | false | | |
@@ -2552,6 +2575,7 @@ CreateParameterRequest is a structure used to create a new parameter value for a
25522575
| `trace` | [codersdk.TraceConfig](#codersdktraceconfig) | false | | |
25532576
| `update_check` | [codersdk.DeploymentConfigField-bool](#codersdkdeploymentconfigfield-bool) | false | | |
25542577
| `wildcard_access_url` | [codersdk.DeploymentConfigField-string](#codersdkdeploymentconfigfield-string) | false | | |
2578+
| `write_config` | [codersdk.DeploymentConfigField-bool](#codersdkdeploymentconfigfield-bool) | false | | |
25552579

25562580
## codersdk.DeploymentConfigField-array_codersdk_GitAuthConfig
25572581

docs/cli/coder_server.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,14 @@ The directory to cache temporary files. If unspecified and $CACHE_DIRECTORY is s
4646
| Consumes | <code>$CODER_CACHE_DIRECTORY</code> |
4747
| Default | <code>~/.cache/coder</code> |
4848

49+
### --config, -c
50+
51+
If provided, coder server will use this configuration file in addition to provided flags.
52+
<br/>
53+
| | |
54+
| --- | --- |
55+
| Consumes | <code>$CODER_CONFIG_PATH</code> |
56+
4957
### --dangerous-allow-path-app-sharing
5058

5159
Allow workspace apps that are not served from subdomains to be shared. Path-based app sharing is DISABLED by default for security purposes. Path-based apps can make requests to the Coder API and pose a security risk when the workspace serves malicious JavaScript. Path-based apps can be disabled entirely with --disable-path-apps for further security.
@@ -658,3 +666,12 @@ Specifies the wildcard hostname to use for workspace applications in the form "\
658666
| | |
659667
| --- | --- |
660668
| Consumes | <code>$CODER_WILDCARD_ACCESS_URL</code> |
669+
670+
### --write-config
671+
672+
If provided, coder server write out a YAML config to the path provided by --config and then immediately abort.
673+
<br/>
674+
| | |
675+
| --- | --- |
676+
| Consumes | <code>$CODER_WRITE_CONFIG</code> |
677+
| Default | <code>false</code> |

site/src/api/typesGenerated.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,8 @@ export interface DeploymentConfig {
332332
readonly max_session_expiry: DeploymentConfigField<number>
333333
readonly disable_session_expiry_refresh: DeploymentConfigField<boolean>
334334
readonly disable_password_auth: DeploymentConfigField<boolean>
335+
readonly write_config: DeploymentConfigField<boolean>
336+
readonly config_path: DeploymentConfigField<string>
335337
readonly address: DeploymentConfigField<string>
336338
readonly experimental: DeploymentConfigField<boolean>
337339
}

0 commit comments

Comments
 (0)