github.com/spinnaker/spin@v1.30.0/cmd/pipeline-template/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_template
    16  
    17  import (
    18  	"errors"
    19  	"fmt"
    20  	"net/http"
    21  
    22  	"github.com/antihax/optional"
    23  	"github.com/spf13/cobra"
    24  
    25  	gate "github.com/spinnaker/spin/gateapi"
    26  	"github.com/spinnaker/spin/util"
    27  )
    28  
    29  type getOptions struct {
    30  	*pipelineTemplateOptions
    31  	id  string
    32  	tag string
    33  }
    34  
    35  var (
    36  	getPipelineTemplateShort = "Get the pipeline template with the provided id"
    37  	getPipelineTemplateLong  = "Get the specified pipeline template"
    38  )
    39  
    40  func NewGetCmd(pipelineTemplateOptions *pipelineTemplateOptions) *cobra.Command {
    41  	options := &getOptions{
    42  		pipelineTemplateOptions: pipelineTemplateOptions,
    43  	}
    44  	cmd := &cobra.Command{
    45  		Use:   "get",
    46  		Short: getPipelineTemplateShort,
    47  		Long:  getPipelineTemplateLong,
    48  		RunE: func(cmd *cobra.Command, args []string) error {
    49  			return getPipelineTemplate(cmd, options, args)
    50  		},
    51  	}
    52  
    53  	cmd.PersistentFlags().StringVar(&options.id, "id", "", "id of the pipeline template")
    54  	cmd.PersistentFlags().StringVar(&options.tag, "tag", "",
    55  		"(optional) specific tag to query")
    56  
    57  	return cmd
    58  }
    59  
    60  func getPipelineTemplate(cmd *cobra.Command, options *getOptions, args []string) error {
    61  	var err error
    62  	id := options.id
    63  	if id == "" {
    64  		id, err = util.ReadArgsOrStdin(args)
    65  		if err != nil {
    66  			return err
    67  		}
    68  		if id == "" {
    69  			return errors.New("no pipeline template id supplied, exiting")
    70  		}
    71  	}
    72  
    73  	queryParams := &gate.V2PipelineTemplatesControllerApiGetUsingGET2Opts{}
    74  	if options.tag != "" {
    75  		queryParams.Tag = optional.NewString(options.tag)
    76  	}
    77  
    78  	successPayload, resp, err := options.GateClient.V2PipelineTemplatesControllerApi.GetUsingGET2(options.GateClient.Context,
    79  		id, queryParams)
    80  	if err != nil {
    81  		return err
    82  	}
    83  
    84  	if resp.StatusCode != http.StatusOK {
    85  		return fmt.Errorf("Encountered an error getting pipeline template with id %s, status code: %d\n",
    86  			id,
    87  			resp.StatusCode)
    88  	}
    89  
    90  	options.Ui.JsonOutput(successPayload)
    91  	return nil
    92  }