github.com/spinnaker/spin@v1.30.0/cmd/pipeline-template/list.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  	"fmt"
    19  	"net/http"
    20  
    21  	"github.com/antihax/optional"
    22  	"github.com/spf13/cobra"
    23  
    24  	gate "github.com/spinnaker/spin/gateapi"
    25  )
    26  
    27  type listOptions struct {
    28  	*pipelineTemplateOptions
    29  	scopes *[]string
    30  }
    31  
    32  var (
    33  	listPipelineTemplateShort = "List the pipeline templates for the provided scopes"
    34  	listPipelineTemplateLong  = "List the pipeline templates for the provided scopes"
    35  )
    36  
    37  func NewListCmd(pipelineTemplateOptions *pipelineTemplateOptions) *cobra.Command {
    38  	options := &listOptions{
    39  		pipelineTemplateOptions: pipelineTemplateOptions,
    40  	}
    41  	cmd := &cobra.Command{
    42  		Use:     "list",
    43  		Aliases: []string{"ls"},
    44  		Short:   listPipelineTemplateShort,
    45  		Long:    listPipelineTemplateLong,
    46  		RunE: func(cmd *cobra.Command, args []string) error {
    47  			return listPipelineTemplate(cmd, options)
    48  		},
    49  	}
    50  
    51  	// TODO(jacobkiefer): Document pipeline template scopes.
    52  	options.scopes = cmd.PersistentFlags().StringArrayP("scopes", "", []string{}, "set of scopes to reduce the pipeline template list to")
    53  
    54  	return cmd
    55  }
    56  
    57  func listPipelineTemplate(cmd *cobra.Command, options *listOptions) error {
    58  	successPayload, resp, err := options.GateClient.V2PipelineTemplatesControllerApi.ListUsingGET1(options.GateClient.Context,
    59  		&gate.V2PipelineTemplatesControllerApiListUsingGET1Opts{Scopes: optional.NewInterface(options.scopes)})
    60  	if err != nil {
    61  		return err
    62  	}
    63  
    64  	if resp.StatusCode != http.StatusOK {
    65  		return fmt.Errorf("Encountered an error listing pipeline templates for scopes %v, status code: %d\n",
    66  			options.scopes,
    67  			resp.StatusCode)
    68  	}
    69  
    70  	options.Ui.JsonOutput(successPayload)
    71  	return nil
    72  }