-
Notifications
You must be signed in to change notification settings - Fork 2
/
imp_j2s.go
55 lines (45 loc) · 1.48 KB
/
imp_j2s.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
52
53
54
55
////////////////////////////////////////////////////////////////////////////
// Program: jsonfiddle
// Purpose: JSON Fiddling
// Authors: Tong Sun (c) 2017-2023, All rights reserved
////////////////////////////////////////////////////////////////////////////
package main
import (
"fmt"
"path/filepath"
"github.com/go-easygen/go-flags/clis"
"github.com/go-jsonfile/gojson"
"github.com/suntong/enum"
)
////////////////////////////////////////////////////////////////////////////
// Global variables definitions
var (
fmtTypeEn = enum.NewEnum()
fmtJson = fmtTypeEn.Iota("json")
fmtYaml = fmtTypeEn.Iota("yaml")
gjparser = []gojson.Parser{gojson.ParseJson, gojson.ParseYaml}
)
////////////////////////////////////////////////////////////////////////////
// Function definitions
// *** Sub-command: j2s ***
// Exec implements the business logic of command `j2s`
func (x *J2sCommand) Exec(args []string) error {
fmtType, ok := fmtTypeEn.Get(x.FmtType)
if !ok {
clis.AbortOn("Get FmtType", fmt.Errorf("Invalid format string: '%s'\n", x.FmtType))
}
nameRoot := clis.Basename(filepath.Base(x.Filei))
if len(x.Name) != 0 {
nameRoot = x.Name
}
fileI := clis.GetInputStream(x.Filei)
defer fileI.Close()
output, err := gojson.Generate(fileI, gjparser[fmtType],
nameRoot, x.Pkg, []string{x.FmtType}, x.SubStruct)
clis.AbortOn("Gojson Parsing", err)
fileO := clis.GetOutputStream(x.Fileo)
defer fileO.Close()
fmt.Fprint(fileO, string(output))
// clis.WarnOn("J2s, Exec", err)
return nil
}