-
Notifications
You must be signed in to change notification settings - Fork 66
feat: add whoami command #356
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| == rhoas whoami | ||
|
|
||
| Print current user name | ||
|
|
||
| === Synopsis | ||
|
|
||
| Print the user name of currently active user. | ||
|
|
||
| This command prints the username associated with the user currently | ||
| logged in. | ||
|
|
||
| .... | ||
| rhoas whoami [flags] | ||
| .... | ||
|
|
||
| === Examples | ||
|
|
||
| .... | ||
| # print current user name | ||
| $ rhoas whoami | ||
| .... | ||
|
|
||
| === Options inherited from parent commands | ||
|
|
||
| .... | ||
| -d, --debug Enable debug mode | ||
| -h, --help Show help for a command | ||
| .... | ||
|
|
||
| === SEE ALSO | ||
|
|
||
| * link:rhoas.adoc[rhoas] - RHOAS CLI |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,26 @@ | ||||||
| [whoami.cmd.use] | ||||||
| description = "Use is the one-line usage message" | ||||||
| one = "whoami" | ||||||
|
|
||||||
| [whoami.cmd.shortDescription] | ||||||
| description = "Short description for command" | ||||||
| one = "Print current user name" | ||||||
|
|
||||||
| [whoami.cmd.longDescription] | ||||||
| description = "Long description for command" | ||||||
| one = ''' | ||||||
| Print the user name of currently active user. | ||||||
|
||||||
| Print the user name of currently active user. | |
| Print the username of the currently active user. |
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| # print current user name | |
| # print current username |
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @bhardesty can you take a look at this command help-text?
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can go once you let the Connection handle the login status.
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| one = 'not logged in. Run "rhoas login" to authenticate' | |
| one = 'Not logged in. Run "rhoas login" to authenticate.' |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| package whoami | ||
|
|
||
| import ( | ||
| "fmt" | ||
|
|
||
| "github.com/bf2fc6cc711aee1a0c2a/cli/internal/config" | ||
| "github.com/bf2fc6cc711aee1a0c2a/cli/internal/localizer" | ||
| "github.com/bf2fc6cc711aee1a0c2a/cli/pkg/auth/token" | ||
| "github.com/bf2fc6cc711aee1a0c2a/cli/pkg/cmd/factory" | ||
|
|
||
| "github.com/bf2fc6cc711aee1a0c2a/cli/pkg/logging" | ||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| type Options struct { | ||
| Config config.IConfig | ||
| Logger func() (logging.Logger, error) | ||
| } | ||
|
|
||
| func NewWhoAmICmd(f *factory.Factory) *cobra.Command { | ||
| opts := &Options{ | ||
| Config: f.Config, | ||
| Logger: f.Logger, | ||
| } | ||
|
|
||
| localizer.LoadMessageFiles("cmd/whoami") | ||
|
|
||
| cmd := &cobra.Command{ | ||
| Use: localizer.MustLocalizeFromID("whoami.cmd.use"), | ||
| Short: localizer.MustLocalizeFromID("whoami.cmd.shortDescription"), | ||
| Long: localizer.MustLocalizeFromID("whoami.cmd.longDescription"), | ||
| Example: localizer.MustLocalizeFromID("whoami.cmd.example"), | ||
| RunE: func(cmd *cobra.Command, _ []string) error { | ||
| return getCurrentUser(opts) | ||
| }, | ||
| } | ||
|
|
||
| return cmd | ||
| } | ||
|
|
||
| func getCurrentUser(opts *Options) (err error) { | ||
|
||
| logger, err := opts.Logger() | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| cfg, err := opts.Config.Load() | ||
| if err != nil { | ||
| logger.Error(err) | ||
|
||
| return err | ||
| } | ||
|
|
||
| accessTkn, _ := token.Parse(cfg.AccessToken) | ||
|
|
||
| if accessTkn == nil { | ||
|
||
| logger.Info(localizer.MustLocalizeFromID("whoami.error.notLoggedInError")) | ||
| return nil | ||
| } | ||
|
|
||
| tknClaims, _ := token.MapClaims(accessTkn) | ||
|
|
||
| userName, _ := tknClaims["preferred_username"] | ||
|
|
||
| logger.Info(fmt.Sprintf("%v", userName)) | ||
|
||
|
|
||
| return nil | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.