Skip to content

Commit 26f1343

Browse files
committed
- [#] switch code gen from cli to go-flags
1 parent bcb4c90 commit 26f1343

25 files changed

+743
-437
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,4 @@
1414
.glide/
1515
jsonfiddle
1616
jsonfiddle.exe
17+
test/*.got

cmd_esc.go

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
////////////////////////////////////////////////////////////////////////////
2+
// Program: jsonfiddle
3+
// Purpose: JSON Fiddling
4+
// Authors: Tong Sun (c) 2017-2023, All rights reserved
5+
////////////////////////////////////////////////////////////////////////////
6+
7+
package main
8+
9+
import (
10+
"fmt"
11+
"os"
12+
13+
"github.com/go-easygen/go-flags/clis"
14+
)
15+
16+
// *** Sub-command: esc ***
17+
18+
////////////////////////////////////////////////////////////////////////////
19+
// Constant and data type/structure definitions
20+
21+
// The EscCommand type defines all the configurable options from cli.
22+
type EscCommand struct {
23+
Filei string `short:"i" long:"input" description:"the source to get json string from (or \"-\" for stdin) (mandatory)" required:"true"`
24+
Fileo string `short:"o" long:"output" description:"the output, default to stdout" default:"-"`
25+
}
26+
27+
var escCommand EscCommand
28+
29+
func init() {
30+
parser.AddCommand("esc",
31+
"Escape json string",
32+
"",
33+
&escCommand)
34+
}
35+
36+
func (x *EscCommand) Execute(args []string) error {
37+
fmt.Fprintf(os.Stderr, "Escape json string\n")
38+
// fmt.Fprintf(os.Stderr, "Copyright (C) 2017-2023, Tong Sun\n\n")
39+
clis.Setup("jsonfiddle::esc", opts.Verbose)
40+
clis.Verbose(1, "Doing Esc, with %+v, %+v", opts, args)
41+
// fmt.Println(x.Filei, x.Fileo)
42+
return x.Exec(args)
43+
}
44+
45+
// Exec implements the business logic of command `esc`
46+
// func (x *EscCommand) Exec(args []string) error {
47+
// // err := ...
48+
// // clis.WarnOn("Esc, Exec", err)
49+
// // or,
50+
// // clis.AbortOn("Esc, Exec", err)
51+
// return nil
52+
// }

cmd_fmt.go

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
////////////////////////////////////////////////////////////////////////////
2+
// Program: jsonfiddle
3+
// Purpose: JSON Fiddling
4+
// Authors: Tong Sun (c) 2017-2023, All rights reserved
5+
////////////////////////////////////////////////////////////////////////////
6+
7+
package main
8+
9+
import (
10+
"fmt"
11+
"os"
12+
13+
"github.com/go-easygen/go-flags/clis"
14+
)
15+
16+
// *** Sub-command: fmt ***
17+
18+
////////////////////////////////////////////////////////////////////////////
19+
// Constant and data type/structure definitions
20+
21+
// The FmtCommand type defines all the configurable options from cli.
22+
type FmtCommand struct {
23+
Filei string `short:"i" long:"input" description:"the source to get json string from (mandatory)" required:"true"`
24+
Fileo string `short:"o" long:"output" description:"the output, default to stdout" default:"-"`
25+
}
26+
27+
var fmtCommand FmtCommand
28+
29+
func init() {
30+
parser.AddCommand("fmt",
31+
"Format json string",
32+
"",
33+
&fmtCommand)
34+
}
35+
36+
func (x *FmtCommand) Execute(args []string) error {
37+
fmt.Fprintf(os.Stderr, "Format json string\n")
38+
// fmt.Fprintf(os.Stderr, "Copyright (C) 2017-2023, Tong Sun\n\n")
39+
clis.Setup("jsonfiddle::fmt", opts.Verbose)
40+
clis.Verbose(1, "Doing Fmt, with %+v, %+v", opts, args)
41+
// fmt.Println(x.Filei, x.Fileo)
42+
return x.Exec(args)
43+
}
44+
45+
// Exec implements the business logic of command `fmt`
46+
// func (x *FmtCommand) Exec(args []string) error {
47+
// // err := ...
48+
// // clis.WarnOn("Fmt, Exec", err)
49+
// // or,
50+
// // clis.AbortOn("Fmt, Exec", err)
51+
// return nil
52+
// }

cmd_j2s.go

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
////////////////////////////////////////////////////////////////////////////
2+
// Program: jsonfiddle
3+
// Purpose: JSON Fiddling
4+
// Authors: Tong Sun (c) 2017-2023, All rights reserved
5+
////////////////////////////////////////////////////////////////////////////
6+
7+
package main
8+
9+
import (
10+
"fmt"
11+
"os"
12+
13+
"github.com/go-easygen/go-flags/clis"
14+
)
15+
16+
// *** Sub-command: j2s ***
17+
18+
////////////////////////////////////////////////////////////////////////////
19+
// Constant and data type/structure definitions
20+
21+
// The J2sCommand type defines all the configurable options from cli.
22+
type J2sCommand struct {
23+
FmtType string `short:"f" long:"fmt" description:"the structural format of the input data (json/yaml)" default:"json"`
24+
Filei string `short:"i" long:"input" description:"the source of the input JSON (mandatory)" required:"true"`
25+
Fileo string `short:"o" long:"output" description:"the output, default to stdout" default:"-"`
26+
Name string `long:"name" description:"the name of the root struct (default: as input file name)"`
27+
Pkg string `long:"pkg" description:"the name of the package for the generated code" default:"main"`
28+
SubStruct bool `long:"subStruct" description:"create types for sub-structs"`
29+
}
30+
31+
var j2sCommand J2sCommand
32+
33+
func init() {
34+
parser.AddCommand("j2s",
35+
"JSON to struct",
36+
"JSON convert to Go struct",
37+
&j2sCommand)
38+
}
39+
40+
func (x *J2sCommand) Execute(args []string) error {
41+
fmt.Fprintf(os.Stderr, "JSON to struct\n")
42+
// fmt.Fprintf(os.Stderr, "Copyright (C) 2017-2023, Tong Sun\n\n")
43+
clis.Setup("jsonfiddle::j2s", opts.Verbose)
44+
clis.Verbose(1, "Doing J2s, with %+v, %+v", opts, args)
45+
// fmt.Println(x.FmtType, x.Filei, x.Fileo, x.Name, x.Pkg, x.SubStruct)
46+
return x.Exec(args)
47+
}
48+
49+
// Exec implements the business logic of command `j2s`
50+
// func (x *J2sCommand) Exec(args []string) error {
51+
// // err := ...
52+
// // clis.WarnOn("J2s, Exec", err)
53+
// // or,
54+
// // clis.AbortOn("J2s, Exec", err)
55+
// return nil
56+
// }

cmd_sort.go

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
////////////////////////////////////////////////////////////////////////////
2+
// Program: jsonfiddle
3+
// Purpose: JSON Fiddling
4+
// Authors: Tong Sun (c) 2017-2023, All rights reserved
5+
////////////////////////////////////////////////////////////////////////////
6+
7+
package main
8+
9+
import (
10+
"fmt"
11+
"os"
12+
13+
"github.com/go-easygen/go-flags/clis"
14+
)
15+
16+
// *** Sub-command: sort ***
17+
18+
////////////////////////////////////////////////////////////////////////////
19+
// Constant and data type/structure definitions
20+
21+
// The SortCommand type defines all the configurable options from cli.
22+
type SortCommand struct {
23+
Filei string `short:"i" long:"input" description:"the source to get json string from (mandatory)" required:"true"`
24+
Fileo string `short:"o" long:"output" description:"the output, default to stdout" default:"-"`
25+
}
26+
27+
var sortCommand SortCommand
28+
29+
func init() {
30+
parser.AddCommand("sort",
31+
"Sort json fields recursively",
32+
"",
33+
&sortCommand)
34+
}
35+
36+
func (x *SortCommand) Execute(args []string) error {
37+
fmt.Fprintf(os.Stderr, "Sort json fields recursively\n")
38+
// fmt.Fprintf(os.Stderr, "Copyright (C) 2017-2023, Tong Sun\n\n")
39+
clis.Setup("jsonfiddle::sort", opts.Verbose)
40+
clis.Verbose(1, "Doing Sort, with %+v, %+v", opts, args)
41+
// fmt.Println(x.Filei, x.Fileo)
42+
return x.Exec(args)
43+
}
44+
45+
// Exec implements the business logic of command `sort`
46+
// func (x *SortCommand) Exec(args []string) error {
47+
// // err := ...
48+
// // clis.WarnOn("Sort, Exec", err)
49+
// // or,
50+
// // clis.AbortOn("Sort, Exec", err)
51+
// return nil
52+
// }

cmd_x2j.go

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
////////////////////////////////////////////////////////////////////////////
2+
// Program: jsonfiddle
3+
// Purpose: JSON Fiddling
4+
// Authors: Tong Sun (c) 2017-2023, All rights reserved
5+
////////////////////////////////////////////////////////////////////////////
6+
7+
package main
8+
9+
import (
10+
"fmt"
11+
"os"
12+
13+
"github.com/go-easygen/go-flags/clis"
14+
)
15+
16+
// *** Sub-command: x2j ***
17+
18+
////////////////////////////////////////////////////////////////////////////
19+
// Constant and data type/structure definitions
20+
21+
// The X2jCommand type defines all the configurable options from cli.
22+
type X2jCommand struct {
23+
Filei string `short:"i" long:"input" description:"the source of the input JSON (mandatory)" required:"true"`
24+
Fileo string `short:"o" long:"output" description:"the output, default to stdout" default:"-"`
25+
}
26+
27+
var x2jCommand X2jCommand
28+
29+
func init() {
30+
parser.AddCommand("x2j",
31+
"XML to JSON",
32+
"",
33+
&x2jCommand)
34+
}
35+
36+
func (x *X2jCommand) Execute(args []string) error {
37+
fmt.Fprintf(os.Stderr, "XML to JSON\n")
38+
// fmt.Fprintf(os.Stderr, "Copyright (C) 2017-2023, Tong Sun\n\n")
39+
clis.Setup("jsonfiddle::x2j", opts.Verbose)
40+
clis.Verbose(1, "Doing X2j, with %+v, %+v", opts, args)
41+
// fmt.Println(x.Filei, x.Fileo)
42+
return x.Exec(args)
43+
}
44+
45+
// Exec implements the business logic of command `x2j`
46+
// func (x *X2jCommand) Exec(args []string) error {
47+
// // err := ...
48+
// // clis.WarnOn("X2j, Exec", err)
49+
// // or,
50+
// // clis.AbortOn("X2j, Exec", err)
51+
// return nil
52+
// }

imp_esc.go

+11-16
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,29 @@
11
////////////////////////////////////////////////////////////////////////////
22
// Program: jsonfiddle
33
// Purpose: JSON Fiddling
4-
// Authors: Tong Sun (c) 2017, All rights reserved
4+
// Authors: Tong Sun (c) 2017-2023, All rights reserved
55
////////////////////////////////////////////////////////////////////////////
66

77
package main
88

99
import (
1010
"encoding/json"
1111
"fmt"
12-
"io/ioutil"
1312

14-
"github.com/mkideal/cli"
13+
"github.com/go-easygen/go-flags/clis"
1514
)
1615

17-
func escCLI(ctx *cli.Context) error {
18-
rootArgv = ctx.RootArgv().(*rootT)
19-
argv := ctx.Argv().(*escT)
20-
// fmt.Printf("[esc]:\n %+v\n %+v\n %v\n", rootArgv, argv, ctx.Args())
21-
Opts.Prefix, Opts.Indent, Opts.Compact, Opts.Verbose =
22-
rootArgv.Prefix, rootArgv.Indent, rootArgv.Compact, rootArgv.Verbose.Value()
23-
24-
data, err := ioutil.ReadAll(argv.Filei)
25-
argv.Filei.Close()
26-
abortOn("[::esc] Reading input", err)
27-
// fmt.Printf("] %s", data)
16+
// *** Sub-command: esc ***
17+
// Exec implements the business logic of command `esc`
18+
func (x *EscCommand) Exec(args []string) error {
19+
data := clis.ReadInput(x.Filei)
2820

2921
out, err := json.Marshal(string(data))
30-
abortOn("[::esc] Formatting input", err)
31-
fmt.Fprintf(argv.Fileo, "%s", out)
22+
clis.AbortOn("Formatting input", err)
23+
24+
fileO := clis.GetOutputStream(x.Fileo)
25+
defer fileO.Close()
3226

27+
fmt.Fprintf(fileO, "%s", out)
3328
return nil
3429
}

imp_fmt.go

+16-17
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,37 @@
11
////////////////////////////////////////////////////////////////////////////
22
// Program: jsonfiddle
33
// Purpose: JSON Fiddling
4-
// Authors: Tong Sun (c) 2017, All rights reserved
4+
// Authors: Tong Sun (c) 2017-2023, All rights reserved
55
////////////////////////////////////////////////////////////////////////////
66

77
package main
88

99
import (
1010
"bytes"
1111
"encoding/json"
12+
"fmt"
1213

13-
"github.com/mkideal/cli"
14+
"github.com/go-easygen/go-flags/clis"
1415
)
1516

16-
func fmtCLI(ctx *cli.Context) error {
17-
rootArgv = ctx.RootArgv().(*rootT)
18-
argv := ctx.Argv().(*fmtT)
19-
// fmt.Printf("[fmt]:\n %+v\n %+v\n %v\n", rootArgv, argv, ctx.Args())
20-
Opts.Prefix, Opts.Indent, Opts.Compact, Opts.Protect, Opts.Verbose =
21-
rootArgv.Prefix, rootArgv.Indent, rootArgv.Compact,
22-
rootArgv.Protect, rootArgv.Verbose.Value()
23-
24-
data := readJson(argv.Filei)
25-
argv.Filei.Close()
17+
// *** Sub-command: fmt ***
18+
// Exec implements the business logic of command `fmt`
19+
func (x *FmtCommand) Exec(args []string) error {
20+
fileI := clis.GetInputStream(x.Filei)
21+
defer fileI.Close()
22+
data := readJson(fileI)
2623

2724
var out bytes.Buffer
2825
var err error
29-
if Opts.Compact {
26+
if opts.Compact {
3027
err = json.Compact(&out, data)
3128
} else {
32-
err = json.Indent(&out, data, Opts.Prefix, Opts.Indent)
29+
err = json.Indent(&out, data, opts.Prefix, opts.Indent)
3330
}
34-
abortOn("[::fmt] Formatting input", err)
35-
out.WriteTo(argv.Fileo)
36-
31+
clis.AbortOn("Formatting input", err)
32+
fileO := clis.GetOutputStream(x.Fileo)
33+
defer fileO.Close()
34+
out.WriteTo(fileO)
35+
fmt.Fprintln(fileO)
3736
return nil
3837
}

0 commit comments

Comments
 (0)