Skip to content

Commit

Permalink
feat: token command (#1520)
Browse files Browse the repository at this point in the history
  • Loading branch information
wtrocki authored Apr 11, 2022
1 parent 46ed356 commit bbd62b6
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 0 deletions.
11 changes: 11 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,17 @@
"whoami"
]
},
{
"name": "auth token",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${workspaceFolder}/cmd/rhoas",
"env": {},
"args": [
"authtoken"
]
},
{
"name": "Logout",
"type": "go",
Expand Down
1 change: 1 addition & 0 deletions docs/commands/rhoas.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 35 additions & 0 deletions docs/commands/rhoas_authtoken.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions pkg/cmd/root/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/redhat-developer/app-services-cli/pkg/cmd/request"
"github.com/redhat-developer/app-services-cli/pkg/cmd/serviceaccount"
"github.com/redhat-developer/app-services-cli/pkg/cmd/status"
"github.com/redhat-developer/app-services-cli/pkg/cmd/token"
cliversion "github.com/redhat-developer/app-services-cli/pkg/cmd/version"
"github.com/redhat-developer/app-services-cli/pkg/cmd/whoami"
"github.com/redhat-developer/app-services-cli/pkg/core/cmdutil/flagutil"
Expand Down Expand Up @@ -53,6 +54,7 @@ func NewRootCommand(f *factory.Factory, version string) *cobra.Command {
cmd.AddCommand(completion.NewCompletionCommand(f))
cmd.AddCommand(whoami.NewWhoAmICmd(f))
cmd.AddCommand(cliversion.NewVersionCmd(f))
cmd.AddCommand(token.NewAuthTokenCmd(f))
// Registry commands
cmd.AddCommand(registry.NewServiceRegistryCommand(f))

Expand Down
64 changes: 64 additions & 0 deletions pkg/cmd/token/token.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package token

import (
"fmt"

"github.com/redhat-developer/app-services-cli/pkg/core/config"
"github.com/redhat-developer/app-services-cli/pkg/core/ioutil/iostreams"
"github.com/redhat-developer/app-services-cli/pkg/core/localize"
"github.com/redhat-developer/app-services-cli/pkg/core/logging"
"github.com/redhat-developer/app-services-cli/pkg/shared/connection"
"github.com/redhat-developer/app-services-cli/pkg/shared/factory"
"github.com/spf13/cobra"
)

type options struct {
Config config.IConfig
Connection factory.ConnectionFunc
IO *iostreams.IOStreams
Logger logging.Logger
localizer localize.Localizer
}

func NewAuthTokenCmd(f *factory.Factory) *cobra.Command {
opts := &options{
Config: f.Config,
Connection: f.Connection,
IO: f.IOStreams,
Logger: f.Logger,
localizer: f.Localizer,
}

cmd := &cobra.Command{
Use: "authtoken",
Short: f.Localizer.MustLocalize("token.cmd.shortDescription"),
Long: f.Localizer.MustLocalize("token.cmd.longDescription"),
Example: f.Localizer.MustLocalize("token.cmd.example"),
Args: cobra.NoArgs,
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(connection.DefaultConfigSkipMasAuth)
if err != nil {
return err
}

if cfg.AccessToken != "" {
fmt.Fprintln(opts.IO.Out, cfg.AccessToken)
} else {
opts.Logger.Info(opts.localizer.MustLocalize("token.log.info.tokenUnavailable"))
}

return nil
}
22 changes: 22 additions & 0 deletions pkg/core/localize/locales/en/cmd/token.en.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
[token.cmd.shortDescription]
description = "Short description for command"
one = "Output the current token"

[token.cmd.longDescription]
description = "Long description for command"
one = '''
View the authentication token of the current user that can be used to
make general API requests against api.openshift.com APIs.
This command outputs the token for the user currently logged in.
'''

[token.cmd.example]
description = 'Examples of how to use the command'
one = '''
# Returns header with token used for authorization
$ echo Authorization: BEARER ${rhoas authtoken}
'''

[token.log.info.tokenUnavailable]
one = 'Token unavailable'

0 comments on commit bbd62b6

Please sign in to comment.