Skip to content

Commit 228b59b

Browse files
authored
Merge pull request #568 from jirtosterone/feature/merge-ext-vars-from-cli-and-files
Fix ext_str/ext_code from option file being overridden by CLI args
2 parents ceed027 + 8d2a8f8 commit 228b59b

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

cli.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,10 @@ func ParseCLI(args []string) (string, *CLIOptions, func(), error) {
117117
kongOpts := []kong.Option{kong.Vars{"version": Version}}
118118

119119
// load default options
120+
var defaultOpt *Option
120121
if optionFilePath != "" {
121-
defaultOpt, err := loadDefinitionFile[Option](nil, optionFilePath, nil)
122+
var err error
123+
defaultOpt, err = loadDefinitionFile[Option](nil, optionFilePath, nil)
122124
if err != nil {
123125
return "", nil, nil, fmt.Errorf("failed to load option file: %w", err)
124126
}
@@ -148,6 +150,17 @@ func ParseCLI(args []string) (string, *CLIOptions, func(), error) {
148150
}
149151
opts.Envfile = lo.Uniq(envfiles) // envfiles are parsed before, so it's safe to overwrite
150152

153+
// Merge ext_str and ext_code from option file with CLI args
154+
// CLI args take precedence over option file values
155+
if defaultOpt != nil {
156+
if defaultOpt.ExtStr != nil || opts.ExtStr != nil {
157+
opts.ExtStr = lo.Assign(defaultOpt.ExtStr, opts.ExtStr)
158+
}
159+
if defaultOpt.ExtCode != nil || opts.ExtCode != nil {
160+
opts.ExtCode = lo.Assign(defaultOpt.ExtCode, opts.ExtCode)
161+
}
162+
}
163+
151164
sub := strings.Fields(c.Command())[0]
152165
return sub, &opts, func() { c.PrintUsage(true) }, nil
153166
}

cli_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,32 @@ var cliTests = []struct {
195195
},
196196
},
197197
},
198+
{
199+
// Test: CLI args are merged with option file, CLI takes precedence
200+
args: []string{
201+
"render", "--option", "ext_vars.jsonnet",
202+
"--ext-str", "cli_key=cli_val",
203+
"--ext-str", "architecture=arm64",
204+
"--ext-code", "memory_size=256",
205+
"--ext-code", "storage_size=1024",
206+
},
207+
sub: "render",
208+
option: &lambroll.Option{
209+
OptionFilePath: "ext_vars.jsonnet",
210+
Color: true,
211+
Envfile: []string{},
212+
ExtStr: map[string]string{
213+
"architecture": "arm64", // CLI overrides option file (x86_64 -> arm64)
214+
"description": "Test function with ext_str and ext_code", // from option file
215+
"cli_key": "cli_val", // from CLI
216+
},
217+
ExtCode: map[string]string{
218+
"memory_size": "256", // CLI overrides option file (128 -> 256)
219+
"storage_size": "1024", // CLI overrides option file (512 -> 1024)
220+
"timeout": "30", // from option file
221+
},
222+
},
223+
},
198224
}
199225

200226
func TestParseCLI(t *testing.T) {

0 commit comments

Comments
 (0)