-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathperms.go
More file actions
138 lines (120 loc) · 4.09 KB
/
perms.go
File metadata and controls
138 lines (120 loc) · 4.09 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package parser
import (
"strings"
"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"
)
func NewPermsCommand(cmdr *commands.DryccCmd) *cobra.Command {
cmd := &cobra.Command{
Use: "perms",
Short: i18n.T("Manage permissions for applications"),
RunE: func(_ *cobra.Command, _ []string) error {
results, _ := commands.ResponseLimit(limit)
return cmdr.PermList(app, results)
},
}
cmd.PersistentFlags().StringVarP(&app, "app", "a", "", i18n.T("The uniquely identifiable name for the application"))
cmd.Flags().IntVarP(&limit, "limit", "l", 0, i18n.T("The maximum number of results to display"))
appCompletion := completion.AppCompletion{ArgsLen: -1, ConfigFile: &cmdr.ConfigFile}
cmd.RegisterFlagCompletionFunc("app", appCompletion.CompletionFunc)
cmd.AddCommand(permsListCommand(cmdr))
cmd.AddCommand(permAddCommand(cmdr))
cmd.AddCommand(permUpdateCommand(cmdr))
cmd.AddCommand(permRemoveCommand(cmdr))
return cmd
}
func permsListCommand(cmdr *commands.DryccCmd) *cobra.Command {
cmd := &cobra.Command{
Use: "list",
Short: i18n.T("List all user permissions"),
RunE: func(_ *cobra.Command, _ []string) error {
results, _ := commands.ResponseLimit(limit)
return cmdr.PermList(app, results)
},
}
cmd.Flags().IntVarP(&limit, "limit", "l", 0, i18n.T("The maximum number of results to display"))
return cmd
}
func permAddCommand(cmdr *commands.DryccCmd) *cobra.Command {
var flags struct {
app string
username string
permissions string
}
userPermsArgsCompletion := completion.UserPermsArgsCompletion{
UserPermsCompletion: &completion.UserPermsCompletion{ConfigFile: &cmdr.ConfigFile},
}
cmd := &cobra.Command{
Use: "add <username> <permission>...",
Args: cobra.MinimumNArgs(2),
Example: template.CustomExample(
"drycc add username view change delete",
map[string]string{
"<username>": i18n.T("The name of the user"),
"<permission>": i18n.T("The user permissions (view,change,delete)"),
},
),
Short: i18n.T("Add a user permissions"),
ValidArgsFunction: userPermsArgsCompletion.CompletionFunc,
RunE: func(_ *cobra.Command, args []string) error {
flags.username = args[0]
flags.permissions = strings.Join(args[1:], ",")
return cmdr.PermCreate(app, flags.username, flags.permissions)
},
}
return cmd
}
func permUpdateCommand(cmdr *commands.DryccCmd) *cobra.Command {
var flags struct {
app string
username string
permissions string
}
permUpdateCompletion := completion.PermUpdateCompletion{AppID: &app, ArgsLen: -1, ConfigFile: &cmdr.ConfigFile}
cmd := &cobra.Command{
Use: "update <username> <permission>... ",
Args: cobra.MinimumNArgs(2),
Example: template.CustomExample(
"drycc update username view,change",
map[string]string{
"<username>": i18n.T("The name of the user"),
"<permission>": i18n.T("The user's permissions (view,change,delete)"),
},
),
Short: i18n.T("Update a user permission"),
ValidArgsFunction: permUpdateCompletion.CompletionFunc,
RunE: func(_ *cobra.Command, args []string) error {
flags.username = args[0]
flags.permissions = strings.Join(args[1:], ",")
return cmdr.PermUpdate(app, flags.username, flags.permissions)
},
}
return cmd
}
func permRemoveCommand(cmdr *commands.DryccCmd) *cobra.Command {
var flags struct {
app string
username string
}
permUsernameCompletion := completion.PermUsernameCompletion{AppID: &app, ArgsLen: 0, ConfigFile: &cmdr.ConfigFile}
cmd := &cobra.Command{
Use: "remove <username>",
Args: cobra.MinimumNArgs(1),
Example: template.CustomExample(
"drycc perms remove username",
map[string]string{
"<username>": i18n.T("The name of the user"),
},
),
Short: i18n.T("Remove a user permissions"),
ValidArgsFunction: permUsernameCompletion.CompletionFunc,
RunE: func(_ *cobra.Command, args []string) error {
flags.username = args[0]
return cmdr.PermDelete(app, flags.username)
},
}
return cmd
}