diff --git a/.changelog/16181.txt b/.changelog/16181.txt new file mode 100644 index 000000000..5bc357459 --- /dev/null +++ b/.changelog/16181.txt @@ -0,0 +1,3 @@ +```release-note:bug +cli: `var put`: when second arg is an @-reference, check extension for format +``` diff --git a/command/var_put.go b/command/var_put.go index b979d6a47..0593cfbbf 100644 --- a/command/var_put.go +++ b/command/var_put.go @@ -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":