@@ -16,6 +16,7 @@ package pipeline
16
16
17
17
import (
18
18
"fmt"
19
+ "io/ioutil"
19
20
"net/http"
20
21
21
22
"github.com/spf13/cobra"
@@ -80,26 +81,27 @@ func savePipeline(cmd *cobra.Command, options *saveOptions) error {
80
81
if ! valid {
81
82
return fmt .Errorf ("Submitted pipeline is invalid: %s\n " , pipelineJson )
82
83
}
84
+
83
85
application := pipelineJson ["application" ].(string )
84
86
pipelineName := pipelineJson ["name" ].(string )
85
87
86
88
foundPipeline , queryResp , _ := options .GateClient .ApplicationControllerApi .GetPipelineConfigUsingGET (options .GateClient .Context , application , pipelineName )
87
-
88
- _ , exists := pipelineJson ["id" ].(string )
89
- var foundPipelineId string
90
- if queryResp .StatusCode == http .StatusOK && len (foundPipeline ) > 0 {
91
- foundPipelineId = foundPipeline ["id" ].(string )
92
- }
93
- if ! exists && foundPipelineId != "" {
94
- pipelineJson ["id" ] = foundPipelineId
89
+ if queryResp .StatusCode == http .StatusOK {
90
+ // pipeline found, let's use Spinnaker's known Pipeline ID, otherwise we'll get one created for us
91
+ if len (foundPipeline ) > 0 {
92
+ pipelineJson ["id" ] = foundPipeline ["id" ].(string )
93
+ }
94
+ } else if queryResp .StatusCode == http .StatusNotFound {
95
+ // pipeline doesn't exists, let's create a new one
96
+ } else {
97
+ b , _ := ioutil .ReadAll (queryResp .Body )
98
+ return fmt .Errorf ("unhandled response %d: %s" , queryResp .StatusCode , b )
95
99
}
96
100
97
101
saveResp , saveErr := options .GateClient .PipelineControllerApi .SavePipelineUsingPOST (options .GateClient .Context , pipelineJson )
98
-
99
102
if saveErr != nil {
100
103
return saveErr
101
- }
102
- if saveResp .StatusCode != http .StatusOK {
104
+ } else if saveResp .StatusCode != http .StatusOK {
103
105
return fmt .Errorf ("Encountered an error saving pipeline, status code: %d\n " , saveResp .StatusCode )
104
106
}
105
107
0 commit comments