feat: add the whoami command

The whoami command shows the user the name of the account they are
currently logged in to.
This commit is contained in:
Dan Anglin 2024-02-28 14:09:12 +00:00
parent 25633e5049
commit c507815ef6
Signed by: dananglin
GPG key ID: 0C1D44CFBEE68638
2 changed files with 37 additions and 0 deletions

View file

@ -28,6 +28,7 @@ func run() error {
createResource string = "create"
deleteResource string = "delete"
updateResource string = "update"
whoami string = "whoami"
)
summaries := map[string]string{
@ -38,6 +39,7 @@ func run() error {
createResource: "create a specific resource",
deleteResource: "delete a specific resource",
updateResource: "update a specific resource",
whoami: "print the account that you are currently logged in to",
}
flag.Usage = enbasUsageFunc(summaries)
@ -70,6 +72,8 @@ func run() error {
executor = newDeleteCommand(deleteResource, summaries[deleteResource])
case updateResource:
executor = newUpdateCommand(updateResource, summaries[updateResource])
case whoami:
executor = newWhoAmICommand(whoami, summaries[whoami])
default:
flag.Usage()
return fmt.Errorf("unknown subcommand %q", subcommand)

33
cmd/enbas/whoami.go Normal file
View file

@ -0,0 +1,33 @@
package main
import (
"flag"
"fmt"
"codeflow.dananglin.me.uk/apollo/enbas/internal/config"
)
type whoAmICommand struct {
*flag.FlagSet
}
func newWhoAmICommand(name, summary string) *whoAmICommand {
command := whoAmICommand{
FlagSet: flag.NewFlagSet(name, flag.ExitOnError),
}
command.Usage = commandUsageFunc(name, summary, command.FlagSet)
return &command
}
func (c *whoAmICommand) Execute() error {
config, err := config.NewAuthenticationConfigFromFile()
if err != nil {
return fmt.Errorf("unable to load the credential config; %w", err)
}
fmt.Printf("You are %s\n", config.CurrentAccount)
return nil
}