Skip to content

Commit 54f8da4

Browse files
committed
Extract cmd logic into cmd package
1 parent 9b8a38b commit 54f8da4

File tree

4 files changed

+138
-123
lines changed

4 files changed

+138
-123
lines changed

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ version := $(shell git describe --tags)
44
.PHONY: build install test testrace coverage
55

66
build:
7-
go build -o ${output} -ldflags="-X main.version=${version}"
7+
go build -o ${output} -ldflags="-X github.com/marhaupe/json2struct/cmd.version=${version}"
88

99
install:
1010
go install

cmd/root.go

+115
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
package cmd
2+
3+
import (
4+
"bufio"
5+
"fmt"
6+
"io/ioutil"
7+
"os"
8+
9+
"github.com/marhaupe/json2struct/internal/editor"
10+
"github.com/marhaupe/json2struct/internal/generate"
11+
"github.com/marhaupe/json2struct/internal/lex"
12+
"github.com/spf13/cobra"
13+
)
14+
15+
var (
16+
inputString string
17+
inputFile string
18+
)
19+
20+
var rootCmd = &cobra.Command{
21+
Use: "json2struct",
22+
Short: "These are all available commands to help you parse JSONs to Go structs",
23+
Args: cobra.ExactArgs(0),
24+
Run: rootFunc,
25+
}
26+
27+
func init() {
28+
rootCmd.Flags().StringVarP(&inputString, "string", "s", "", "JSON string")
29+
rootCmd.Flags().StringVarP(&inputFile, "file", "f", "", "Path to JSON file")
30+
rootCmd.AddCommand(versionCmd)
31+
}
32+
33+
func Execute() {
34+
if err := rootCmd.Execute(); err != nil {
35+
fmt.Println(err)
36+
}
37+
}
38+
39+
func rootFunc(cmd *cobra.Command, args []string) {
40+
var res string
41+
42+
switch {
43+
case inputFile != "":
44+
res = generateFromFile()
45+
case inputString != "":
46+
res = generateFromString()
47+
default:
48+
res = generateFromEditor()
49+
}
50+
51+
fmt.Println(res)
52+
}
53+
54+
func generateFromFile() string {
55+
data, err := ioutil.ReadFile(inputFile)
56+
if err != nil {
57+
fmt.Println(err)
58+
os.Exit(4)
59+
}
60+
gen, err := generate.GenerateWithFormatting(string(data))
61+
if err != nil {
62+
fmt.Println(err)
63+
os.Exit(5)
64+
}
65+
return gen
66+
}
67+
68+
func generateFromString() string {
69+
gen, err := generate.GenerateWithFormatting(inputString)
70+
if err != nil {
71+
fmt.Println(err)
72+
os.Exit(1)
73+
}
74+
return gen
75+
}
76+
77+
func generateFromEditor() string {
78+
jsonstr := awaitValidInput()
79+
gen, err := generate.GenerateWithFormatting(jsonstr)
80+
if err != nil {
81+
os.Exit(2)
82+
}
83+
return gen
84+
}
85+
86+
func awaitValidInput() string {
87+
edit := editor.New()
88+
defer edit.Delete()
89+
edit.Display()
90+
91+
var jsonstr string
92+
jsonstr, _ = edit.Read()
93+
94+
isValid := lex.ValidateJSON(jsonstr)
95+
if isValid {
96+
return jsonstr
97+
}
98+
reader := bufio.NewReader(os.Stdin)
99+
for {
100+
fmt.Print("You supplied an invalid json. Do you want to fix it (y/n)? ")
101+
102+
input, _ := reader.ReadString('\n')
103+
userWantsFix := string(input[0]) == "y"
104+
if !userWantsFix {
105+
return ""
106+
}
107+
108+
edit.Display()
109+
jsonstr, _ = edit.Read()
110+
isValid := lex.ValidateJSON(jsonstr)
111+
if isValid {
112+
return jsonstr
113+
}
114+
}
115+
}

cmd/version.go

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package cmd
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/spf13/cobra"
7+
)
8+
9+
var version string
10+
11+
var versionCmd = &cobra.Command{
12+
Use: "version",
13+
Short: "Print the version number",
14+
Args: cobra.ExactArgs(0),
15+
Run: versionFunc,
16+
}
17+
18+
func versionFunc(cmd *cobra.Command, args []string) {
19+
fmt.Println(version)
20+
}

main.go

+2-122
Original file line numberDiff line numberDiff line change
@@ -1,129 +1,9 @@
11
package main
22

33
import (
4-
"bufio"
5-
"fmt"
6-
"io/ioutil"
7-
"os"
8-
9-
"github.com/marhaupe/json2struct/internal/editor"
10-
"github.com/marhaupe/json2struct/internal/generate"
11-
"github.com/marhaupe/json2struct/internal/lex"
12-
"github.com/spf13/cobra"
4+
"github.com/marhaupe/json2struct/cmd"
135
)
146

15-
var version string
16-
17-
// InputString may optionally contain the JSON provided by the user
18-
var InputString string
19-
20-
// InputFile may optionally contain the path to a JSON file provided by the user
21-
var InputFile string
22-
23-
var rootCmd = &cobra.Command{
24-
Use: "json2struct",
25-
Short: "These are all available commands to help you parse JSONs to Go structs",
26-
Args: cobra.ExactArgs(0),
27-
Run: rootFunc,
28-
}
29-
30-
var versionCmd = &cobra.Command{
31-
Use: "version",
32-
Short: "Print the version number",
33-
Args: cobra.ExactArgs(0),
34-
Run: cmdFunc,
35-
}
36-
37-
func init() {
38-
rootCmd.Flags().StringVarP(&InputString, "string", "s", "", "JSON string")
39-
rootCmd.Flags().StringVarP(&InputFile, "file", "f", "", "Path to JSON file")
40-
rootCmd.AddCommand(versionCmd)
41-
}
42-
437
func main() {
44-
if err := rootCmd.Execute(); err != nil {
45-
fmt.Println(err)
46-
}
47-
}
48-
49-
func cmdFunc(cmd *cobra.Command, args []string) {
50-
fmt.Println(version)
51-
}
52-
53-
func rootFunc(cmd *cobra.Command, args []string) {
54-
var res string
55-
56-
switch {
57-
case InputFile != "":
58-
res = generateFromFile()
59-
case InputString != "":
60-
res = generateFromString()
61-
default:
62-
res = generateFromEditor()
63-
}
64-
65-
fmt.Println(res)
66-
}
67-
68-
func generateFromFile() string {
69-
data, err := ioutil.ReadFile(InputFile)
70-
if err != nil {
71-
fmt.Println(err)
72-
os.Exit(4)
73-
}
74-
gen, err := generate.GenerateWithFormatting(string(data))
75-
if err != nil {
76-
fmt.Println(err)
77-
os.Exit(5)
78-
}
79-
return gen
80-
}
81-
82-
func generateFromString() string {
83-
gen, err := generate.GenerateWithFormatting(InputString)
84-
if err != nil {
85-
fmt.Println(err)
86-
os.Exit(1)
87-
}
88-
return gen
89-
}
90-
91-
func generateFromEditor() string {
92-
jsonstr := awaitValidInput()
93-
gen, err := generate.GenerateWithFormatting(jsonstr)
94-
if err != nil {
95-
os.Exit(2)
96-
}
97-
return gen
98-
}
99-
100-
func awaitValidInput() string {
101-
edit := editor.New()
102-
defer edit.Delete()
103-
edit.Display()
104-
105-
var jsonstr string
106-
jsonstr, _ = edit.Read()
107-
108-
isValid := lex.ValidateJSON(jsonstr)
109-
if isValid {
110-
return jsonstr
111-
}
112-
reader := bufio.NewReader(os.Stdin)
113-
for {
114-
fmt.Print("You supplied an invalid json. Do you want to fix it (y/n)? ")
115-
116-
input, _ := reader.ReadString('\n')
117-
userWantsFix := string(input[0]) == "y"
118-
if !userWantsFix {
119-
return ""
120-
}
121-
122-
edit.Display()
123-
jsonstr, _ = edit.Read()
124-
isValid := lex.ValidateJSON(jsonstr)
125-
if isValid {
126-
return jsonstr
127-
}
128-
}
8+
cmd.Execute()
1299
}

0 commit comments

Comments
 (0)