Skip to content

Commit

Permalink
apps: Nest dev-config settings under 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewsomething committed Oct 18, 2022
1 parent 4b8c170 commit e0369a3
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 8 deletions.
18 changes: 11 additions & 7 deletions commands/apps_dev.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ import (

const (
// AppsDevDefaultEnvFile is the default env file path.
AppsDevDefaultEnvFile = ".env"
AppsDevDefaultEnvFile = ".env"
appDevConfigFileNamespace = "dev"
)

// AppsDev creates the apps dev command subtree.
Expand Down Expand Up @@ -64,7 +65,7 @@ func AppsDev() *Command {
"Build an app component",
heredoc.Docf(`
[BETA] Build an app component locally.
The component name is optional unless running non-interactively.
All command line flags as optional. You may specify flags to be applied to the current build
Expand Down Expand Up @@ -220,7 +221,7 @@ func RunAppsDevBuild(c *CmdConfig) error {
{{warning (print crossmark " functions builds are coming soon!")}}
please use {{highlight "doctl serverless deploy"}} to build functions in the meantime.
`), nil)
return fmt.Errorf("not supported")
}
Expand Down Expand Up @@ -438,10 +439,10 @@ func RunAppsDevBuild(c *CmdConfig) error {
tmpl := `
{{success checkmark}} successfully built {{success .component}} in {{highlight (duration .dur)}}
{{success checkmark}} created container image {{success .img}}
{{pointerRight}} push your image to a container registry using {{highlight "docker push"}}
{{pointerRight}} or run it locally using {{highlight "docker run"}}; for example:
{{muted promptPrefix}} {{highlight (printf "docker run %s--rm %s%s" .port_env .port_arg .img)}}`

if _, ok := componentSpec.(godo.AppRoutableComponentSpec); ok {
Expand Down Expand Up @@ -481,7 +482,10 @@ func fileExists(path ...string) bool {
}

func appDevWorkspace(cmdConfig *CmdConfig) (*workspace.AppDev, error) {
devConfigFilePath, err := cmdConfig.Doit.GetString(cmdConfig.NS, doctl.ArgAppDevConfig)
// The setting is nested under the "dev" namespace, i.e. dev.config.set.dev-config
// This is needed to prevent a conflict with the base config setting.
ns := fmt.Sprintf("%s.%s", appDevConfigFileNamespace, cmdConfig.NS)
devConfigFilePath, err := cmdConfig.Doit.GetString(ns, doctl.ArgAppDevConfig)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -602,7 +606,7 @@ func appsDevBuildSpecRequired(ws *workspace.AppDev, appsService do.AppsService)
template.Print(heredoc.Doc(`
{{error (print crossmark " no app spec found.")}}
an app spec is required to start a local build. make sure doctl is run in the correct directory where your app code is.
`,
), nil)

Expand Down
13 changes: 12 additions & 1 deletion commands/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,19 @@ func TestAuthInitConfig(t *testing.T) {
err = yaml.Unmarshal(buf.Bytes(), &configFile)
assert.NoError(t, err)
defaultCfgFile := filepath.Join(defaultConfigHome(), defaultConfigName)

assert.Equal(t, configFile["config"], defaultCfgFile, "unexpected setting for 'config'")

// Ensure that the dev.config.set.dev-config setting is correct to prevent
// a conflict with the base config setting.
devConfig := configFile["dev"]
devConfigSetting := devConfig.(map[interface{}]interface{})["config"]
expectedConfigSetting := map[interface{}]interface{}(
map[interface{}]interface{}{
"set": map[interface{}]interface{}{"dev-config": ""},
"unset": map[interface{}]interface{}{"dev-config": ""},
},
)
assert.Equal(t, devConfigSetting, expectedConfigSetting, "unexpected setting for 'dev.config'")
})
}

Expand Down
8 changes: 8 additions & 0 deletions commands/doit.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,14 @@ func requiredOpt() flagOpt {
// AddStringFlag adds a string flag to a command.
func AddStringFlag(cmd *Command, name, shorthand, dflt, desc string, opts ...flagOpt) {
fn := flagName(cmd, name)
// flagName only supports nesting three levels deep. We need to force the
// app dev config set/unset --dev-config flag to be nested deeper.
// i.e dev.config.set.dev-config over config.set.dev-config
// This prevents a conflict with the base config setting.
if name == doctl.ArgAppDevConfig {
fn = fmt.Sprintf("%s.%s", appDevConfigFileNamespace, fn)
}

cmd.Flags().StringP(name, shorthand, dflt, desc)

for _, o := range opts {
Expand Down

0 comments on commit e0369a3

Please sign in to comment.