github.com/spinnaker/spin@v1.30.0/cmd/pipeline/get.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
    16  
    17  import (
    18  	"errors"
    19  	"fmt"
    20  	"net/http"
    21  
    22  	"github.com/spf13/cobra"
    23  )
    24  
    25  type getOptions struct {
    26  	*PipelineOptions
    27  	output      string
    28  	application string
    29  	name        string
    30  }
    31  
    32  var (
    33  	getPipelineShort = "Get the pipeline with the provided name from the provided application"
    34  	getPipelineLong  = "Get the specified pipeline"
    35  )
    36  
    37  func NewGetCmd(pipelineOptions *PipelineOptions) *cobra.Command {
    38  	options := &getOptions{
    39  		PipelineOptions: pipelineOptions,
    40  	}
    41  	cmd := &cobra.Command{
    42  		Use:   "get",
    43  		Short: getPipelineShort,
    44  		Long:  getPipelineLong,
    45  		RunE: func(cmd *cobra.Command, args []string) error {
    46  			return getPipeline(cmd, options)
    47  		},
    48  	}
    49  
    50  	cmd.PersistentFlags().StringVarP(&options.application, "application", "a", "", "Spinnaker application the pipeline belongs to")
    51  	cmd.PersistentFlags().StringVarP(&options.name, "name", "n", "", "name of the pipeline")
    52  
    53  	return cmd
    54  }
    55  
    56  func getPipeline(cmd *cobra.Command, options *getOptions) error {
    57  	if options.application == "" || options.name == "" {
    58  		return errors.New("one of required parameters 'application' or 'name' not set")
    59  	}
    60  
    61  	successPayload, resp, err := options.GateClient.ApplicationControllerApi.GetPipelineConfigUsingGET(options.GateClient.Context,
    62  		options.application,
    63  		options.name)
    64  	if err != nil {
    65  		return err
    66  	}
    67  
    68  	if resp.StatusCode != http.StatusOK {
    69  		return fmt.Errorf("Encountered an error getting pipeline in pipeline %s with name %s, status code: %d\n",
    70  			options.application,
    71  			options.name,
    72  			resp.StatusCode)
    73  	}
    74  
    75  	options.Ui.JsonOutput(successPayload)
    76  	return nil
    77  }