Skip to content

Commit 17abf65

Browse files
committed
refactor: configmanager package's readability enhancement
Signed-off-by: Daniel Hu <tao.hu@merico.dev>
1 parent a50e5b6 commit 17abf65

15 files changed

Lines changed: 350 additions & 342 deletions

File tree

cmd/devstream/apply.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,15 @@ DevStream will generate and execute a new plan based on the config file and the
2121

2222
func applyCMDFunc(cmd *cobra.Command, args []string) {
2323
log.Info("Apply started.")
24-
if err := pluginengine.Apply(configFile, continueDirectly); err != nil {
24+
if err := pluginengine.Apply(configFilePath, continueDirectly); err != nil {
2525
log.Errorf("Apply failed => %s.", err)
2626
os.Exit(1)
2727
}
2828
log.Success("Apply finished.")
2929
}
3030

3131
func init() {
32-
applyCMD.Flags().StringVarP(&configFile, configFlagName, "f", "config.yaml", "config file")
32+
applyCMD.Flags().StringVarP(&configFilePath, configFlagName, "f", "config.yaml", "config file")
3333
applyCMD.Flags().StringVarP(&pluginDir, pluginDirFlagName, "d", "", "plugins directory")
3434
applyCMD.Flags().BoolVarP(&continueDirectly, "yes", "y", false, "apply directly without confirmation")
3535

cmd/devstream/common.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package main
22

33
var (
4-
configFile string
4+
configFilePath string
55
pluginDir string
66
continueDirectly bool
77
)

cmd/devstream/delete.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ DevStream will delete everything defined in the config file, regardless of the s
2222

2323
func deleteCMDFunc(cmd *cobra.Command, args []string) {
2424
log.Info("Delete started.")
25-
if err := pluginengine.Remove(configFile, continueDirectly, isForceDelete); err != nil {
25+
if err := pluginengine.Remove(configFilePath, continueDirectly, isForceDelete); err != nil {
2626
log.Errorf("Delete error: %s.", err)
2727
os.Exit(1)
2828
}
@@ -32,7 +32,7 @@ func deleteCMDFunc(cmd *cobra.Command, args []string) {
3232

3333
func init() {
3434
deleteCMD.Flags().BoolVarP(&isForceDelete, "force", "", false, "force delete by config")
35-
deleteCMD.Flags().StringVarP(&configFile, configFlagName, "f", "config.yaml", "config file")
35+
deleteCMD.Flags().StringVarP(&configFilePath, configFlagName, "f", "config.yaml", "config file")
3636
deleteCMD.Flags().StringVarP(&pluginDir, pluginDirFlagName, "d", "", "plugins directory")
3737
deleteCMD.Flags().BoolVarP(&continueDirectly, "yes", "y", false, "delete directly without confirmation")
3838

cmd/devstream/destroy.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ var destroyCMD = &cobra.Command{
2121

2222
func destroyCMDFunc(cmd *cobra.Command, args []string) {
2323
log.Info("Destroy started.")
24-
if err := pluginengine.Destroy(configFile, continueDirectly, isForceDestroy); err != nil {
24+
if err := pluginengine.Destroy(configFilePath, continueDirectly, isForceDestroy); err != nil {
2525
log.Errorf("Destroy failed => %s.", err)
2626
os.Exit(1)
2727
}
@@ -30,7 +30,7 @@ func destroyCMDFunc(cmd *cobra.Command, args []string) {
3030

3131
func init() {
3232
destroyCMD.Flags().BoolVarP(&isForceDestroy, "force", "", false, "force destroy by config")
33-
destroyCMD.Flags().StringVarP(&configFile, configFlagName, "f", "config.yaml", "config file")
33+
destroyCMD.Flags().StringVarP(&configFilePath, configFlagName, "f", "config.yaml", "config file")
3434
destroyCMD.Flags().StringVarP(&pluginDir, pluginDirFlagName, "d", "", "plugins directory")
3535
destroyCMD.Flags().BoolVarP(&continueDirectly, "yes", "y", false, "destroy directly without confirmation")
3636

cmd/devstream/init.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ func initCMDFunc(_ *cobra.Command, _ []string) {
6565
}
6666

6767
func GetPluginsAndPluginDirFromConfig() (tools []configmanager.Tool, pluginDir string, err error) {
68-
cfg, err := configmanager.NewManager(configFile).LoadConfig()
68+
cfg, err := configmanager.NewManager(configFilePath).LoadConfig()
6969
if err != nil {
7070
return nil, "", err
7171
}
@@ -125,7 +125,7 @@ func GetPluginsAndPluginDirFromFlags() (tools []configmanager.Tool, pluginDir st
125125

126126
func init() {
127127
// flags for init from config file
128-
initCMD.Flags().StringVarP(&configFile, configFlagName, "f", "config.yaml", "config file")
128+
initCMD.Flags().StringVarP(&configFilePath, configFlagName, "f", "config.yaml", "config file")
129129
initCMD.Flags().StringVarP(&pluginDir, pluginDirFlagName, "d", "", "plugins directory")
130130

131131
// downloading specific plugins from flags

cmd/devstream/show.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func showConfigCMDFunc(_ *cobra.Command, _ []string) {
5151

5252
func showStatusCMDFunc(_ *cobra.Command, _ []string) {
5353
log.Debug("Show status information.")
54-
if err := status.Show(configFile); err != nil {
54+
if err := status.Show(configFilePath); err != nil {
5555
log.Fatal(err)
5656
}
5757
}
@@ -68,6 +68,6 @@ func init() {
6868
showStatusCMD.Flags().StringVarP(&instanceID, "id", "i", "", "specify id with the plugin instance")
6969
showStatusCMD.Flags().BoolVarP(&statusAllFlag, "all", "a", false, "show all instances of all plugins status")
7070
showStatusCMD.Flags().StringVarP(&pluginDir, "plugin-dir", "d", "", "plugins directory")
71-
showStatusCMD.Flags().StringVarP(&configFile, "config-file", "f", "config.yaml", "config file")
71+
showStatusCMD.Flags().StringVarP(&configFilePath, "config-file", "f", "config.yaml", "config file")
7272
completion.FlagPluginsCompletion(showStatusCMD, "plugin")
7373
}

cmd/devstream/verify.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,15 @@ var verifyCMD = &cobra.Command{
1717

1818
func verifyCMDFunc(cmd *cobra.Command, args []string) {
1919
log.Info("Verify started.")
20-
if pluginengine.Verify(configFile) {
20+
if pluginengine.Verify(configFilePath) {
2121
log.Success("Verify succeeded.")
2222
} else {
2323
log.Info("Verify finished.")
2424
}
2525
}
2626

2727
func init() {
28-
verifyCMD.Flags().StringVarP(&configFile, configFlagName, "f", "config.yaml", "config file")
28+
verifyCMD.Flags().StringVarP(&configFilePath, configFlagName, "f", "config.yaml", "config file")
2929
verifyCMD.Flags().StringVarP(&pluginDir, pluginDirFlagName, "d", "", "plugins directory")
3030

3131
completion.FlagFilenameCompletion(verifyCMD, configFlagName)

internal/pkg/configmanager/app.go

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,14 @@ const (
1414
repoScaffoldingPluginName = "repo-scaffolding"
1515
)
1616

17-
type (
18-
appRaw struct {
19-
Name string `yaml:"name" mapstructure:"name"`
20-
Spec map[string]any `yaml:"spec" mapstructure:"spec"`
21-
Repo *scm.SCMInfo `yaml:"repo" mapstructure:"repo"`
22-
RepoTemplate *scm.SCMInfo `yaml:"repoTemplate" mapstructure:"repoTemplate"`
23-
CIRawConfigs []pipelineRaw `yaml:"ci" mapstructure:"ci"`
24-
CDRawConfigs []pipelineRaw `yaml:"cd" mapstructure:"cd"`
25-
}
26-
)
17+
type RawApp struct {
18+
Name string `yaml:"name" mapstructure:"name"`
19+
Spec map[string]any `yaml:"spec" mapstructure:"spec"`
20+
Repo *scm.SCMInfo `yaml:"repo" mapstructure:"repo"`
21+
RepoTemplate *scm.SCMInfo `yaml:"repoTemplate" mapstructure:"repoTemplate"`
22+
CIRawConfigs []pipelineRaw `yaml:"ci" mapstructure:"ci"`
23+
CDRawConfigs []pipelineRaw `yaml:"cd" mapstructure:"cd"`
24+
}
2725

2826
// getToolsFromApp return app tools
2927
func getToolsFromApp(appStr string, globalVars map[string]any, templateMap map[string]string) (Tools, error) {
@@ -33,8 +31,8 @@ func getToolsFromApp(appStr string, globalVars map[string]any, templateMap map[s
3331
log.Debugf("configmanager/app %s render globalVars %+v failed", appRenderStr, globalVars)
3432
return nil, fmt.Errorf("app render globalVars failed: %w", err)
3533
}
36-
// 2. unmarshal appRaw config for render pipelineTemplate
37-
var rawData appRaw
34+
// 2. unmarshal RawApp config for render pipelineTemplate
35+
var rawData RawApp
3836
if err := yaml.Unmarshal([]byte(appRenderStr), &rawData); err != nil {
3937
return nil, fmt.Errorf("app parse yaml failed: %w", err)
4038
}
@@ -57,7 +55,7 @@ func getToolsFromApp(appStr string, globalVars map[string]any, templateMap map[s
5755
}
5856

5957
// getAppPipelineTool generate ci/cd tools from app config
60-
func (a *appRaw) generateCICDToolsFromAppConfig(templateMap map[string]string, appVars map[string]any) (Tools, error) {
58+
func (a *RawApp) generateCICDToolsFromAppConfig(templateMap map[string]string, appVars map[string]any) (Tools, error) {
6159
allPipelineRaw := append(a.CIRawConfigs, a.CDRawConfigs...)
6260
var tools Tools
6361
for _, p := range allPipelineRaw {
@@ -76,7 +74,7 @@ func (a *appRaw) generateCICDToolsFromAppConfig(templateMap map[string]string, a
7674
}
7775

7876
// getRepoTemplateTool will use repo-scaffolding plugin for app
79-
func (a *appRaw) getRepoTemplateTool(appVars map[string]any) (*Tool, error) {
77+
func (a *RawApp) getRepoTemplateTool(appVars map[string]any) (*Tool, error) {
8078
if a.Repo == nil {
8179
return nil, fmt.Errorf("app.repo field can't be empty")
8280
}
@@ -101,14 +99,14 @@ func (a *appRaw) getRepoTemplateTool(appVars map[string]any) (*Tool, error) {
10199
}
102100

103101
// setDefault will set repoName to appName if repo.name field is empty
104-
func (a *appRaw) setDefault() {
102+
func (a *RawApp) setDefault() {
105103
if a.Repo != nil && a.Repo.Name == "" {
106104
a.Repo.Name = a.Name
107105
}
108106
}
109107

110108
// since all plugin depends on code is deployed, get dependsOn for repoTemplate
111-
func (a *appRaw) getRepoTemplateDependants() []string {
109+
func (a *RawApp) getRepoTemplateDependants() []string {
112110
var dependsOn []string
113111
// if a.RepoTemplate is configured, pipeline need to wait reposcaffolding finished
114112
if a.RepoTemplate != nil {

internal/pkg/configmanager/app_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -195,9 +195,9 @@ options:
195195
})
196196
})
197197

198-
var _ = Describe("appRaw struct", func() {
198+
var _ = Describe("RawApp struct", func() {
199199
var (
200-
a *appRaw
200+
a *RawApp
201201
appName string
202202
rawConfig []pipelineRaw
203203
templateMap map[string]string
@@ -211,7 +211,7 @@ var _ = Describe("appRaw struct", func() {
211211
When("repoInfo is not config", func() {
212212
BeforeEach(func() {
213213
appName = "test"
214-
a = &appRaw{
214+
a = &RawApp{
215215
Repo: &scm.SCMInfo{},
216216
Name: appName,
217217
}

0 commit comments

Comments
 (0)