-
Notifications
You must be signed in to change notification settings - Fork 1
/
info.go
72 lines (58 loc) · 1.54 KB
/
info.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
72
package account
import (
"context"
"errors"
"github.com/MakeNowJust/heredoc"
"github.com/spf13/cobra"
"github.com/netsoc/cli/pkg/config"
"github.com/netsoc/cli/pkg/util"
iam "github.com/netsoc/iam/client"
)
type infoOptions struct {
Config func() (*config.Config, error)
IAMClient func() (*iam.APIClient, error)
OutputFormat string
User string
}
// NewCmdInfo creates a new account info command
func NewCmdInfo(f *util.CmdFactory) *cobra.Command {
opts := infoOptions{
Config: f.Config,
IAMClient: f.IAMClient,
}
cmd := &cobra.Command{
Use: "info",
Aliases: []string{"get"},
Short: "Get info about user",
Long: heredoc.Doc(`
Prints details about the user. By default, retrieves the logged-in user's profile.
If logged in with an admin account, the -u flag can be used to select the user to retrieve.
A number of output format options are available.
`),
RunE: func(cmd *cobra.Command, args []string) error {
return infoRun(opts)
},
}
util.AddOptFormat(cmd, &opts.OutputFormat)
util.AddOptUser(cmd, &opts.User)
return cmd
}
func infoRun(opts infoOptions) error {
c, err := opts.Config()
if err != nil {
return err
}
if c.Token == "" {
return errors.New("not logged in")
}
client, err := opts.IAMClient()
if err != nil {
return err
}
ctx := context.WithValue(context.Background(), iam.ContextAccessToken, c.Token)
u, _, err := client.UsersApi.GetUser(ctx, opts.User)
if err != nil {
return util.APIError(err)
}
return util.PrintUsers([]iam.User{u}, opts.OutputFormat, true)
}