Skip to content
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

Pr/ajaypratap003/1517 #1520

Merged
merged 5 commits into from
Apr 11, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
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 @@ -11,6 +11,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 @@ -50,6 +51,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'