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

     1  // Copyright 2021, Han Byul Ko.
     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/spf13/cobra"
    22  )
    23  
    24  type whoamiOptions struct {
    25  	*accountOptions
    26  	expand bool
    27  }
    28  
    29  var (
    30  	whoamiAccountShort   = "Show information about user who is making the API calls via spin"
    31  	whoamiAccountLong    = "Show information about user who is making the API calls via spin"
    32  	whoamiAccountExample = "usage: spin account whoami [options]"
    33  )
    34  
    35  func NewWhoamiCmd(accOptions *accountOptions) *cobra.Command {
    36  	options := &whoamiOptions{
    37  		accountOptions: accOptions,
    38  		expand:         false,
    39  	}
    40  
    41  	cmd := &cobra.Command{
    42  		Use:     "whoami",
    43  		Aliases: []string{"user"},
    44  		Short:   whoamiAccountShort,
    45  		Long:    whoamiAccountLong,
    46  		Example: whoamiAccountExample,
    47  		RunE: func(cmd *cobra.Command, args []string) error {
    48  			return whoamiAccount(cmd, options, args)
    49  		},
    50  	}
    51  	return cmd
    52  }
    53  
    54  func whoamiAccount(cmd *cobra.Command, options *whoamiOptions, args []string) error {
    55  	whoami, resp, err := options.GateClient.AuthControllerApi.UserUsingGET(options.GateClient.Context)
    56  	if err != nil {
    57  		return err
    58  	}
    59  
    60  	if resp.StatusCode != http.StatusOK {
    61  		return fmt.Errorf("Encountered an error whoamiing accounts, status code: %d\n", resp.StatusCode)
    62  	}
    63  
    64  	options.Ui.JsonOutput(whoami)
    65  	return nil
    66  }