Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into pretty-json
Browse files Browse the repository at this point in the history
  • Loading branch information
takaishi committed Jun 19, 2023
2 parents 520e58f + 040cc9d commit 95b7403
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 13 deletions.
8 changes: 1 addition & 7 deletions cmd/terraform-j2md/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package main

import (
"fmt"
"io"
"os"

"github.com/reproio/terraform-j2md/internal/terraform"
Expand All @@ -13,12 +12,7 @@ func main() {
}

func run() int {
input, err := io.ReadAll(os.Stdin)
if err != nil {
fmt.Fprintf(os.Stderr, "cannot read stdin: %v", err)
return 1
}
planData, err := terraform.NewPlanData(input)
planData, err := terraform.NewPlanData(os.Stdin)
if err != nil {
fmt.Fprintf(os.Stderr, "cannot parse input as Terraform plan JSON: %v", err)
return 1
Expand Down
4 changes: 2 additions & 2 deletions internal/terraform/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,9 @@ func (plan *PlanData) Render(w io.Writer) error {
return nil
}

func NewPlanData(input []byte) (*PlanData, error) {
func NewPlanData(input io.Reader) (*PlanData, error) {
var plan tfjson.Plan
if err := json.Unmarshal(input, &plan); err != nil {
if err := json.NewDecoder(input).Decode(&plan); err != nil {
return nil, fmt.Errorf("cannot parse input: %w", err)
}
sanitizedPlan, err := sanitize.SanitizePlan(&plan)
Expand Down
10 changes: 6 additions & 4 deletions test/plan_test/plan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,14 @@ func Test_newPlanData(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
inputFilePath := testDataPath(tt.name, "show.json")
input, err := os.ReadFile(inputFilePath)
file, err := os.Open(inputFilePath)
if err != nil {
t.Errorf("cannot open input file: %s", inputFilePath)
return
}
defer file.Close()

_, err = terraform.NewPlanData(input)
_, err = terraform.NewPlanData(file)
if (err != nil) != tt.wantErr {
t.Errorf("NewPlanData() error = %v, wantErr %v", err, tt.wantErr)
return
Expand All @@ -64,13 +65,14 @@ func Test_render(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
inputFilePath := testDataPath(tt.name, "show.json")
input, err := os.ReadFile(inputFilePath)
file, err := os.Open(inputFilePath)
if err != nil {
t.Errorf("cannot open input file: %s", inputFilePath)
return
}
defer file.Close()

plan, err := terraform.NewPlanData(input)
plan, err := terraform.NewPlanData(file)
if err != nil {
t.Errorf("cannot parse JSON as plan: %v", err)
return
Expand Down

0 comments on commit 95b7403

Please sign in to comment.