From 7bd65cea873085f9a2a8ed030fc9610ea94c04f1 Mon Sep 17 00:00:00 2001 From: Kyle Isom Date: Mon, 26 Oct 2015 16:37:44 -0700 Subject: [PATCH] Add jlp utility. --- README.md | 1 + cmd/jlp/README | 3 ++ cmd/jlp/jlp.go | 95 ++++++++++++++++++++++++++++++++++++++++++++++++++ lib/lib.go | 6 ++++ 4 files changed, 105 insertions(+) create mode 100644 cmd/jlp/README create mode 100644 cmd/jlp/jlp.go diff --git a/README.md b/README.md index e3d41a7..07c011a 100644 --- a/README.md +++ b/README.md @@ -14,6 +14,7 @@ Contents: csrpubdump/ Dump the public key from an X.509 certificate request. fragment/ Print a fragment of a file. + jlp/ JSON linter/prettifier. pembody/ Print the body of a PEM certificate. showimp/ List the external (e.g. non-stdlib and outside the current working directory) imports for a Go file. diff --git a/cmd/jlp/README b/cmd/jlp/README new file mode 100644 index 0000000..33c04c4 --- /dev/null +++ b/cmd/jlp/README @@ -0,0 +1,3 @@ +jlp + +This is a JSON linter / prettifier. \ No newline at end of file diff --git a/cmd/jlp/jlp.go b/cmd/jlp/jlp.go new file mode 100644 index 0000000..89688c8 --- /dev/null +++ b/cmd/jlp/jlp.go @@ -0,0 +1,95 @@ +package main + +import ( + "bytes" + "encoding/json" + "flag" + "fmt" + "io/ioutil" + + "github.com/kisom/goutils/lib" +) + +func prettify(file string) error { + 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 + } + + err = ioutil.WriteFile(file, buf.Bytes(), 0644) + if err != nil { + lib.Warn(err, "WriteFile") + } + + return err +} + +func compact(file string) error { + 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 + } + + 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. +`, progname, progname) + +} + +func init() { + flag.Usage = usage +} + +func main() { + var shouldCompact bool + flag.BoolVar(&shouldCompact, "c", false, "Compact files instead of prettifying.") + flag.Parse() + + action := prettify + if shouldCompact { + action = compact + } + + var errCount int + for _, fileName := range flag.Args() { + err := action(fileName) + if err != nil { + errCount++ + } + } + + if errCount > 0 { + lib.Errx(lib.ExitFailure, "Not all files succeeded.") + } +} diff --git a/lib/lib.go b/lib/lib.go index 104aa65..b717c7b 100644 --- a/lib/lib.go +++ b/lib/lib.go @@ -9,6 +9,12 @@ import ( var progname = filepath.Base(os.Args[0]) +// ProgName returns what lib thinks the program name is, namely the +// basename of of argv0. +func ProgName() string { + return progname +} + // Warnx displays a formatted error message to standard error, à la // warnx(3). func Warnx(format string, a ...interface{}) (int, error) {