Skip to content

Commit

Permalink
Merge pull request #469 from fujiwara/fix-option-override
Browse files Browse the repository at this point in the history
Make the specification clear for overriding flag priorities.
  • Loading branch information
fujiwara authored Jan 23, 2025
2 parents d803b0b + 7bbef33 commit 28fb0fc
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 0 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,12 @@ The file format is JSON or Jsonnet.
All fields are optional. If the field is not defined, the default value is used.
When command-line flags are specified, they take precedence over the options file.

The priority of the option values is as follows:

1. Command-line flags. (`--log-level=debug`)
2. The values defined in the option file. (`{"log_level": "debug"}`)
3. Environment variables. (`LAMBROLL_LOGLEVEL=debug`)

While parsing the option file, lambroll evaluates only the `{{env}}` and `{{must_env}}` template functions and `env` and `must_env` native functions in Jsonnet. Other functions are not available.

### Init
Expand Down
37 changes: 37 additions & 0 deletions cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,43 @@ var cliTests = []struct {
sub: "render",
err: os.ErrNotExist,
},
{
args: []string{"render", "--option", "override.jsonnet",
"--profile", "mine",
"--region", "us-west-2",
},
env: map[string]string{
"LAMBROLL_LOG_LEVEL": "debug",
},
sub: "render",
option: &lambroll.Option{
OptionFilePath: "override.jsonnet",
Color: true,
Envfile: []string{},
LogLevel: "trace", // option file's priority is higher than env
Profile: ptr("mine"),
Region: ptr("us-west-2"),
},
},
{
args: []string{"render",
"--profile", "mine",
"--region", "us-west-2",
},
env: map[string]string{
"LAMBROLL_OPTION": "override.jsonnet",
"LAMBROLL_LOG_LEVEL": "debug",
},
sub: "render",
option: &lambroll.Option{
OptionFilePath: "override.jsonnet",
Color: true,
Envfile: []string{},
LogLevel: "trace", // option file's priority is higher than env
Profile: ptr("mine"),
Region: ptr("us-west-2"),
},
},
}

func TestParseCLI(t *testing.T) {
Expand Down
4 changes: 4 additions & 0 deletions lambroll_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,7 @@ func TestFillDefaultValues(t *testing.T) {
})
}
}

func ptr[T any](v T) *T {
return &v
}
5 changes: 5 additions & 0 deletions test/cli/override.jsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
profile: 'dummy',
region: 'us-east-1',
log_level: 'trace',
}

0 comments on commit 28fb0fc

Please sign in to comment.