Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/rhoas/pkged.go

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions docs/commands/rhoas.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,4 @@ Apache Kafka instances
delete and update service accounts
* link:rhoas_status.adoc[rhoas status] - View the status of all currently
used services
* link:rhoas_whoami.adoc[rhoas whoami] - Print current user name
32 changes: 32 additions & 0 deletions docs/commands/rhoas_whoami.adoc
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
26 changes: 26 additions & 0 deletions locales/cmd/whoami/active.en.toml
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"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
one = "Print current user name"
one = "Print current username"


[whoami.cmd.longDescription]
description = "Long description for command"
one = '''
Print the user name of currently active user.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Print the user name of currently active user.
Print the username of the currently active user.


This command prints the username associated with the user currently logged in.
'''

[whoami.cmd.example]
description = 'Examples of how to use the command'
one = '''
# print current user name
Copy link
Contributor

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

$ rhoas whoami
'''
Copy link
Contributor

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?


[whoami.error.notLoggedInError]
description = 'Text to display if no user is logged in'
one = 'not logged in. Run "rhoas login" to authenticate'
Copy link
Contributor

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.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
one = 'not logged in. Run "rhoas login" to authenticate'
one = 'Not logged in. Run "rhoas login" to authenticate.'

2 changes: 2 additions & 0 deletions pkg/cmd/root/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/bf2fc6cc711aee1a0c2a/cli/internal/localizer"
"github.com/bf2fc6cc711aee1a0c2a/cli/pkg/cmd/status"
"github.com/bf2fc6cc711aee1a0c2a/cli/pkg/cmd/whoami"

"github.com/bf2fc6cc711aee1a0c2a/cli/pkg/arguments"
"github.com/bf2fc6cc711aee1a0c2a/cli/pkg/cmd/cluster"
Expand Down Expand Up @@ -49,6 +50,7 @@ func NewRootCommand(cmdFactory *factory.Factory, version string) *cobra.Command
cmd.AddCommand(cluster.NewClusterCommand(cmdFactory))
cmd.AddCommand(status.NewStatusCommand(cmdFactory))
cmd.AddCommand(completion.CompletionCmd)
cmd.AddCommand(whoami.NewWhoAmICmd(cmdFactory))

return cmd
}
67 changes: 67 additions & 0 deletions pkg/cmd/whoami/whoami.go
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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would suggest naming this command as runCmd. Though we are getting the current user, it is not being returned, rather it is just printed inside the function so can never be "gotten" from another function.

logger, err := opts.Logger()
if err != nil {
return err
}

cfg, err := opts.Config.Load()
if err != nil {
logger.Error(err)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to log this error, it will be printed twice as returning it will print it from the main.go.

return err
}

accessTkn, _ := token.Parse(cfg.AccessToken)

if accessTkn == nil {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If my session has expired, my access token will still be in the config, but I will be not logged in. So checking if the access token is nil is not enough.

Also, the access token may also be expired, but if the refresh token is valid the user can still be logged in once it is refreshed.

The best thing to do here is let the Connection instance handle checking if the user is logged in.

See how this is done in the logout command: https://github.com/bf2fc6cc711aee1a0c2a/cli/blob/76028b9a74ba70fc546cfdf15b68217a49606ed6/pkg/cmd/logout/logout.go#L56

logger.Info(localizer.MustLocalizeFromID("whoami.error.notLoggedInError"))
return nil
}

tknClaims, _ := token.MapClaims(accessTkn)

userName, _ := tknClaims["preferred_username"]

logger.Info(fmt.Sprintf("%v", userName))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to be printed to stdout. The username being printed to stdout is vital for scripting.

If, for example I want to run rhoas whoami from within a Bash script, I will want the script to be able to read the username and use it. If it is printed to stderr it will be invisible to the script, as stderr is for logging information that only a human could understand (think natural language).

Use fmt.Fprintln to print the username to stdout. Use the IOStreams abstraction as the io.Writer:

https://github.com/bf2fc6cc711aee1a0c2a/cli/blob/ee46c6c03c8db658a93f2f1ba253f66193b38a2e/pkg/iostreams/iostreams.go#L14


return nil
}