grim/convey

310962069a9a
Parents 93014ac12e40
Children 2c339dc89014
Clean up some of the complexity of loaders/convey/convey.go
--- a/loaders/convey/convey.go Sun Jan 14 03:13:49 2018 -0600
+++ b/loaders/convey/convey.go Sun Jan 14 04:07:43 2018 -0600
@@ -63,6 +63,76 @@
fileLoader func(string, *Loader) (*cConfig.Config, error)
}
+func (l *Loader) loadBase(cfg config, path string, disableDeprecated bool) (*cConfig.Config, error) {
+ // see if we're extending something - partially load a base
+ // config object if so that we'll modify; otherwise use an
+ // empty base config object that we'll set in a similar way
+ if cfg.Extends != "" {
+ if l.depth >= MaxExtendsDepth {
+ return nil, ErrMaxExtendsDepth
+ }
+
+ extendsAbsName := filepath.Join(path, cfg.Extends)
+
+ l.depth++
+ baseConfig, err := l.loadFile(extendsAbsName, disableDeprecated)
+ l.depth--
+
+ // We can safely ignore no plans and no tasks errors here
+ // as we're ensured to also get a valid base config back.
+ // This is a bit of a strange idiom, but is still used in
+ // places like io.Reader (return non-zero n on error).
+ if err != nil && err != ErrNoPlans && err != ErrNoTasks {
+ return nil, err
+ }
+
+ return baseConfig, nil
+ }
+
+ return &cConfig.Config{
+ Tasks: map[string]tasks.Task{},
+ Plans: map[string]plans.Plan{},
+ MetaPlans: map[string]plans.MetaPlan{},
+ SSHIdentities: []string{},
+ }, nil
+}
+
+func (l *Loader) loadPlans(baseConfig *cConfig.Config, cfg config) error {
+ // iterate through each plan and do any cleanup we need to
+ for _, plan := range cfg.Plans {
+ // set stage names for any that are missing them
+ for idx := range plan.Stages {
+ if plan.Stages[idx].Name == "" {
+ plan.Stages[idx].Name = fmt.Sprintf("stage-%d", idx)
+ }
+ }
+ }
+
+ // If the plan has the merge attribute set, try to overwrite
+ // stages of a plan that is already declared with the same name.
+ // If no such plan exists, raise an error (you're trying to
+ // overwrite a stage of nothing, which is almost certainly an error.
+ for name, plan := range cfg.Plans {
+ if plan.Merge {
+ base, ok := baseConfig.Plans[name]
+ if !ok {
+ return fmt.Errorf("cannot merge with unknown plan '%s'", name)
+ }
+
+ base, err := mergePlan(base, plan, name)
+ if err != nil {
+ return err
+ }
+
+ baseConfig.Plans[name] = base
+ } else {
+ baseConfig.Plans[name] = plan
+ }
+ }
+
+ return nil
+}
+
// Load loads the given filename and returns it as a config.Config.
func (l *Loader) Load(path, base string, data []byte, disableDeprecated bool) (*cConfig.Config, error) {
if l.logger == nil {
@@ -76,37 +146,10 @@
return nil, err
}
- // see if we're extending something - partially load a base
- // config object if so that we'll modify; otherwise use an
- // empty base config object that we'll set in a similar way
-
- var baseConfig *cConfig.Config
-
- if cfg.Extends != "" {
- if l.depth >= MaxExtendsDepth {
- return nil, ErrMaxExtendsDepth
- }
-
- extendsAbsName := filepath.Join(path, cfg.Extends)
-
- l.depth++
- baseConfig, err = l.loadFile(extendsAbsName, disableDeprecated)
- l.depth--
-
- // We can safely ignore no plans and no tasks errors here
- // as we're ensured to also get a valid base config back.
- // This is a bit of a strange idiom, but is still used in
- // places like io.Reader (return non-zero n on error).
- if err != nil && err != ErrNoPlans && err != ErrNoTasks {
- return nil, err
- }
- } else {
- baseConfig = &cConfig.Config{
- Tasks: map[string]tasks.Task{},
- Plans: map[string]plans.Plan{},
- MetaPlans: map[string]plans.MetaPlan{},
- SSHIdentities: []string{},
- }
+ // get our base config
+ baseConfig, err := l.loadBase(cfg, path, disableDeprecated)
+ if err != nil {
+ return nil, err
}
// turn the raw tasks into real tasks
@@ -115,14 +158,9 @@
return nil, err
}
- // iterate through each plan and do any cleanup we need to
- for _, plan := range cfg.Plans {
- // set stage names for any that are missing them
- for idx := range plan.Stages {
- if plan.Stages[idx].Name == "" {
- plan.Stages[idx].Name = fmt.Sprintf("stage-%d", idx)
- }
- }
+ err = l.loadPlans(baseConfig, cfg)
+ if err != nil {
+ return nil, err
}
// store the default plan in the loader
@@ -137,29 +175,6 @@
baseConfig.Tasks[name] = task
}
- // If the plan has the merge attribute set, try to overwrite
- // stages of a plan that is already declared with the same name.
- // If no such plan exists, raise an error (you're trying to
- // overwrite a stage of nothing, which is almost certainly an error.
-
- for name, plan := range cfg.Plans {
- if plan.Merge {
- base, ok := baseConfig.Plans[name]
- if !ok {
- return nil, fmt.Errorf("cannot merge with unknown plan '%s'", name)
- }
-
- base, err := mergePlan(base, plan, name)
- if err != nil {
- return nil, err
- }
-
- baseConfig.Plans[name] = base
- } else {
- baseConfig.Plans[name] = plan
- }
- }
-
for name, metaPlan := range cfg.MetaPlans {
baseConfig.MetaPlans[name] = metaPlan
}