forked from mystiq/dex
schema: add small tool to standardize JSON formatting
This commit is contained in:
parent
a846016ceb
commit
f39191a92d
2 changed files with 56 additions and 0 deletions
|
@ -31,6 +31,8 @@ if [ ! -f $GENDOC ]; then
|
|||
exit 1
|
||||
fi
|
||||
|
||||
go run schema/jsonfmt.go $IN
|
||||
|
||||
$GENDOC --f $IN --o $DOC
|
||||
|
||||
# Though google-api-go-generator is a main, dex vendors the app using the same
|
||||
|
|
54
schema/jsonfmt.go
Normal file
54
schema/jsonfmt.go
Normal file
|
@ -0,0 +1,54 @@
|
|||
// +build ignore
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"os"
|
||||
)
|
||||
|
||||
func init() {
|
||||
log.SetPrefix("fmtjson: ")
|
||||
log.SetFlags(0)
|
||||
}
|
||||
|
||||
func main() {
|
||||
if len(os.Args) < 1 {
|
||||
log.Fatal(`usage: go run jsonfmt.go [files...]
|
||||
|
||||
This is a small utility that standardizes the formatting of JSON files.
|
||||
`)
|
||||
}
|
||||
|
||||
for _, path := range os.Args[1:] {
|
||||
data, err := ioutil.ReadFile(path)
|
||||
if err != nil {
|
||||
log.Fatalf("reading file %s: %v", path, err)
|
||||
}
|
||||
|
||||
buff := new(bytes.Buffer)
|
||||
if err := json.Indent(buff, data, "", " "); err != nil {
|
||||
if err, ok := err.(*json.SyntaxError); ok {
|
||||
// Calculate line number and column of error.
|
||||
data := data[:err.Offset]
|
||||
lineNum := 1 + bytes.Count(data, []byte{'\n'})
|
||||
lastIndex := bytes.LastIndex(data, []byte{'\n'})
|
||||
|
||||
colNum := err.Offset
|
||||
if lastIndex > -1 {
|
||||
colNum = int64(len(data) - lastIndex)
|
||||
}
|
||||
log.Fatalf("file %s: invalid json at line %d, column %d: %v", path, lineNum, colNum, err)
|
||||
}
|
||||
|
||||
log.Fatalf("file %s: invalid json: %v", path, err)
|
||||
}
|
||||
|
||||
if err := ioutil.WriteFile(path, buff.Bytes(), 0644); err != nil {
|
||||
log.Fatalf("write file %s: %v", path, err)
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue