From c507815ef6a2ede02cbb91d39f16c2fa556e7b38 Mon Sep 17 00:00:00 2001 From: Dan Anglin Date: Wed, 28 Feb 2024 14:09:12 +0000 Subject: [PATCH] feat: add the whoami command The whoami command shows the user the name of the account they are currently logged in to. --- cmd/enbas/main.go | 4 ++++ cmd/enbas/whoami.go | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) create mode 100644 cmd/enbas/whoami.go diff --git a/cmd/enbas/main.go b/cmd/enbas/main.go index d0e658f..a23d2a6 100644 --- a/cmd/enbas/main.go +++ b/cmd/enbas/main.go @@ -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) diff --git a/cmd/enbas/whoami.go b/cmd/enbas/whoami.go new file mode 100644 index 0000000..d87174a --- /dev/null +++ b/cmd/enbas/whoami.go @@ -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 +}