github.com/spinnaker/spin@v1.30.0/cmd/pipeline-template/plan.go (about) 1 // Copyright (c) 2018, Google, Inc. 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_template 16 17 import ( 18 "errors" 19 "fmt" 20 "net/http" 21 22 "github.com/spf13/cobra" 23 24 "github.com/spinnaker/spin/util" 25 ) 26 27 type planOptions struct { 28 *pipelineTemplateOptions 29 configPath string 30 } 31 32 const ( 33 planPipelineTemplateShort = "Plan the provided pipeline template config" 34 planPipelineTemplateLong = "Plan the provided pipeline template config" 35 ) 36 37 func NewPlanCmd(pipelineTemplateOptions *pipelineTemplateOptions) *cobra.Command { 38 options := &planOptions{ 39 pipelineTemplateOptions: pipelineTemplateOptions, 40 } 41 cmd := &cobra.Command{ 42 Use: "plan", 43 Short: planPipelineTemplateShort, 44 Long: planPipelineTemplateLong, 45 RunE: func(cmd *cobra.Command, args []string) error { 46 return planPipelineTemplate(cmd, options) 47 }, 48 } 49 50 cmd.PersistentFlags().StringVarP(&options.configPath, "file", "f", "", "path to the pipeline template config file") 51 52 return cmd 53 } 54 55 func planPipelineTemplate(cmd *cobra.Command, options *planOptions) error { 56 configJson, err := util.ParseJsonFromFileOrStdin(options.configPath, false) 57 if err != nil { 58 return err 59 } 60 61 if _, exists := configJson["schema"]; !exists { 62 return errors.New("Required pipeline key 'schema' missing for templated pipeline config...\n") 63 } 64 65 successPayload, resp, err := options.GateClient.V2PipelineTemplatesControllerApi.PlanUsingPOST(options.GateClient.Context, configJson) 66 if err != nil { 67 return err 68 } 69 70 if resp.StatusCode != http.StatusOK { 71 return fmt.Errorf("Encountered an error planning pipeline template config, status code: %d\n", 72 resp.StatusCode) 73 } 74 75 options.Ui.JsonOutput(successPayload) 76 return nil 77 }