-
Notifications
You must be signed in to change notification settings - Fork 1
/
issue.go
75 lines (61 loc) · 1.5 KB
/
issue.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
73
74
75
package account
import (
"context"
"errors"
"fmt"
"log"
"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 issueOptions struct {
Config func() (*config.Config, error)
IAMClient func() (*iam.APIClient, error)
Username string
Duration string
}
// NewCmdIssue creates a new account issue command
func NewCmdIssue(f *util.CmdFactory) *cobra.Command {
opts := issueOptions{
Config: f.Config,
IAMClient: f.IAMClient,
}
cmd := &cobra.Command{
Use: "issue <username> <duration>",
Short: "(admin only) Issue a token",
Long: heredoc.Doc(`
Issue a token for a user. duration is a Go duration
(see https://golang.org/pkg/time/#ParseDuration for details).
`),
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
opts.Username = args[0]
opts.Duration = args[1]
return issueRun(opts)
},
}
return cmd
}
func issueRun(opts issueOptions) 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)
r, _, err := client.UsersApi.IssueToken(ctx, opts.Username, iam.IssueTokenRequest{Duration: opts.Duration})
if err != nil {
return util.APIError(err)
}
log.Println("New token:")
fmt.Println(r.Token)
return nil
}