Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue #20 "unexpected end of JSON input" error #64

Merged
merged 5 commits into from
Jan 27, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 75 additions & 20 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,89 @@ name: Build

on:
push:
branches: [ main ]
branches: [main]
pull_request:
branches: [ main ]
branches: [main]

jobs:

build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/checkout@v4

- name: Run Gosec Security Scanner
uses: securego/gosec@master
with:
args: -exclude=G204 ./...

- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: "1.21"

- name: golangci-lint
uses: golangci/golangci-lint-action@v3
with:
version: latest

- name: Test
run: go test -v ./...

- name: Build
run: go build

demo:
mdb marked this conversation as resolved.
Show resolved Hide resolved
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3

- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: "1.21"

- name: Build
run: |
go build -o example/tf-summarize

- name: Setup Terraform
uses: hashicorp/setup-terraform@v2
with:
terraform_wrapper: false

- name: Print tf-summarize version and help
run: |
./tf-summarize -v
./tf-summarize -h
working-directory: ./example

- name: Terraform Init
run: terraform init
working-directory: ./example

- name: Terraform Plan
run: |
terraform plan -out=tfplan -refresh=false # -refresh=false is only for demo workflow
terraform show -json tfplan > tfplan.json
working-directory: ./example

- name: Run Gosec Security Scanner
uses: securego/gosec@master
with:
args: -exclude=G204 ./...
- name: summary in table format
run: terraform show -json tfplan | ./tf-summarize
working-directory: ./example

- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: '1.21'
- name: summary in table format with plan JSON file passed
run: |
./tf-summarize tfplan.json
working-directory: ./example

- name: golangci-lint
uses: golangci/golangci-lint-action@v3
with:
version: latest
- name: summary in tree format
run: terraform show -json tfplan | ./tf-summarize -tree
working-directory: ./example

- name: Test
run: go test -v ./...
- name: summary in separate tree format
run: terraform show -json tfplan | ./tf-summarize -separate-tree
working-directory: ./example

- name: Build
run: go build
- name: summary in draw visual tree format
run: terraform show -json tfplan | ./tf-summarize -tree -draw
working-directory: ./example
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func main() {
err := validateFlags(*tree, *separateTree, *drawable, *md, args)
logIfErrorAndExit("invalid input flags: %s\n", err, flag.Usage)

newReader, err := reader.CreateReader(os.Stdin, args)
newReader, err := reader.CreateReader(args)
logIfErrorAndExit("error creating input reader: %s\n", err, flag.Usage)

input, err := newReader.Read()
Expand Down
24 changes: 14 additions & 10 deletions reader/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package reader

import (
"bufio"
"errors"
"fmt"
"io"
"os"
"strings"
)

type Reader interface {
Expand All @@ -22,19 +23,22 @@ func readFile(f io.Reader) ([]byte, error) {
input = append(input, line...)
}
if err != io.EOF {
return nil, fmt.Errorf("error reading file: %s", err.Error())
return nil, fmt.Errorf("error reading input: %s", err.Error())
}
if len(input) == 0 {
return nil, errors.New("no input data; expected input via a non-empty file or via STDIN")
}
return input, nil
}

func CreateReader(stdin *os.File, args []string) (Reader, error) {
stat, _ := stdin.Stat()
if (stat.Mode() & os.ModeCharDevice) == 0 {
return NewStdinReader(), nil
dineshba marked this conversation as resolved.
Show resolved Hide resolved
func CreateReader(args []string) (Reader, error) {
if len(args) > 1 {
return nil, fmt.Errorf("expected input via a single filename argument or via STDIN; received multiple arguments: %s", strings.Join(args, ", "))
}
if len(args) < 1 {
return nil, fmt.Errorf("should have either stdin input through pipe or first argument should be file")

if len(args) == 1 {
return NewFileReader(args[0]), nil
}
fileName := args[0]
return NewFileReader(fileName), nil

return NewStdinReader(), nil
}
Loading