-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathtokens.go
More file actions
106 lines (91 loc) · 3.07 KB
/
tokens.go
File metadata and controls
106 lines (91 loc) · 3.07 KB
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package parser
import (
"github.com/drycc/workflow-cli/internal/commands"
"github.com/drycc/workflow-cli/internal/completion"
"github.com/drycc/workflow-cli/internal/template"
"github.com/drycc/workflow-cli/pkg/i18n"
"github.com/spf13/cobra"
)
// NewTokensCommand creates the tokens command
func NewTokensCommand(cmdr *commands.DryccCmd) *cobra.Command {
cmd := &cobra.Command{
Use: "tokens",
Short: i18n.T("Manage user tokens"),
RunE: func(_ *cobra.Command, _ []string) error {
results, _ := commands.ResponseLimit(limit)
return cmdr.TokensList(results)
},
}
cmd.Flags().IntVarP(&limit, "limit", "l", 0, i18n.T("The maximum number of results to display"))
cmd.AddCommand(tokensListCommand(cmdr))
cmd.AddCommand(tokensAddCommand(cmdr))
cmd.AddCommand(tokensRemoveCommand(cmdr))
return cmd
}
func tokensListCommand(cmdr *commands.DryccCmd) *cobra.Command {
cmd := &cobra.Command{
Use: "list",
Short: i18n.T("Lists tokens visible to the current controller"),
RunE: func(_ *cobra.Command, _ []string) error {
results, _ := commands.ResponseLimit(limit)
return cmdr.TokensList(results)
},
}
cmd.Flags().IntVarP(&limit, "limit", "l", 0, i18n.T("The maximum number of results to display"))
return cmd
}
func tokensAddCommand(cmdr *commands.DryccCmd) *cobra.Command {
var flags struct {
username string
password string
}
cmd := &cobra.Command{
Use: "add <alias>",
Example: template.CustomExample(
"drycc tokens add mytoken -u uname -p passwd",
map[string]string{
"<alias>": i18n.T("Provide a alias for controller authentication token"),
},
),
Short: i18n.T("Add a token for controller authentication"),
Args: cobra.ExactArgs(1),
RunE: func(_ *cobra.Command, args []string) error {
alias := args[0]
_, err := cmdr.TokensAdd(nil, flags.username, flags.password, alias, "", true)
return err
},
}
cmd.Flags().StringVarP(&flags.username, "username", "u", "", i18n.T("Provide a username for the account"))
cmd.Flags().StringVarP(&flags.password, "password", "p", "", i18n.T("Provide a password for the account"))
mustFlags := []string{"username", "password"}
for _, mustFlag := range mustFlags {
cmd.MarkFlagRequired(mustFlag)
}
return cmd
}
func tokensRemoveCommand(cmdr *commands.DryccCmd) *cobra.Command {
var flags struct {
id string
confirm string
}
// coTokenCompletion
tokenCompletion := completion.TokenCompletion{ArgsLen: 0, ConfigFile: &cmdr.ConfigFile}
cmd := &cobra.Command{
Use: "remove <id>",
Example: template.CustomExample(
"drycc tokens remove 34103c3b-077e-4e37-a9ad-29ba4324ad8b",
map[string]string{
"<id>": i18n.T("The id of the token for controller authentication"),
},
),
Short: i18n.T("Remove a token for controller authentication"),
Args: cobra.ExactArgs(1),
ValidArgsFunction: tokenCompletion.CompletionFunc,
RunE: func(_ *cobra.Command, args []string) error {
flags.id = args[0]
return cmdr.TokensRemove(flags.id, flags.confirm)
},
}
cmd.Flags().StringVarP(&flags.confirm, "confirm", "", "", i18n.T(`To proceed, type "yes"`))
return cmd
}