@@ -10,23 +10,19 @@ import (
1010
1111// Config is a general config in DevStream.
1212type Config struct {
13- Config CoreConfig `yaml:"config"`
14- Vars map [string ]any `yaml:"vars"`
15- Tools Tools `yaml:"tools"`
16- Apps []app `yaml:"apps"`
17- PipelineTemplates []pipelineTemplate `yaml:"pipelineTemplates"`
18- pipelineTemplateMap map [string ]string `yaml:"-"`
13+ Config CoreConfig `yaml:"config"`
14+ Vars map [string ]any `yaml:"vars"`
15+ Tools Tools `yaml:"tools"`
16+ Apps []* app `yaml:"apps"`
17+ // We'll read the pipeline templates from config file and render it to pipelineTemplateMap in no time.
18+ //PipelineTemplates []*pipelineTemplate `yaml:"-"`
19+ pipelineTemplateMap map [string ]string `yaml:"-"`
1920}
2021
2122func (c * Config ) getToolsFromApps () (Tools , error ) {
22- err := c .renderPipelineTemplateMap ()
23- if err != nil {
24- return nil , err
25- }
26-
2723 var tools Tools
2824 for _ , a := range c .Apps {
29- appTools , err := c .getToolsWithVarsFromApp (a )
25+ appTools , err := c .getToolsFromApp (a )
3026 if err != nil {
3127 return nil , err
3228 }
@@ -35,92 +31,45 @@ func (c *Config) getToolsFromApps() (Tools, error) {
3531 return tools , nil
3632}
3733
38- func (c * Config ) getToolsWithVarsFromApp (a app ) (Tools , error ) {
39- appStr , err := yaml .Marshal (a )
34+ func (c * Config ) getToolsFromApp (a * app ) (Tools , error ) {
35+ // generate app repo and template repo from scmInfo
36+ a .setDefault ()
37+ repoScaffoldingTool , err := a .getRepoTemplateTool ()
4038 if err != nil {
41- return nil , err
39+ return nil , fmt . Errorf ( "app[%s] get repo failed: %w" , a . Name , err )
4240 }
4341
44- // 1. render appStr with Vars
45- appRenderStr , err := renderConfigWithVariables (string (appStr ), c .Vars )
42+ // get ci/cd pipelineTemplates
43+ appVars := a .Spec .merge (c .Vars )
44+ tools , err := a .generateCICDToolsFromAppConfig (c .pipelineTemplateMap , appVars )
4645 if err != nil {
47- log .Debugf ("configmanager/app %s render globalVars %+v failed" , appRenderStr , c .Vars )
48- return nil , fmt .Errorf ("app render globalVars failed: %w" , err )
49- }
50-
51- // 2. unmarshal app config for render pipelineTemplate
52- var rawApp app
53- if err = yaml .Unmarshal (appRenderStr , & rawApp ); err != nil {
54- return nil , fmt .Errorf ("app parse yaml failed: %w" , err )
55- }
56-
57- // 3. generate app repo and template repo from scmInfo
58- rawApp .setDefault ()
59- repoScaffoldingTool , err := rawApp .getRepoTemplateTool ()
60- if err != nil {
61- return nil , fmt .Errorf ("app[%s] get repo failed: %w" , rawApp .Name , err )
62- }
63-
64- // 4. get ci/cd pipelineTemplates
65- appVars := rawApp .Spec .merge (c .Vars )
66- tools , err := rawApp .generateCICDToolsFromAppConfig (c .pipelineTemplateMap , appVars )
67- if err != nil {
68- return nil , fmt .Errorf ("app[%s] get pipeline tools failed: %w" , rawApp .Name , err )
46+ return nil , fmt .Errorf ("app[%s] get pipeline tools failed: %w" , a .Name , err )
6947 }
7048 if repoScaffoldingTool != nil {
7149 tools = append (tools , repoScaffoldingTool )
7250 }
7351
74- // 5. all tools from apps should depend on the original tools,
75- // because dtm will execute all original tools first, then execute all tools from apps
52+ // all tools from apps should depend on the original tools,
53+ // because dtm will execute all original tools first, then execute all tools from apps
7654 for _ , toolFromApps := range tools {
7755 for _ , t := range c .Tools {
7856 toolFromApps .DependsOn = append (toolFromApps .DependsOn , t .KeyWithNameAndInstanceID ())
7957 }
8058 }
8159
82- log .Debugf ("Have got %d tools from app %s." , len (tools ), rawApp .Name )
60+ log .Debugf ("Have got %d tools from app %s." , len (tools ), a .Name )
8361 for i , t := range tools {
8462 log .Debugf ("Tool %d: %v" , i + 1 , t )
8563 }
8664
8765 return tools , nil
8866}
8967
90- func (c * Config ) renderToolsWithVars () error {
91- toolsStr , err := yaml .Marshal (c .Tools )
92- if err != nil {
93- return err
94- }
95-
96- toolsStrWithVars , err := renderConfigWithVariables (string (toolsStr ), c .Vars )
97- if err != nil {
98- return err
99- }
100-
101- var tools Tools
102- if err = yaml .Unmarshal (toolsStrWithVars , & tools ); err != nil {
103- return err
104- }
105- c .Tools = tools
106-
107- return nil
108- }
109-
110- func (c * Config ) renderPipelineTemplateMap () error {
111- c .pipelineTemplateMap = make (map [string ]string )
112- for _ , pt := range c .PipelineTemplates {
113- tplBytes , err := yaml .Marshal (pt )
114- if err != nil {
115- return err
116- }
117- c .pipelineTemplateMap [pt .Name ] = string (tplBytes )
118- }
119- return nil
120- }
121-
12268func (c * Config ) renderInstanceIDtoOptions () {
12369 for _ , t := range c .Tools {
70+ if t .Options == nil {
71+ t .Options = make (RawOptions )
72+ }
12473 t .Options ["instanceID" ] = t .InstanceID
12574 }
12675}
0 commit comments