github.com/spinnaker/spin@v1.30.0/cmd/pipeline/update.go (about) 1 // Copyright (c) 2020, Anosua "Chini" Mukhopadhyay 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package pipeline 16 17 import ( 18 "fmt" 19 "net/http" 20 21 "github.com/spf13/cobra" 22 23 gate "github.com/spinnaker/spin/gateapi" 24 ) 25 26 type updateOptions struct { 27 *PipelineOptions 28 disabled bool 29 enabled bool 30 application string 31 name string 32 } 33 34 const ( 35 updatePipelineShort = "Update the provided pipeline" 36 updatePipelineLong = "Update the provided pipeline" 37 ) 38 39 // NewUpdateCmd sets flags and options for the pipeline update command 40 func NewUpdateCmd(pipelineOptions *PipelineOptions) *cobra.Command { 41 options := &updateOptions{ 42 PipelineOptions: pipelineOptions, 43 } 44 cmd := &cobra.Command{ 45 Use: "update", 46 Aliases: []string{}, 47 Short: updatePipelineShort, 48 Long: updatePipelineLong, 49 RunE: func(cmd *cobra.Command, args []string) error { 50 return updatePipeline(cmd, options) 51 }, 52 } 53 54 cmd.PersistentFlags().StringVarP(&options.application, "application", "a", "", "Spinnaker application the pipeline belongs to") 55 cobra.MarkFlagRequired(cmd.PersistentFlags(), "application") 56 cmd.PersistentFlags().StringVarP(&options.name, "name", "n", "", "name of the pipeline") 57 cobra.MarkFlagRequired(cmd.PersistentFlags(), "name") 58 cmd.PersistentFlags().BoolVarP(&options.disabled, "disabled", "d", false, "enable or disable pipeline") 59 cmd.PersistentFlags().BoolVarP(&options.enabled, "enabled", "e", false, "enable or disable pipeline") 60 61 return cmd 62 } 63 64 func updatePipeline(cmd *cobra.Command, options *updateOptions) error { 65 application := options.application 66 pipelineName := options.name 67 68 foundPipeline, queryResp, _ := options.GateClient.ApplicationControllerApi.GetPipelineConfigUsingGET(options.GateClient.Context, application, pipelineName) 69 if queryResp.StatusCode == http.StatusNotFound { 70 return fmt.Errorf("Pipeline %s not found under application %s", pipelineName, application) 71 } 72 73 if cmd.Flags().Changed("disabled") && cmd.Flags().Changed("enabled") { 74 return fmt.Errorf("Cannot pass in both enabled and disabled flag") 75 } 76 77 if cmd.Flags().Changed("disabled") { 78 // User passed in the disabled flag and so pipeline should update its value 79 foundPipeline["disabled"] = options.disabled 80 } 81 82 if cmd.Flags().Changed("enabled") { 83 // User passed in the enabled flag and so pipeline should update its value 84 foundPipeline["disabled"] = !options.enabled 85 } 86 87 // TODO: support option passing in and remove nil in below call 88 opt := &gate.PipelineControllerApiSavePipelineUsingPOSTOpts{} 89 saveResp, err := options.GateClient.PipelineControllerApi.SavePipelineUsingPOST(options.GateClient.Context, foundPipeline, opt) 90 if err != nil { 91 return err 92 } 93 94 if saveResp.StatusCode != http.StatusOK { 95 return fmt.Errorf("Encountered an error saving pipeline, status code: %d\n", saveResp.StatusCode) 96 } 97 98 options.Ui.Success("Pipeline update succeeded") 99 return nil 100 }