/
make_command.go
151 lines (124 loc) · 4.65 KB
/
make_command.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
package config
import (
"errors"
"github.com/edwinvautier/go-gadgeto/helpers"
"github.com/edwinvautier/go-gadgeto/prompt/modelPrompt"
"github.com/edwinvautier/go-gadgeto/services/filesystem"
"github.com/gobuffalo/packr/v2"
"github.com/mitchellh/mapstructure"
log "github.com/sirupsen/logrus"
"github.com/spf13/viper"
)
// MakeCmdConfig is the struct used to configure make command
type MakeCmdConfig struct {
GoPackageFullPath string
Box *packr.Box
Model modelPrompt.NewModel
ProjectPath string
FixturesModels []string
}
// GetBox returns the box in which templates for make command are stored
func (cmd MakeCmdConfig) GetBox() *packr.Box {
return cmd.Box
}
// GetProjectPath returns the path to project in user's computer
func (cmd MakeCmdConfig) GetProjectPath() string {
return cmd.ProjectPath
}
// InitMakeModelCmdConfig creates the needed config for the create command by prompting user and doing other actions
func InitMakeModelCmdConfig(config *MakeCmdConfig) error {
configBase := initBasicConfig()
config.GoPackageFullPath = configBase.PackagePath
config.ProjectPath = configBase.ProjectPath
config.Box = packr.New("makeModelBox", "../templates/makeModel")
config.Model.NameLowerCase = helpers.LowerCase(config.Model.Name)
config.Model.NamePascalCase = helpers.UpperCaseFirstChar(config.Model.Name)
return modelPrompt.PromptUserForModelFields(&config.Model)
}
// AddModelToConfig set the new bundle to true in config after install
func AddModelToConfig(newModel modelPrompt.NewModel) error {
workdir := filesystem.GetWorkdirOrDie()
viper.AddConfigPath(workdir)
viper.SetConfigName(".go-gadgeto-config")
viper.SetDefault("models", map[string]map[string]string{})
// If a config file is found, read it in.
if err := viper.ReadInConfig(); err != nil {
return err
}
models := viper.GetStringMap("models")
models[newModel.Name] = newModel
viper.Set("models", models)
viper.WriteConfig()
return nil
}
// InitMakeCRUDCmdConfig creates the needed config for the make crud command
func InitModelConfig(config *MakeCmdConfig) error {
// Get model from config
if err := InitViper(); err != nil {
return errors.New("couldn't load config, try again")
}
if !IsInConfig(config.Model.Name) {
return errors.New("could'nt find this model, run go-gadgeto update or go-gadgeto make model to fix it")
}
modelData := viper.GetStringMap("models." + config.Model.Name)
var model modelPrompt.NewModel
if err := mapstructure.Decode(modelData, &model); err != nil {
return errors.New("error while decoding " + config.Model.Name + " from config")
}
config.Model = model
configBase := initBasicConfig()
config.GoPackageFullPath = configBase.PackagePath
config.ProjectPath = configBase.ProjectPath
return nil
}
// InitMakeCRUDCmdConfig inits a config for the make CRUD command
func InitMakeCRUDCmdConfig(config *MakeCmdConfig) error {
if err := InitModelConfig(config); err != nil {
return err
}
config.Box = packr.New("makeCRUDBox", "../templates/makeCRUD")
return nil
}
// InitMakeFixturesCmdConfig inits a config for the make fixtures command
func InitMakeFixturesCmdConfig(config *MakeCmdConfig) error {
if err := InitModelConfig(config); err != nil {
return err
}
config.Box = packr.New("makeFixturesBox", "../templates/makeFixtures")
config.FixturesModels = filesystem.GetFixturesModelsList()
if !helpers.ContainsString(config.FixturesModels, config.Model.NamePascalCase) {
config.FixturesModels = append(config.FixturesModels, config.Model.NamePascalCase)
}
return nil
}
// InitMakeTestsCmdConfig inits a config for the make tests command
func InitMakeTestsCmdConfig(config *MakeCmdConfig) error {
if err := InitModelConfig(config); err != nil {
return err
}
config.Box = packr.New("makeTestsBox", "../templates/makeTests")
return nil
}
// IsInConfig returns a boolean telling wether the modelName was found in config or not
func IsInConfig(modelName string) bool {
if err := InitViper(); err != nil {
return false
}
modelsStructs := viper.GetStringMap("models")
var models []string
for key := range modelsStructs {
models = append(models, key)
}
return helpers.ContainsString(models, modelName)
}
// InitUpdateModelConfig is the same as initMakeModelConfig but it gather infos from config first
func InitUpdateModelConfig(config *MakeCmdConfig) error {
log.Info("model already exists! Select fields to add :")
modelData := viper.GetStringMap("models." + config.Model.Name)
var model modelPrompt.NewModel
if err := mapstructure.Decode(modelData, &model); err != nil {
return errors.New("error while decoding " + config.Model.Name + " from config")
}
config.Model = model
return InitMakeModelCmdConfig(config)
}