diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml new file mode 100644 index 0000000..4805281 --- /dev/null +++ b/.github/workflows/ci.yaml @@ -0,0 +1,27 @@ +name: CI + +on: + push: + branches: + - master + pull_request: + +jobs: + ci: + + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v6 + + - name: Set up Go + uses: actions/setup-go@v6 + with: + go-version: stable + + - name: Test + run: go test -timeout 60s -v ./... + + - name: golangci-lint + uses: golangci/golangci-lint-action@v9 + with: + version: latest \ No newline at end of file diff --git a/.github/workflows/release.yaml b/.github/workflows/release.yaml new file mode 100644 index 0000000..04bb0f2 --- /dev/null +++ b/.github/workflows/release.yaml @@ -0,0 +1,23 @@ +on: + release: + types: [created] + +permissions: + contents: write + packages: write + +jobs: + release-go-binary: + name: release Go Binary + runs-on: ubuntu-latest + strategy: + matrix: + goos: [linux, darwin] + goarch: [amd64, arm64] + steps: + - uses: actions/checkout@v6 + - uses: wangyoucao577/go-release-action@v1 + with: + github_token: ${{ secrets.GITHUB_TOKEN }} + goos: ${{ matrix.goos }} + goarch: ${{ matrix.goarch }} diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index 3869ae0..0000000 --- a/.travis.yml +++ /dev/null @@ -1,7 +0,0 @@ -language: go -go: - - 1.x -sudo: false -notifications: - email: false - diff --git a/README.md b/README.md index f93c62c..3f76b7c 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,8 @@ json2csv Converts a stream of newline separated json data to csv format. -[![Build Status](https://travis-ci.org/jehiah/json2csv.png?branch=master)](https://travis-ci.org/jehiah/json2csv) [![GitHub release](https://img.shields.io/github/release/jehiah/json2csv.svg)](https://github.com/jehiah/json2csv/releases/latest) +![Build Status](https://github.com/jehiah/json2csv/actions/workflows/ci.yaml/badge.svg?branch=master) +[![GitHub release](https://img.shields.io/github/release/jehiah/json2csv.svg)](https://github.com/jehiah/json2csv/releases/latest) Installation diff --git a/go.mod b/go.mod index 335e738..ea544b7 100644 --- a/go.mod +++ b/go.mod @@ -1,5 +1,11 @@ module github.com/jehiah/json2csv -go 1.13 +go 1.20 -require github.com/stretchr/testify v1.4.0 +require github.com/stretchr/testify v1.11.1 + +require ( + github.com/davecgh/go-spew v1.1.1 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect +) diff --git a/go.sum b/go.sum index 8fdee58..c4c1710 100644 --- a/go.sum +++ b/go.sum @@ -1,11 +1,10 @@ -github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= -github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= -github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk= -github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= +github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U= +github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw= -gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/main.go b/main.go index eca0ae4..cc0e198 100644 --- a/main.go +++ b/main.go @@ -69,15 +69,14 @@ func get_value(data map[string]interface{}, keyparts []string) string { subdata, _ := data[keyparts[0]].(map[string]interface{}) return get_value(subdata, keyparts[1:]) } else if v, ok := data[keyparts[0]]; ok { - switch v.(type) { + switch v := v.(type) { case nil: return "" case float64: - f, _ := v.(float64) - if math.Mod(f, 1.0) == 0.0 { - return fmt.Sprintf("%d", int(f)) + if math.Mod(v, 1.0) == 0.0 { + return fmt.Sprintf("%d", int(v)) } else { - return fmt.Sprintf("%f", f) + return fmt.Sprintf("%f", v) } default: return fmt.Sprintf("%+v", v) @@ -114,7 +113,11 @@ func json2csv(r LineReader, w *csv.Writer, keys []string, printHeader bool) { } if printHeader { - w.Write(keys) + err = w.Write(keys) + if err != nil { + log.Printf("ERROR Writing header: %s", err) + break + } w.Flush() printHeader = false } @@ -131,7 +134,16 @@ func json2csv(r LineReader, w *csv.Writer, keys []string, printHeader bool) { record = append(record, get_value(data, expanded_key)) } - w.Write(record) + err = w.Write(record) + if err != nil { + log.Printf("ERROR Writing record: %s", err) + break + } w.Flush() + err = w.Error() + if err != nil { + log.Printf("ERROR Flushing record: %s", err) + break + } } } diff --git a/main_test.go b/main_test.go index f52ac01..7c2a07a 100644 --- a/main_test.go +++ b/main_test.go @@ -3,18 +3,12 @@ package main import ( "bytes" "encoding/csv" - "io/ioutil" - "log" - "os" "testing" "github.com/stretchr/testify/assert" ) func TestGetTopic(t *testing.T) { - log.SetOutput(ioutil.Discard) - defer log.SetOutput(os.Stdout) - reader := bytes.NewBufferString(`{"a": 1, "b": "asdf\n"} {"a" : null}`) buf := bytes.NewBuffer([]byte{}) @@ -27,9 +21,6 @@ func TestGetTopic(t *testing.T) { } func TestGetLargeInt(t *testing.T) { - log.SetOutput(ioutil.Discard) - defer log.SetOutput(os.Stdout) - reader := bytes.NewBufferString(`{"a": 1356998399}`) buf := bytes.NewBuffer([]byte{}) writer := csv.NewWriter(buf) @@ -41,8 +32,6 @@ func TestGetLargeInt(t *testing.T) { } func TestGetFloat(t *testing.T) { - log.SetOutput(ioutil.Discard) - defer log.SetOutput(os.Stdout) reader := bytes.NewBufferString(`{"a": 1356998399.32}`) buf := bytes.NewBuffer([]byte{}) @@ -55,9 +44,6 @@ func TestGetFloat(t *testing.T) { } func TestGetNested(t *testing.T) { - log.SetOutput(ioutil.Discard) - defer log.SetOutput(os.Stdout) - reader := bytes.NewBufferString(`{"a": {"b": "asdf"}}`) buf := bytes.NewBuffer([]byte{}) writer := csv.NewWriter(buf) @@ -69,9 +55,6 @@ func TestGetNested(t *testing.T) { } func TestHeader(t *testing.T) { - log.SetOutput(ioutil.Discard) - defer log.SetOutput(os.Stdout) - reader := bytes.NewBufferString(`{"a": "b"} {"a": "c"}`) buf := bytes.NewBuffer([]byte{})