Skip to content

Commit

Permalink
Use glob in jsoncompact input (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
vearutop authored Jun 13, 2021
1 parent 8302499 commit 9a52fc1
Show file tree
Hide file tree
Showing 11 changed files with 219 additions and 29 deletions.
36 changes: 36 additions & 0 deletions .github/workflows/cloc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# This script is provided by github.com/bool64/dev.
name: cloc
on:
pull_request:
jobs:
cloc:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
with:
path: pr
- name: Checkout base code
uses: actions/checkout@v2
with:
ref: ${{ github.event.pull_request.base.sha }}
path: base
- name: Count Lines Of Code
id: loc
run: |
curl -OL https://github.com/vearutop/sccdiff/releases/download/v1.0.1/linux_amd64.tar.gz && tar xf linux_amd64.tar.gz
OUTPUT=$(cd pr && ../sccdiff -basedir ../base)
OUTPUT="${OUTPUT//'%'/'%25'}"
OUTPUT="${OUTPUT//$'\n'/'%0A'}"
OUTPUT="${OUTPUT//$'\r'/'%0D'}"
echo "::set-output name=diff::$OUTPUT"
- name: Comment Code Lines
uses: marocchino/sticky-pull-request-comment@v2
with:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
header: LOC
message: |
### Lines Of Code
${{ steps.loc.outputs.diff }}
5 changes: 3 additions & 2 deletions .github/workflows/golangci-lint.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# This script is provided by github.com/bool64/dev.
name: lint
on:
push:
Expand All @@ -14,10 +15,10 @@ jobs:
steps:
- uses: actions/checkout@v2
- name: golangci-lint
uses: golangci/golangci-lint-action@v2.5.1
uses: golangci/golangci-lint-action@v2.5.2
with:
# Required: the version of golangci-lint is required and must be specified without patch version: we always use the latest patch version.
version: v1.38.0
version: v1.40.1

# Optional: working directory, useful for monorepos
# working-directory: somedir
Expand Down
77 changes: 77 additions & 0 deletions .github/workflows/release-assets.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# This script is provided by github.com/bool64/dev.

# This script uploads application binaries as GitHub release assets.
name: release-assets
on:
release:
types:
- created
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

jobs:
build:
name: Upload Release Assets
strategy:
matrix:
go-version: [ 1.16.x ]
runs-on: ubuntu-latest
steps:
- name: Install Go
uses: actions/setup-go@v2
with:
go-version: ${{ matrix.go-version }}
- name: Checkout code
uses: actions/checkout@v2
- name: Build artifacts
run: |
make release-assets
- name: Upload linux_amd64.tar.gz
if: hashFiles('linux_amd64.tar.gz') != ''
uses: actions/upload-release-asset@v1
with:
upload_url: ${{ github.event.release.upload_url }}
asset_path: ./linux_amd64.tar.gz
asset_name: linux_amd64.tar.gz
asset_content_type: application/tar+gzip
- name: Upload linux_arm64.tar.gz
if: hashFiles('linux_arm64.tar.gz') != ''
uses: actions/upload-release-asset@v1
with:
upload_url: ${{ github.event.release.upload_url }}
asset_path: ./linux_arm64.tar.gz
asset_name: linux_arm64.tar.gz
asset_content_type: application/tar+gzip
- name: Upload linux_arm.tar.gz
if: hashFiles('linux_arm.tar.gz') != ''
uses: actions/upload-release-asset@v1
with:
upload_url: ${{ github.event.release.upload_url }}
asset_path: ./linux_arm.tar.gz
asset_name: linux_arm.tar.gz
asset_content_type: application/tar+gzip
- name: Upload darwin_amd64.tar.gz
if: hashFiles('darwin_amd64.tar.gz') != ''
uses: actions/upload-release-asset@v1
with:
upload_url: ${{ github.event.release.upload_url }}
asset_path: ./darwin_amd64.tar.gz
asset_name: darwin_amd64.tar.gz
asset_content_type: application/tar+gzip
- name: Upload darwin_arm64.tar.gz
if: hashFiles('darwin_arm64.tar.gz') != ''
uses: actions/upload-release-asset@v1
with:
upload_url: ${{ github.event.release.upload_url }}
asset_path: ./darwin_arm64.tar.gz
asset_name: darwin_arm64.tar.gz
asset_content_type: application/tar+gzip
- name: Upload windows_amd64.zip
if: hashFiles('windows_amd64.zip') != ''
uses: actions/upload-release-asset@v1
with:
upload_url: ${{ github.event.release.upload_url }}
asset_path: ./windows_amd64.zip
asset_name: windows_amd64.zip
asset_content_type: application/zip

4 changes: 2 additions & 2 deletions .github/workflows/test-unit.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# This script is provided by github.com/bool64/dev.
name: test-unit
on:
push:
Expand Down Expand Up @@ -51,8 +52,7 @@ jobs:
if: matrix.go-version == '1.16.x' && env.RUN_BASE_COVERAGE == 'on' && steps.benchmark-base.outputs.cache-hit != 'true' && github.event.pull_request.base.sha != ''
run: |
cd __base
make test-unit
go tool cover -func=./unit.coverprofile | sed -e 's/.go:[0-9]*:\t/.go\t/g' | sed -e 's/\t\t*/\t/g' > ../unit-base.txt
make | grep test-unit && (make test-unit && go tool cover -func=./unit.coverprofile | sed -e 's/.go:[0-9]*:\t/.go\t/g' | sed -e 's/\t\t*/\t/g' > ../unit-base.txt) || echo "No test-unit in base"
- name: Test
id: test
run: |
Expand Down
6 changes: 4 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/.idea
/coverage.txt
/unit.coverprofile
/*.coverprofile
/.vscode
/bench-*.txt
/vendor
5 changes: 4 additions & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,11 @@ linters:
- paralleltest
- forbidigo
- exhaustivestruct
- interfacer
- interfacer # deprecated
- forcetypeassert
- scopelint # deprecated
- ifshort # too many false positives
- golint # deprecated

issues:
exclude-use-default: false
Expand Down
9 changes: 8 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
#GOLANGCI_LINT_VERSION := "v1.38.0" # Optional configuration to pinpoint golangci-lint version.
# Override in app Makefile to control build target, example BUILD_PKG=./cmd/my-app
BUILD_PKG ?= ./cmd/jsoncompact

#GOLANGCI_LINT_VERSION := "v1.40.1" # Optional configuration to pinpoint golangci-lint version.

# The head of Makefile determines location of dev-go to include standard targets.
GO ?= go
Expand Down Expand Up @@ -30,7 +33,11 @@ endif
-include $(DEVGO_PATH)/makefiles/main.mk
-include $(DEVGO_PATH)/makefiles/lint.mk
-include $(DEVGO_PATH)/makefiles/test-unit.mk
-include $(DEVGO_PATH)/makefiles/build.mk
-include $(DEVGO_PATH)/makefiles/release-assets.mk
-include $(DEVGO_PATH)/makefiles/reset-ci.mk

# Add your custom targets here.

## Run tests
test: test-unit
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -188,4 +188,27 @@ Available as `jsoncompact` CLI tool.

```
go get github.com/swaggest/assertjson/cmd/jsoncompact
```

You can invoke the tool against one or more JSON files, for example:

```
jsoncompact some-*.json path/to/another/*.json
```

Additional flags.

```
Usage of jsoncompact:
-indent string
Set indent. (default " ")
-len int
Line length limit. (default 100)
-output string
Path to output json file, if not specified input file is used.
-prefix string
Set prefix.
-v Verbose mode.
-version
Print version and exit.
```
78 changes: 59 additions & 19 deletions cmd/jsoncompact/main.go
Original file line number Diff line number Diff line change
@@ -1,28 +1,42 @@
package main

import (
"bytes"
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"log"
"path/filepath"

"github.com/bool64/dev/version"
"github.com/swaggest/assertjson"
)

// nolint // The function is a bit lengthy, but I'm not sure it
// would be more approachable if split in several functions.
func main() {
var (
input, output string
length int
prefix, indent string
ver, verbose bool
)

flag.StringVar(&output, "output", "", "path to output json file, if not specified input file is used")
flag.IntVar(&length, "len", 100, "line length limit")
flag.StringVar(&prefix, "prefix", "", "prefix")
flag.StringVar(&indent, "indent", " ", "indent")
flag.StringVar(&output, "output", "", "Path to output json file, if not specified input file is used.")
flag.IntVar(&length, "len", 100, "Line length limit.")
flag.StringVar(&prefix, "prefix", "", "Set prefix.")
flag.StringVar(&indent, "indent", " ", "Set indent.")
flag.BoolVar(&ver, "version", false, "Print version and exit.")
flag.BoolVar(&verbose, "v", false, "Verbose mode.")
flag.Parse()

if ver {
fmt.Println(version.Info().Version)

return
}

input = flag.Arg(0)
if input == "" {
_, _ = fmt.Fprintln(flag.CommandLine.Output(), "Missing input path argument.")
Expand All @@ -31,23 +45,49 @@ func main() {
return
}

if output == "" {
output = input
}
for _, in := range flag.Args() {
matches, err := filepath.Glob(in)
if err != nil {
log.Fatalf("could not read input: %v", err)
}

// nolint:gosec // Intentional file reading.
data, err := ioutil.ReadFile(input)
if err != nil {
log.Fatalf("could not read input: %v", err)
}
for _, m := range matches {
if verbose {
log.Printf("compacting %s.\n", m)
}

data, err = assertjson.MarshalIndentCompact(json.RawMessage(data), "", " ", length)
if err != nil {
log.Fatalf("could not process input: %v", err)
}
// nolint:gosec // Intentional file reading.
orig, err := ioutil.ReadFile(m)
if err != nil {
log.Fatalf("could not read input %s: %v", m, err)
}

comp, err := assertjson.MarshalIndentCompact(json.RawMessage(orig), prefix, indent, length)
if err != nil {
log.Fatalf("could not process input: %v", err)
}

if bytes.Equal(orig, comp) {
if verbose {
log.Printf("already compact, skipping %s\n", m)
}

continue
}

out := output
if out == "" {
out = m
}

if verbose {
log.Printf("writing to %s\n", out)
}

err = ioutil.WriteFile(output, data, 0600)
if err != nil {
log.Fatalf("could not write output: %v", err)
err = ioutil.WriteFile(out, comp, 0600)
if err != nil {
log.Fatalf("could not write output to %s: %v", out, err)
}
}
}
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/swaggest/assertjson
go 1.11

require (
github.com/bool64/dev v0.1.25
github.com/bool64/dev v0.1.35
github.com/bool64/shared v0.1.3
github.com/iancoleman/orderedmap v0.2.0
github.com/k0kubun/colorstring v0.0.0-20150214042306-9440f1994b88 // indirect
Expand Down
3 changes: 2 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
github.com/bool64/dev v0.1.25 h1:iSfcaS1FP6F2TlMI1daAvqk6PYl23XDf6oZu7WRXB8Y=
github.com/bool64/dev v0.1.25/go.mod h1:cTHiTDNc8EewrQPy3p1obNilpMpdmlUesDkFTF2zRWU=
github.com/bool64/dev v0.1.35 h1:uouBAq2kAJ+k9UypYRs118bAYttNQWDyK4IzjfLb5fc=
github.com/bool64/dev v0.1.35/go.mod h1:cTHiTDNc8EewrQPy3p1obNilpMpdmlUesDkFTF2zRWU=
github.com/bool64/shared v0.1.3 h1:gj7XZPYa1flQsCg3q9AIju+W2A1jaexK0fdFu2XtaG0=
github.com/bool64/shared v0.1.3/go.mod h1:RF1p1Oi29ofgOvinBpetbF5mceOUP3kpMkvLbWOmtm0=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand Down

0 comments on commit 9a52fc1

Please sign in to comment.