Skip to content

Commit 9e7b090

Browse files
authored
Merge pull request #47 from coder/lilac/plan-raw-message
feat: support passing in plan json data directly
2 parents 248bf11 + fc5d12e commit 9e7b090

File tree

2 files changed

+18
-7
lines changed

2 files changed

+18
-7
lines changed

plan.go

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package preview
22

33
import (
4+
"bytes"
45
"encoding/json"
56
"fmt"
67
"io"
@@ -20,15 +21,20 @@ import (
2021
)
2122

2223
func PlanJSONHook(dfs fs.FS, input Input) (func(ctx *tfcontext.Context, blocks terraform.Blocks, inputVars map[string]cty.Value), error) {
23-
if input.PlanJSONPath == "" {
24-
return func(ctx *tfcontext.Context, blocks terraform.Blocks, inputVars map[string]cty.Value) {}, nil
25-
}
26-
file, err := dfs.Open(input.PlanJSONPath)
27-
if err != nil {
28-
return nil, fmt.Errorf("unable to open plan JSON file: %w", err)
24+
var contents io.Reader = bytes.NewReader(input.PlanJSON)
25+
if len(input.PlanJSON) == 0 {
26+
if input.PlanJSONPath == "" {
27+
return func(ctx *tfcontext.Context, blocks terraform.Blocks, inputVars map[string]cty.Value) {}, nil
28+
}
29+
30+
var err error
31+
contents, err = dfs.Open(input.PlanJSONPath)
32+
if err != nil {
33+
return nil, fmt.Errorf("unable to open plan JSON file: %w", err)
34+
}
2935
}
3036

31-
plan, err := ParsePlanJSON(file)
37+
plan, err := ParsePlanJSON(contents)
3238
if err != nil {
3339
return nil, fmt.Errorf("unable to parse plan JSON: %w", err)
3440
}

preview.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package preview
22

33
import (
44
"context"
5+
"encoding/json"
56
"fmt"
67
"io/fs"
78
"log/slog"
@@ -17,7 +18,11 @@ import (
1718
)
1819

1920
type Input struct {
21+
// PlanJSONPath is an optional path to a plan file. If PlanJSON isn't
22+
// specified, and PlanJSONPath is, then the file will be read and treated
23+
// as if the contents were passed in directly.
2024
PlanJSONPath string
25+
PlanJSON json.RawMessage
2126
ParameterValues map[string]string
2227
Owner types.WorkspaceOwner
2328
}

0 commit comments

Comments
 (0)