Skip to content

Commit

Permalink
refactor: configmanager package's readability enhancement
Browse files Browse the repository at this point in the history
Signed-off-by: Daniel Hu <tao.hu@merico.dev>
  • Loading branch information
daniel-hutao committed Nov 22, 2022
1 parent a50e5b6 commit 56cd984
Show file tree
Hide file tree
Showing 15 changed files with 346 additions and 342 deletions.
4 changes: 2 additions & 2 deletions cmd/devstream/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ DevStream will generate and execute a new plan based on the config file and the

func applyCMDFunc(cmd *cobra.Command, args []string) {
log.Info("Apply started.")
if err := pluginengine.Apply(configFile, continueDirectly); err != nil {
if err := pluginengine.Apply(configFilePath, continueDirectly); err != nil {
log.Errorf("Apply failed => %s.", err)
os.Exit(1)
}
log.Success("Apply finished.")
}

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

Expand Down
2 changes: 1 addition & 1 deletion cmd/devstream/common.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package main

var (
configFile string
configFilePath string
pluginDir string
continueDirectly bool
)
Expand Down
4 changes: 2 additions & 2 deletions cmd/devstream/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ DevStream will delete everything defined in the config file, regardless of the s

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

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

Expand Down
4 changes: 2 additions & 2 deletions cmd/devstream/destroy.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ var destroyCMD = &cobra.Command{

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

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

Expand Down
4 changes: 2 additions & 2 deletions cmd/devstream/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func initCMDFunc(_ *cobra.Command, _ []string) {
}

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

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

// downloading specific plugins from flags
Expand Down
4 changes: 2 additions & 2 deletions cmd/devstream/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func showConfigCMDFunc(_ *cobra.Command, _ []string) {

func showStatusCMDFunc(_ *cobra.Command, _ []string) {
log.Debug("Show status information.")
if err := status.Show(configFile); err != nil {
if err := status.Show(configFilePath); err != nil {
log.Fatal(err)
}
}
Expand All @@ -68,6 +68,6 @@ func init() {
showStatusCMD.Flags().StringVarP(&instanceID, "id", "i", "", "specify id with the plugin instance")
showStatusCMD.Flags().BoolVarP(&statusAllFlag, "all", "a", false, "show all instances of all plugins status")
showStatusCMD.Flags().StringVarP(&pluginDir, "plugin-dir", "d", "", "plugins directory")
showStatusCMD.Flags().StringVarP(&configFile, "config-file", "f", "config.yaml", "config file")
showStatusCMD.Flags().StringVarP(&configFilePath, "config-file", "f", "config.yaml", "config file")
completion.FlagPluginsCompletion(showStatusCMD, "plugin")
}
4 changes: 2 additions & 2 deletions cmd/devstream/verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ var verifyCMD = &cobra.Command{

func verifyCMDFunc(cmd *cobra.Command, args []string) {
log.Info("Verify started.")
if pluginengine.Verify(configFile) {
if pluginengine.Verify(configFilePath) {
log.Success("Verify succeeded.")
} else {
log.Info("Verify finished.")
}
}

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

completion.FlagFilenameCompletion(verifyCMD, configFlagName)
Expand Down
30 changes: 14 additions & 16 deletions internal/pkg/configmanager/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,14 @@ const (
repoScaffoldingPluginName = "repo-scaffolding"
)

type (
appRaw struct {
Name string `yaml:"name" mapstructure:"name"`
Spec map[string]any `yaml:"spec" mapstructure:"spec"`
Repo *scm.SCMInfo `yaml:"repo" mapstructure:"repo"`
RepoTemplate *scm.SCMInfo `yaml:"repoTemplate" mapstructure:"repoTemplate"`
CIRawConfigs []pipelineRaw `yaml:"ci" mapstructure:"ci"`
CDRawConfigs []pipelineRaw `yaml:"cd" mapstructure:"cd"`
}
)
type RawApp struct {
Name string `yaml:"name" mapstructure:"name"`
Spec map[string]any `yaml:"spec" mapstructure:"spec"`
Repo *scm.SCMInfo `yaml:"repo" mapstructure:"repo"`
RepoTemplate *scm.SCMInfo `yaml:"repoTemplate" mapstructure:"repoTemplate"`
CIRawConfigs []pipelineRaw `yaml:"ci" mapstructure:"ci"`
CDRawConfigs []pipelineRaw `yaml:"cd" mapstructure:"cd"`
}

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

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

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

// setDefault will set repoName to appName if repo.name field is empty
func (a *appRaw) setDefault() {
func (a *RawApp) setDefault() {
if a.Repo != nil && a.Repo.Name == "" {
a.Repo.Name = a.Name
}
}

// since all plugin depends on code is deployed, get dependsOn for repoTemplate
func (a *appRaw) getRepoTemplateDependants() []string {
func (a *RawApp) getRepoTemplateDependants() []string {
var dependsOn []string
// if a.RepoTemplate is configured, pipeline need to wait reposcaffolding finished
if a.RepoTemplate != nil {
Expand Down
6 changes: 3 additions & 3 deletions internal/pkg/configmanager/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,9 +195,9 @@ options:
})
})

var _ = Describe("appRaw struct", func() {
var _ = Describe("RawApp struct", func() {
var (
a *appRaw
a *RawApp
appName string
rawConfig []pipelineRaw
templateMap map[string]string
Expand All @@ -211,7 +211,7 @@ var _ = Describe("appRaw struct", func() {
When("repoInfo is not config", func() {
BeforeEach(func() {
appName = "test"
a = &appRaw{
a = &RawApp{
Repo: &scm.SCMInfo{},
Name: appName,
}
Expand Down
Loading

0 comments on commit 56cd984

Please sign in to comment.