-
Notifications
You must be signed in to change notification settings - Fork 72
/
whoami.go
71 lines (59 loc) · 1.66 KB
/
whoami.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package whoami
import (
"fmt"
"github.com/redhat-developer/app-services-cli/internal/config"
"github.com/redhat-developer/app-services-cli/pkg/auth/token"
"github.com/redhat-developer/app-services-cli/pkg/cmd/factory"
"github.com/redhat-developer/app-services-cli/pkg/connection"
"github.com/redhat-developer/app-services-cli/pkg/iostreams"
"github.com/redhat-developer/app-services-cli/pkg/localize"
"github.com/redhat-developer/app-services-cli/pkg/logging"
"github.com/spf13/cobra"
)
type options struct {
Config config.IConfig
Connection factory.ConnectionFunc
IO *iostreams.IOStreams
Logger logging.Logger
localizer localize.Localizer
}
func NewWhoAmICmd(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: "whoami",
Short: f.Localizer.MustLocalize("whoami.cmd.shortDescription"),
Long: f.Localizer.MustLocalize("whoami.cmd.longDescription"),
Example: f.Localizer.MustLocalize("whoami.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
}
userName, ok := token.GetUsername(cfg.AccessToken)
if !ok {
userName = "unknown"
}
if ok {
fmt.Fprintln(opts.IO.Out, userName)
} else {
opts.Logger.Info(opts.localizer.MustLocalize("whoami.log.info.tokenHasNoUsername"))
}
return nil
}