diff --git a/plan.go b/plan.go index 2a598a7..5733372 100644 --- a/plan.go +++ b/plan.go @@ -1,6 +1,7 @@ package preview import ( + "bytes" "encoding/json" "fmt" "io" @@ -20,15 +21,20 @@ import ( ) func PlanJSONHook(dfs fs.FS, input Input) (func(ctx *tfcontext.Context, blocks terraform.Blocks, inputVars map[string]cty.Value), error) { - if input.PlanJSONPath == "" { - return func(ctx *tfcontext.Context, blocks terraform.Blocks, inputVars map[string]cty.Value) {}, nil - } - file, err := dfs.Open(input.PlanJSONPath) - if err != nil { - return nil, fmt.Errorf("unable to open plan JSON file: %w", err) + var contents io.Reader = bytes.NewReader(input.PlanJSON) + if len(input.PlanJSON) == 0 { + if input.PlanJSONPath == "" { + return func(ctx *tfcontext.Context, blocks terraform.Blocks, inputVars map[string]cty.Value) {}, nil + } + + var err error + contents, err = dfs.Open(input.PlanJSONPath) + if err != nil { + return nil, fmt.Errorf("unable to open plan JSON file: %w", err) + } } - plan, err := ParsePlanJSON(file) + plan, err := ParsePlanJSON(contents) if err != nil { return nil, fmt.Errorf("unable to parse plan JSON: %w", err) } diff --git a/preview.go b/preview.go index 927ae55..10cfb6e 100644 --- a/preview.go +++ b/preview.go @@ -2,6 +2,7 @@ package preview import ( "context" + "encoding/json" "fmt" "io/fs" "log/slog" @@ -17,7 +18,11 @@ import ( ) type Input struct { + // PlanJSONPath is an optional path to a plan file. If PlanJSON isn't + // specified, and PlanJSONPath is, then the file will be read and treated + // as if the contents were passed in directly. PlanJSONPath string + PlanJSON json.RawMessage ParameterValues map[string]string Owner types.WorkspaceOwner }