github.com/spinnaker/spin@v1.30.0/cmd/canary/canary-config/list.go (about)

     1  // Copyright (c) 2019, Waze, 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 canary_config
    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  	*canaryConfigOptions
    29  	application string
    30  }
    31  
    32  const (
    33  	listCanaryConfigShort = "List the canary configs"
    34  	listCanaryConfigLong  = "List the canary configs"
    35  )
    36  
    37  func NewListCmd(canaryConfigOptions *canaryConfigOptions) *cobra.Command {
    38  	options := &listOptions{
    39  		canaryConfigOptions: canaryConfigOptions,
    40  	}
    41  	cmd := &cobra.Command{
    42  		Use:     "list",
    43  		Aliases: []string{"ls"},
    44  		Short:   listCanaryConfigShort,
    45  		Long:    listCanaryConfigLong,
    46  		RunE: func(cmd *cobra.Command, args []string) error {
    47  			return listCanaryConfig(cmd, options)
    48  		},
    49  	}
    50  
    51  	cmd.PersistentFlags().StringVarP(
    52  		&options.application, "application", "a", "", "application to list")
    53  
    54  	return cmd
    55  }
    56  
    57  func listCanaryConfig(cmd *cobra.Command, options *listOptions) error {
    58  	successPayload, resp, err := options.GateClient.V2CanaryConfigControllerApi.GetCanaryConfigsUsingGET(
    59  		options.GateClient.Context, &gate.V2CanaryConfigControllerApiGetCanaryConfigsUsingGETOpts{Application: optional.NewString(options.application)})
    60  	if err != nil {
    61  		return err
    62  	}
    63  
    64  	if resp.StatusCode != http.StatusOK {
    65  		return fmt.Errorf(
    66  			"Encountered an error listing canary configs, status code: %d\n",
    67  			resp.StatusCode)
    68  	}
    69  
    70  	options.Ui.JsonOutput(successPayload)
    71  	return nil
    72  }