Last active
September 25, 2018 13:52
-
-
Save orian/c6aef9f484efae6cb6005338d54e9018 to your computer and use it in GitHub Desktop.
A super simple JSON formatter in Go.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bytes" | |
"encoding/json" | |
"flag" | |
"fmt" | |
"io/ioutil" | |
"os" | |
) | |
func main() { | |
indent := flag.String("indent", " ", "indentation string/character for formatter") | |
flag.Parse() | |
src, err := ioutil.ReadAll(os.Stdin) | |
if err != nil { | |
fmt.Fprintf(os.Stderr, "problem reading: %s", err) | |
os.Exit(1) | |
} | |
dst := &bytes.Buffer{} | |
if err := json.Indent(dst, src, "", *indent); err != nil { | |
fmt.Fprintf(os.Stderr, "problem formatting: %s", err) | |
os.Exit(1) | |
} | |
if _, err = dst.WriteTo(os.Stdout); err != nil { | |
fmt.Fprintf(os.Stderr, "problem writing: %s", err) | |
os.Exit(1) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment