Skip to content

Commit

Permalink
[hcledit/read] add flag to CLI command to control this behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
ryan-ph committed Apr 22, 2024
1 parent 19d2193 commit 76a798e
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
3 changes: 3 additions & 0 deletions cmd/hcledit/internal/command/fixture/file.tf
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ module "my-module" {
"f",
]

unevaluateable_reference = var.name
unevaluateable_interpolation = "this-${local.reference}"

map_variable = {
bool_variable = true
int_variable = 1
Expand Down
8 changes: 7 additions & 1 deletion cmd/hcledit/internal/command/read.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (

type ReadOptions struct {
OutputFormat string
Fallback bool
}

func NewCmdRead() *cobra.Command {
Expand All @@ -36,6 +37,7 @@ func NewCmdRead() *cobra.Command {
}

cmd.Flags().StringVarP(&opts.OutputFormat, "output-format", "o", "go-template='{{.Value}}'", "format to print the value as")
cmd.Flags().BoolVar(&opts.Fallback, "fallback", false, "falls back to reading the raw value if it cannot be evaluated")

return cmd
}
Expand All @@ -48,7 +50,11 @@ func runRead(opts *ReadOptions, args []string) (string, error) {
return "", fmt.Errorf("failed to read file: %s", err)
}

results, err := editor.Read(query)
readOpts:= []hcledit.Option{}
if opts.Fallback {
readOpts = append(readOpts, hcledit.WithReadFallbackToRawString())
}
results, err := editor.Read(query, readOpts...)
if err != nil {
return "", fmt.Errorf("failed to read file: %s", err)
}
Expand Down
16 changes: 16 additions & 0 deletions cmd/hcledit/internal/command/read_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,22 @@ func TestRunRead(t *testing.T) {
want: "",
opts: defaultOpts,
},
"unevaluateable reference fallback": {
query: "module.my-module.unevaluateable_reference",
want: "var.name",
opts: &ReadOptions{
OutputFormat: "go-template='{{.Value}}'",
Fallback: true,
},
},
"unevaluateable interpolation fallback": {
query: "module.my-module.unevaluateable_interpolation",
want: `"this-${local.reference}"`,
opts: &ReadOptions{
OutputFormat: "go-template='{{.Value}}'",
Fallback: true,
},
},
}

for name, tc := range cases {
Expand Down

0 comments on commit 76a798e

Please sign in to comment.