-
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
Merged
Merged
Changes from 2 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,22 @@ | ||||||
| [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
Contributor
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.
Suggested change
| # print current user name | |
| # print current username |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| 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/connection" | ||
| "github.com/bf2fc6cc711aee1a0c2a/cli/pkg/iostreams" | ||
|
|
||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| type Options struct { | ||
| Config config.IConfig | ||
| Connection func() (connection.Connection, error) | ||
| IO *iostreams.IOStreams | ||
| } | ||
|
|
||
| func NewWhoAmICmd(f *factory.Factory) *cobra.Command { | ||
| opts := &Options{ | ||
| Config: f.Config, | ||
| Connection: f.Connection, | ||
| IO: f.IOStreams, | ||
| } | ||
|
|
||
| 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 runCmd(opts) | ||
| }, | ||
| } | ||
|
|
||
| return cmd | ||
| } | ||
|
|
||
| func runCmd(opts *Options) (err error) { | ||
|
|
||
| cfg, err := opts.Config.Load() | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| _, err = opts.Connection() | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| accessTkn, _ := token.Parse(cfg.AccessToken) | ||
|
|
||
| tknClaims, _ := token.MapClaims(accessTkn) | ||
|
|
||
| userName, _ := tknClaims["preferred_username"] | ||
|
|
||
| fmt.Fprintln(opts.IO.Out, userName) | ||
|
|
||
| return nil | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.