-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathflagx.go
More file actions
90 lines (76 loc) · 2.04 KB
/
flagx.go
File metadata and controls
90 lines (76 loc) · 2.04 KB
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
package main
import (
"encoding/json"
"flag"
"fmt"
"github.com/maolinc/gencode/core"
"github.com/maolinc/gencode/tools/filex"
"log"
"os"
"strings"
)
type JsonConfig struct {
DBConfig *gencode.DBConfig
GlobalConfig *gencode.Config
ApiConfig *gencode.ApiSchema
ProtoConfig *gencode.ProtoSchema
ModelConfig *gencode.ModelSchema
}
func getDefaultConfig() *JsonConfig {
jsonConfig := &JsonConfig{
DBConfig: &gencode.DBConfig{
DbType: "mysql",
},
GlobalConfig: &gencode.Config{
FieldStyle: "mLc",
},
ApiConfig: &gencode.ApiSchema{Dataset: &gencode.Dataset{SessionConfig: &gencode.SessionConfig{}}, ApiConfig: &gencode.ApiConfig{Switch: "A"}},
ProtoConfig: &gencode.ProtoSchema{Dataset: &gencode.Dataset{SessionConfig: &gencode.SessionConfig{}}, ProtoConfig: &gencode.ProtoConfig{Switch: "A"}},
ModelConfig: &gencode.ModelSchema{Dataset: &gencode.Dataset{SessionConfig: &gencode.SessionConfig{}}, ModelConfig: &gencode.ModelConfig{Switch: "A"}},
}
return jsonConfig
}
func parseFlag(jsonConfig *JsonConfig) (err error) {
templateFlag := flag.String("t", "", "input 'init', init template file")
configFlag := flag.String("f", "genConfig.json", "json config file")
flag.Parse()
err = checkTemplate()
if err != nil {
log.Fatal(err)
}
if *templateFlag == "init" {
if err := initTemplate(); err != nil {
return err
}
}
if !strings.Contains(*configFlag, ".json") {
fmt.Println(" - please input the json config file ")
return nil
}
err = checkTemplate()
if err != nil {
return err
}
fileContent, _ := os.ReadFile(*configFlag)
err = json.Unmarshal(fileContent, jsonConfig)
if err != nil {
return err
}
return nil
}
func checkTemplate() error {
templateDir := filex.GetHomeDir() + "/" + gencode.TemplatePath
exists, err := filex.PathExists(templateDir)
if err != nil {
return err
}
if exists {
return nil
}
return initTemplate()
}
func initTemplate() (err error) {
templateDir := filex.GetHomeDir() + "/" + gencode.TemplatePath
err = filex.CopyDirEm(dir, templateDir, "template")
return err
}