github.com/spinnaker/spin@v1.30.0/cmd/account/list.go (about)

     1  // Copyright 2019 New Relic Corporation. All rights reserved.
     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 account
    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  	*accountOptions
    29  	expand bool
    30  }
    31  
    32  var (
    33  	listAccountShort   = "List the all accounts"
    34  	listAccountLong    = "List the all accounts"
    35  	listAccountExample = "usage: spin account list [options]"
    36  )
    37  
    38  func NewListCmd(accOptions *accountOptions) *cobra.Command {
    39  	options := &listOptions{
    40  		accountOptions: accOptions,
    41  		expand:         false,
    42  	}
    43  
    44  	cmd := &cobra.Command{
    45  		Use:     "list",
    46  		Aliases: []string{"ls"},
    47  		Short:   listAccountShort,
    48  		Long:    listAccountLong,
    49  		Example: listAccountExample,
    50  		RunE: func(cmd *cobra.Command, args []string) error {
    51  			return listAccount(cmd, options, args)
    52  		},
    53  	}
    54  	return cmd
    55  }
    56  
    57  func listAccount(cmd *cobra.Command, options *listOptions, args []string) error {
    58  	accountList, resp, err := options.GateClient.CredentialsControllerApi.GetAccountsUsingGET(options.GateClient.Context, &gate.CredentialsControllerApiGetAccountsUsingGETOpts{Expand: optional.NewBool(options.expand)})
    59  	if err != nil {
    60  		return err
    61  	}
    62  
    63  	if resp.StatusCode != http.StatusOK {
    64  		return fmt.Errorf("Encountered an error listing accounts, status code: %d\n", resp.StatusCode)
    65  	}
    66  
    67  	options.Ui.JsonOutput(accountList)
    68  	return nil
    69  }