@@ -51,35 +51,32 @@ func AddContainer(cmd *cobra.Command, args []string) error {
51
51
}
52
52
53
53
pipelineInput := args [0 ]
54
- pipelineParts := strings .Split (pipelineInput , "/" )
55
-
56
- if len (pipelineParts ) != 3 {
57
- return fmt .Errorf ("invalid pipeline format: %s, expected format: LIFECYCLE/ACTION/PIPELINE-NAME" , pipelineInput )
54
+ containerArgs , err := ParseContainerCmdArgs (pipelineInput )
55
+ if err != nil {
56
+ return err
58
57
}
59
58
60
- lifecycle , action , pipelineName := pipelineParts [0 ], pipelineParts [1 ], pipelineParts [2 ]
61
-
62
- if err := generateWorkflow (lifecycle , action , pipelineName , containerName , image , false ); err != nil {
59
+ if err := generateWorkflow (containerArgs , containerName , image , false ); err != nil {
63
60
return err
64
61
}
65
62
66
63
pipelineScriptFilename := "pipeline.sh"
67
- scriptsPath := filepath .Join ("workflows" , lifecycle , action , pipelineName , containerName , "scripts" , pipelineScriptFilename )
64
+ scriptsPath := filepath .Join ("workflows" , containerArgs . Lifecycle , containerArgs . Action , containerArgs . Pipeline , containerName , "scripts" , pipelineScriptFilename )
68
65
fmt .Printf ("Customise your container by editing %s \n " , scriptsPath )
69
66
fmt .Println ("Don't forget to build and push your image!" )
70
67
return nil
71
68
}
72
69
73
- func generateWorkflow (lifecycle , action , pipelineName , containerName , image string , overwrite bool ) error {
74
- if lifecycle != "promise" && lifecycle != "resource" {
75
- return fmt .Errorf ("invalid lifecycle: %s, expected one of: promise, resource" , lifecycle )
70
+ func generateWorkflow (c * ContainerCmdArgs , containerName , image string , overwrite bool ) error {
71
+ if c . Lifecycle != "promise" && c . Lifecycle != "resource" {
72
+ return fmt .Errorf ("invalid lifecycle: %s, expected one of: promise, resource" , c . Lifecycle )
76
73
}
77
74
78
- if action != "configure" && action != "delete" {
79
- return fmt .Errorf ("invalid action: %s, expected one of: configure, delete" , action )
75
+ if c . Action != "configure" && c . Action != "delete" {
76
+ return fmt .Errorf ("invalid action: %s, expected one of: configure, delete" , c . Action )
80
77
}
81
78
82
- if pipelineName == "" {
79
+ if c . Pipeline == "" {
83
80
return fmt .Errorf ("pipeline name cannot be empty" )
84
81
}
85
82
@@ -88,7 +85,7 @@ func generateWorkflow(lifecycle, action, pipelineName, containerName, image stri
88
85
Image : image ,
89
86
}
90
87
91
- workflowPath := filepath .Join ("workflows" , lifecycle , action )
88
+ workflowPath := filepath .Join ("workflows" , c . Lifecycle , c . Action )
92
89
var promise v1alpha1.Promise
93
90
94
91
splitFiles := filesGeneratedWithSplit (dir )
@@ -111,7 +108,7 @@ func generateWorkflow(lifecycle, action, pipelineName, containerName, image stri
111
108
}
112
109
yaml .Unmarshal (fileBytes , & pipelines )
113
110
114
- pipelineIdx , err = getPipelineIdx (pipelines , pipelineName )
111
+ pipelineIdx , err = getPipelineIdx (pipelines , c . Pipeline )
115
112
if err != nil {
116
113
return err
117
114
}
@@ -133,7 +130,7 @@ func generateWorkflow(lifecycle, action, pipelineName, containerName, image stri
133
130
return err
134
131
}
135
132
136
- pipelines , pipelineIdx , err = findPipelinesForLifecycleAction (lifecycle , action , pipelineName , allPipelines )
133
+ pipelines , pipelineIdx , err = findPipelinesForLifecycleAction (c , allPipelines )
137
134
if err != nil {
138
135
return err
139
136
}
@@ -146,7 +143,7 @@ func generateWorkflow(lifecycle, action, pipelineName, containerName, image stri
146
143
pipelines [pipelineIdx ].Spec .Containers = append (pipelines [pipelineIdx ].Spec .Containers , container )
147
144
} else {
148
145
if ! overwrite {
149
- return fmt .Errorf ("image '%s' already exists in Pipeline '%s'" , container .Name , pipelineName )
146
+ return fmt .Errorf ("image '%s' already exists in Pipeline '%s'" , container .Name , c . Pipeline )
150
147
}
151
148
pipelines [pipelineIdx ].Spec .Containers [containerIdx ] = container
152
149
}
@@ -161,7 +158,7 @@ func generateWorkflow(lifecycle, action, pipelineName, containerName, image stri
161
158
"apiVersion" : "platform.kratix.io/v1alpha1" ,
162
159
"kind" : "Pipeline" ,
163
160
"metadata" : map [string ]interface {}{
164
- "name" : pipelineName ,
161
+ "name" : c . Pipeline ,
165
162
},
166
163
"spec" : map [string ]interface {}{
167
164
"containers" : []interface {}{container },
@@ -182,21 +179,21 @@ func generateWorkflow(lifecycle, action, pipelineName, containerName, image stri
182
179
return err
183
180
}
184
181
} else {
185
- updatePipeline (lifecycle , action , pipelinesUnstructured , & promise )
182
+ updatePipeline (c . Lifecycle , c . Action , pipelinesUnstructured , & promise )
186
183
187
184
fileBytes , err = yaml .Marshal (promise )
188
185
if err != nil {
189
186
return err
190
187
}
191
188
}
192
- if err := generatePipelineDirFiles (dir , workflowPath , pipelineName , containerName ); err != nil {
189
+ if err := generatePipelineDirFiles (dir , workflowPath , c . Pipeline , containerName ); err != nil {
193
190
return err
194
191
}
195
192
196
193
if err := os .WriteFile (filePath , fileBytes , filePerm ); err != nil {
197
194
return err
198
195
}
199
- fmt .Printf ("generated the %s/%s/%s/%s in %s \n " , lifecycle , action , pipelineName , containerName , filePath )
196
+ fmt .Printf ("generated the %s/%s/%s/%s in %s \n " , c . Lifecycle , c . Action , c . Pipeline , containerName , filePath )
200
197
201
198
return nil
202
199
}
@@ -206,26 +203,26 @@ func generateContainerName(image string) string {
206
203
return strings .Split (nameAndVersion , ":" )[0 ]
207
204
}
208
205
209
- func findPipelinesForLifecycleAction (lifecycle , action , pipelineName string , allPipelines map [v1alpha1.Type ]map [v1alpha1.Action ][]v1alpha1.Pipeline ) ([]v1alpha1.Pipeline , int , error ) {
206
+ func findPipelinesForLifecycleAction (c * ContainerCmdArgs , allPipelines map [v1alpha1.Type ]map [v1alpha1.Action ][]v1alpha1.Pipeline ) ([]v1alpha1.Pipeline , int , error ) {
210
207
var pipelines []v1alpha1.Pipeline
211
- switch lifecycle {
208
+ switch c . Lifecycle {
212
209
case "promise" :
213
- switch action {
210
+ switch c . Action {
214
211
case "configure" :
215
212
pipelines = allPipelines [v1alpha1.WorkflowTypePromise ][v1alpha1.WorkflowActionConfigure ]
216
213
case "delete" :
217
214
pipelines = allPipelines [v1alpha1.WorkflowTypePromise ][v1alpha1.WorkflowActionDelete ]
218
215
}
219
216
case "resource" :
220
- switch action {
217
+ switch c . Action {
221
218
case "configure" :
222
219
pipelines = allPipelines [v1alpha1.WorkflowTypeResource ][v1alpha1.WorkflowActionConfigure ]
223
220
case "delete" :
224
221
pipelines = allPipelines [v1alpha1.WorkflowTypeResource ][v1alpha1.WorkflowActionDelete ]
225
222
}
226
223
}
227
224
228
- idx , err := getPipelineIdx (pipelines , pipelineName )
225
+ idx , err := getPipelineIdx (pipelines , c . Pipeline )
229
226
if err != nil {
230
227
return nil , - 1 , err
231
228
}
0 commit comments