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