-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommand_method.go
152 lines (137 loc) · 4.73 KB
/
command_method.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package generator
import (
"encoding/json"
"flag"
"fmt"
"github.com/ar-mokhtari/orginfo-generator/entity"
"github.com/ar-mokhtari/orginfo-generator/env"
preparename "github.com/ar-mokhtari/orginfo-generator/prepare_name"
"os"
"strings"
)
type (
InputData struct {
DomainName string `json:"domain_name"`
Fields []entity.Field `json:"fields"`
}
)
type newCommand struct {
Fs *flag.FlagSet
FlagDomainNameValue string
FlagFieldsValue string
FlagFromFile bool
}
func (nc *newCommand) Name() string { return nc.Fs.Name() }
func (nc *newCommand) Init(args []string) error {
return nc.Fs.Parse(args)
}
func (nc *newCommand) Run() (err error) {
switch nc.FlagFromFile {
case false:
//first, prepare domain_name
res, prepareErr := preparename.PrepareDomainName(nc.FlagDomainNameValue)
if prepareErr != nil {
return prepareErr
}
//next prepare fields
fields := strings.Split(nc.FlagFieldsValue, ",")
var finalFields []entity.Field
if len(fields) != 0 {
for _, field := range fields {
splitField := strings.Split(field, "-")
sliceCount := len(splitField)
if sliceCount != 3 {
return fmt.Errorf("fields have to content 3 parts: name-type-jsonKey, your input have %d, insert data is: %s", sliceCount, field)
}
resFields, prepareFieldErr := preparename.PrepareFieldsName(splitField[0], splitField[1], splitField[2])
if prepareFieldErr != nil {
return prepareFieldErr
}
finalFields = append(finalFields, resFields)
}
}
res.Fields = finalFields
res.ProjectName = env.MainRepositoryPath
addInit(res)
case true:
allDomain, parseErr := parseDomainFromFile()
if parseErr != nil {
return parseErr
}
for _, domain := range allDomain {
res, prepareErr := preparename.PrepareDomainName(domain.DomainName)
if prepareErr != nil {
return prepareErr
}
for index, field := range domain.Fields {
domain.Fields[index], err = preparename.PrepareFieldsName(field.Name, field.Type, field.JsonName)
if err != nil {
return err
}
}
res.Fields = domain.Fields
res.ProjectName = env.MainRepositoryPath
addInit(res)
}
}
return nil
}
type helpCommand struct {
Fs *flag.FlagSet
FlagHelpValue string
}
func (nc *helpCommand) Name() string { return nc.Fs.Name() }
func (nc *helpCommand) Init(args []string) error {
return nc.Fs.Parse(args)
}
func (nc *helpCommand) Run() (err error) {
fmt.Println("----------- help menu -----------")
fmt.Print("Domain generator\nto create new domain with CRUD operation and other prerequisite: run, build and execute this file:\ndomain generator and follow command(s) ... like run this from '/bin/':\n\n cd go/src/orginfo/bin/\npattern #1\ninput from command and flag one by one:\n./generator sub-command -domain_name=\"DOMAIN NAME\" -fields=\"field1-string-field1_1,field2-uint-field1_2,...\"\nrun this for add a new domain:\n\n./generator new -domain_name=\"DOMAIN_NAME\" -fields=\"codeType-uint-code_type,code-uint-code\"\nAlso, to remove a domain run this:\n\n./generator delete -domain_name=\"DOMAIN_NAME\"\npattern #2\nimport domain(s) from json type file ...\n./generator new -from_file\nremove domain(s) from json type file ...\n./generator delete -from_file\nthe file name must be: input.json. in this address: /\nyou can find default of file in this address: sample structure\n[\n {\n \"domain_name\": \"string\",\n \"fields\": [\n {\n \"name\": \"string\",\n \"type\": \"string\",\n \"json_name\": \"string\"\n }\n ]\n }\n]\n")
fmt.Println("----------- help menu -----------")
return nil
}
type deleteCommand struct {
Fs *flag.FlagSet
FlagValue string
FlagFromFile bool
}
func (dc *deleteCommand) Name() string { return dc.Fs.Name() }
func (dc *deleteCommand) Init(args []string) error {
return dc.Fs.Parse(args)
}
func (dc *deleteCommand) Run() (err error) {
switch dc.FlagFromFile {
case false:
res, prepareErr := preparename.PrepareDomainName(dc.FlagValue)
if prepareErr != nil {
return prepareErr
}
deleteInit(res)
case true:
allDomain, parseErr := parseDomainFromFile()
if parseErr != nil {
return parseErr
}
for _, domain := range allDomain {
res, prepareErr := preparename.PrepareDomainName(domain.DomainName)
if prepareErr != nil {
return prepareErr
}
deleteInit(res)
}
}
return nil
}
func parseDomainFromFile() ([]InputData, error) {
//load and read input.json and convert json to struct (entity.Domain)
inputJson, jsonErr := os.ReadFile(env.MainPath + "cli/generator/" + "input.json")
if jsonErr != nil {
return nil, jsonErr
}
var allDomain []InputData
unmarshalErr := json.Unmarshal(inputJson, &allDomain)
if unmarshalErr != nil {
return nil, unmarshalErr
}
return allDomain, nil
}