Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
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
22 changes: 22 additions & 0 deletions locales/cmd/whoami/active.en.toml
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"
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
'''
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
}
65 changes: 65 additions & 0 deletions pkg/cmd/whoami/whoami.go
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
}