-
Notifications
You must be signed in to change notification settings - Fork 2
/
imp_sort.go
45 lines (38 loc) · 1.09 KB
/
imp_sort.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
////////////////////////////////////////////////////////////////////////////
// Program: jsonfiddle
// Purpose: JSON Fiddling
// Authors: Tong Sun (c) 2017-2023, All rights reserved
////////////////////////////////////////////////////////////////////////////
package main
import (
"encoding/json"
"fmt"
"io"
"github.com/go-easygen/go-flags/clis"
)
// *** Sub-command: sort ***
// Exec implements the business logic of command `sort`
func (x *SortCommand) Exec(args []string) error {
fileI := clis.GetInputStream(x.Filei)
defer fileI.Close()
fileO := clis.GetOutputStream(x.Fileo)
defer fileO.Close()
return cmdSort(fileI, fileO)
}
//==========================================================================
// support functions
func cmdSort(r io.Reader, w io.Writer) error {
var res interface{}
content := readJson(r)
json.Unmarshal(content, &res)
var js []byte
var err error
if opts.Compact {
js, err = json.Marshal(res)
} else {
js, err = json.MarshalIndent(res, opts.Prefix, opts.Indent)
}
clis.AbortOn("[::sort] Marshaling input", err)
fmt.Fprintln(w, string(js))
return nil
}