github.com/spinnaker/spin@v1.30.0/cmd/application/get_pipelines.go (about)

     1  // Copyright (c) 2020, Anosua "Chini" Mukhopadhyay
     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 application
    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  )
    27  
    28  type getPipelinesOptions struct {
    29  	*pipelineOptions
    30  	applicationName string
    31  	expand          bool
    32  	status          string
    33  }
    34  
    35  var (
    36  	getPipelinesShort   = "Get the pipelines for the specified application"
    37  	getPipelinesLong    = "Get the pipelines for the specified application"
    38  	getPipelinesExample = "usage: spin application pipelines get [options] application-name status"
    39  )
    40  
    41  func NewGetPipelinesCmd(pipeOptions *pipelineOptions) *cobra.Command {
    42  	options := &getPipelinesOptions{
    43  		pipelineOptions: pipeOptions,
    44  		expand:          false,
    45  	}
    46  
    47  	cmd := &cobra.Command{
    48  		Use:     "get",
    49  		Aliases: []string{"get"},
    50  		Short:   getPipelinesShort,
    51  		Long:    getPipelinesLong,
    52  		Example: getPipelinesExample,
    53  		RunE: func(cmd *cobra.Command, args []string) error {
    54  			return getPipelinesWithOutput(cmd, options, args)
    55  		},
    56  	}
    57  
    58  	cmd.PersistentFlags().StringVarP(&options.applicationName, "application-name", "a", "", "name of the application")
    59  	cmd.PersistentFlags().StringVarP(&options.status, "status", "s", "", "status pipeline to search for")
    60  	cmd.PersistentFlags().BoolVarP(&options.expand, "expand", "x", false, "expand app payload to include clusters")
    61  	return cmd
    62  }
    63  
    64  func getPipelinesWithOutput(cmd *cobra.Command, options *getPipelinesOptions, args []string) error {
    65  	app, err := getPipelines(cmd, options, args)
    66  	if err != nil {
    67  		return err
    68  	}
    69  
    70  	options.Ui.JsonOutput(app)
    71  	return nil
    72  }
    73  
    74  func getPipelines(cmd *cobra.Command, options *getPipelinesOptions, args []string) ([]interface{}, error) {
    75  	if options.applicationName == "" {
    76  		return nil, errors.New("Application name must be passed in")
    77  	}
    78  
    79  	app, resp, err := options.GateClient.ApplicationControllerApi.GetPipelinesUsingGET(options.GateClient.Context, options.applicationName, &gate.ApplicationControllerApiGetPipelinesUsingGETOpts{Expand: optional.NewBool(options.expand), Statuses: optional.NewString(options.status)})
    80  	if resp != nil {
    81  		switch resp.StatusCode {
    82  		case http.StatusOK:
    83  			// pass
    84  		case http.StatusNotFound:
    85  			return nil, fmt.Errorf("Application '%s' not found\n", options.applicationName)
    86  		default:
    87  			return nil, fmt.Errorf("Encountered an error getting application, status code: %d\n", resp.StatusCode)
    88  		}
    89  	}
    90  
    91  	if err != nil {
    92  		return nil, err
    93  	}
    94  
    95  	return app, nil
    96  }