2015-10-26 23:37:44 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
|
|
|
|
"github.com/kisom/goutils/lib"
|
|
|
|
)
|
|
|
|
|
2016-07-01 22:56:52 +00:00
|
|
|
func prettify(file string, validateOnly bool) error {
|
2015-10-26 23:37:44 +00:00
|
|
|
in, err := ioutil.ReadFile(file)
|
|
|
|
if err != nil {
|
|
|
|
lib.Warn(err, "ReadFile")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var buf = &bytes.Buffer{}
|
|
|
|
err = json.Indent(buf, in, "", " ")
|
|
|
|
if err != nil {
|
|
|
|
lib.Warn(err, "%s", file)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-07-01 22:56:52 +00:00
|
|
|
if validateOnly {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-10-26 23:37:44 +00:00
|
|
|
err = ioutil.WriteFile(file, buf.Bytes(), 0644)
|
|
|
|
if err != nil {
|
|
|
|
lib.Warn(err, "WriteFile")
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-07-01 22:56:52 +00:00
|
|
|
func compact(file string, validateOnly bool) error {
|
2015-10-26 23:37:44 +00:00
|
|
|
in, err := ioutil.ReadFile(file)
|
|
|
|
if err != nil {
|
|
|
|
lib.Warn(err, "ReadFile")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var buf = &bytes.Buffer{}
|
|
|
|
err = json.Compact(buf, in)
|
|
|
|
if err != nil {
|
|
|
|
lib.Warn(err, "%s", file)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-07-01 22:56:52 +00:00
|
|
|
if validateOnly {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-10-26 23:37:44 +00:00
|
|
|
err = ioutil.WriteFile(file, buf.Bytes(), 0644)
|
|
|
|
if err != nil {
|
|
|
|
lib.Warn(err, "WriteFile")
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func usage() {
|
|
|
|
progname := lib.ProgName()
|
|
|
|
fmt.Printf(`Usage: %s [-h] files...
|
|
|
|
%s is used to lint and prettify (or compact) JSON files. The
|
|
|
|
files will be updated in-place.
|
|
|
|
|
|
|
|
Flags:
|
|
|
|
-c Compact files.
|
|
|
|
-h Print this help message.
|
2016-07-01 22:56:52 +00:00
|
|
|
-n Don't prettify; only perform validation.
|
2015-10-26 23:37:44 +00:00
|
|
|
`, progname, progname)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
flag.Usage = usage
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
2016-07-01 22:56:52 +00:00
|
|
|
var shouldCompact, validateOnly bool
|
2015-10-26 23:37:44 +00:00
|
|
|
flag.BoolVar(&shouldCompact, "c", false, "Compact files instead of prettifying.")
|
2016-07-01 22:56:52 +00:00
|
|
|
flag.BoolVar(&validateOnly, "n", false, "Don't write changes; only perform validation.")
|
2015-10-26 23:37:44 +00:00
|
|
|
flag.Parse()
|
|
|
|
|
|
|
|
action := prettify
|
|
|
|
if shouldCompact {
|
|
|
|
action = compact
|
|
|
|
}
|
|
|
|
|
|
|
|
var errCount int
|
|
|
|
for _, fileName := range flag.Args() {
|
2016-07-01 22:56:52 +00:00
|
|
|
err := action(fileName, validateOnly)
|
2015-10-26 23:37:44 +00:00
|
|
|
if err != nil {
|
|
|
|
errCount++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if errCount > 0 {
|
|
|
|
lib.Errx(lib.ExitFailure, "Not all files succeeded.")
|
|
|
|
}
|
|
|
|
}
|