[cli] var put - Add extension parsing to second argument when file (#16181)

This commit is contained in:
Charlie Voiselle 2023-02-16 13:43:01 -05:00 committed by GitHub
parent 671d9f64ec
commit c28c0eb6bc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 8 deletions

3
.changelog/16181.txt Normal file
View File

@ -0,0 +1,3 @@
```release-note:bug
cli: `var put`: when second arg is an @-reference, check extension for format
```

View File

@ -220,16 +220,11 @@ func (c *VarPutCommand) Run(args []string) int {
// ArgFileRefs start with "@" so we need to peel that off
// detect format based on file extension
specPath := arg[1:]
switch filepath.Ext(specPath) {
case ".json":
c.inFmt = "json"
case ".hcl":
c.inFmt = "hcl"
default:
c.Ui.Error(fmt.Sprintf("Unable to determine format of %s; Use the -in flag to specify it.", specPath))
err = c.setParserForFileArg(specPath)
if err != nil {
c.Ui.Error(err.Error())
return 1
}
verbose(fmt.Sprintf("Reading whole %s variable specification from %q", strings.ToUpper(c.inFmt), specPath))
c.contents, err = os.ReadFile(specPath)
if err != nil {
@ -260,6 +255,11 @@ func (c *VarPutCommand) Run(args []string) int {
case isArgFileRef(args[0]):
arg := args[0]
err = c.setParserForFileArg(arg)
if err != nil {
c.Ui.Error(err.Error())
return 1
}
verbose(fmt.Sprintf("Creating variable %q from specification file %q", path, arg))
fPath := arg[1:]
c.contents, err = os.ReadFile(fPath)
@ -519,6 +519,18 @@ func (c *VarPutCommand) GetConcurrentUI() cli.ConcurrentUi {
return cli.ConcurrentUi{Ui: c.Ui}
}
func (c *VarPutCommand) setParserForFileArg(arg string) error {
switch filepath.Ext(arg) {
case ".json":
c.inFmt = "json"
case ".hcl":
c.inFmt = "hcl"
default:
return fmt.Errorf("Unable to determine format of %s; Use the -in flag to specify it.", arg)
}
return nil
}
func (c *VarPutCommand) validateInputFlag() error {
switch c.inFmt {
case "hcl", "json":