github.com/spinnaker/spin@v1.30.0/cmd/account/get.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  	gate "github.com/spinnaker/spin/gateapi"
    22  
    23  	"github.com/spf13/cobra"
    24  
    25  	"github.com/spinnaker/spin/util"
    26  )
    27  
    28  type getOptions struct {
    29  	*accountOptions
    30  }
    31  
    32  var (
    33  	getAccountShort   = "Get the specified account details"
    34  	getAccountLong    = "Get the specified account details"
    35  	getAccountExample = "usage: spin account get [options] account-name"
    36  )
    37  
    38  func NewGetCmd(accOptions *accountOptions) *cobra.Command {
    39  	options := &getOptions{
    40  		accountOptions: accOptions,
    41  	}
    42  
    43  	cmd := &cobra.Command{
    44  		Use:     "get",
    45  		Short:   getAccountShort,
    46  		Long:    getAccountLong,
    47  		Example: getAccountExample,
    48  		RunE: func(cmd *cobra.Command, args []string) error {
    49  			return getAccount(cmd, options, args)
    50  		},
    51  	}
    52  
    53  	return cmd
    54  }
    55  
    56  func getAccount(cmd *cobra.Command, options *getOptions, args []string) error {
    57  	accountName, err := util.ReadArgsOrStdin(args)
    58  	if err != nil {
    59  		return err
    60  	}
    61  
    62  	account, resp, err := options.GateClient.CredentialsControllerApi.GetAccountUsingGET(options.GateClient.Context, accountName, &gate.CredentialsControllerApiGetAccountUsingGETOpts{})
    63  	if resp != nil {
    64  		switch resp.StatusCode {
    65  		case http.StatusOK:
    66  			// pass
    67  		case http.StatusNotFound:
    68  			return fmt.Errorf("Account '%s' not found\n", accountName)
    69  		default:
    70  			return fmt.Errorf("Encountered an error getting account, status code: %d\n", resp.StatusCode)
    71  		}
    72  	}
    73  
    74  	if err != nil {
    75  		return err
    76  	}
    77  	options.Ui.JsonOutput(account)
    78  
    79  	return nil
    80  }