-
Notifications
You must be signed in to change notification settings - Fork 72
/
token.go
63 lines (53 loc) · 1.47 KB
/
token.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
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/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) {
_, err = opts.Connection()
if err != nil {
return err
}
cfg, err := opts.Config.Load()
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
}