-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
51 lines (42 loc) · 920 Bytes
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package main
import (
"bufio"
"encoding/json"
"fmt"
"github.com/alexflint/go-arg"
"io/ioutil"
"os"
)
var args struct {
JSONFilePath string `arg:"-f" help:"path to the JSON file"`
}
func main() {
arg.MustParse(&args)
var rawContent []byte
if args.JSONFilePath != "" {
content, err := ioutil.ReadFile(args.JSONFilePath)
if err != nil {
panic(fmt.Errorf("error when reading the JSON file: %w", err))
}
rawContent = content
} else {
reader := bufio.NewReader(os.Stdin)
var err error
var input byte
for {
input, err = reader.ReadByte()
if err != nil {
break
} else {
rawContent = append(rawContent, input)
}
}
}
var output any
err := json.Unmarshal(rawContent, &output)
if err != nil {
panic(fmt.Errorf("error when unmarshalling JSON content: %w", err))
}
formattedOutput, err := json.MarshalIndent(output, "", " ")
fmt.Println(string(formattedOutput))
}