Skip to content

feat: add whoami command#356

Merged
rkpattnaik780 merged 3 commits intomainfrom
whoami
Feb 19, 2021
Merged

feat: add whoami command#356
rkpattnaik780 merged 3 commits intomainfrom
whoami

Conversation

@rkpattnaik780
Copy link
Contributor

whoami command prints the currently logged in user.

fixes: #339

whoami command prints the currently logged in user

fixes: #339
Copy link
Contributor

@craicoverflow craicoverflow left a comment

Choose a reason for hiding this comment

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

I've left some feedback on what needs to be changed. It's almost there 😄

Most commands follow a very similar pattern, so check out similar commands when working on one.

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.


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.


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


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

Comment on lines +1 to +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.

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
$ 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?

Comment on lines +24 to +26
[whoami.error.notLoggedInError]
description = 'Text to display if no user is logged in'
one = 'not logged in. Run "rhoas login" to authenticate' No newline at end of file
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.


[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.


[whoami.error.notLoggedInError]
description = 'Text to display if no user is logged in'
one = 'not logged in. Run "rhoas login" to authenticate' No newline at end of file
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.'

Copy link
Contributor

@bhardesty bhardesty left a comment

Choose a reason for hiding this comment

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

Just a couple minor suggestions for the help text.

[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

Copy link
Contributor

@craicoverflow craicoverflow left a comment

Choose a reason for hiding this comment

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

Nice work 👍🏻

@rkpattnaik780 rkpattnaik780 merged commit 421b165 into main Feb 19, 2021
@rkpattnaik780 rkpattnaik780 deleted the whoami branch February 19, 2021 11:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add rhoas whoami command

3 participants