Skip to content

Commit 3c7e4de

Browse files
demisitify collaborators command
1 parent 4008665 commit 3c7e4de

1 file changed

Lines changed: 59 additions & 60 deletions

File tree

cmd/collaborator.go

Lines changed: 59 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -14,26 +14,13 @@ import (
1414
"gopkg.in/yaml.v3"
1515
)
1616

17-
var collaboratorCmd = &cobra.Command{
18-
Use: "collaborator",
17+
var collaboratorsCmd = &cobra.Command{
18+
Use: "collaborators",
1919
Short: "Manage collaborators and access requests",
2020
}
2121

2222
var emailRegex = regexp.MustCompile(`^[a-z0-9._%+\-]+@[a-z0-9.\-]+\.[a-z]{2,}$`)
2323

24-
func validateProjectAndUser(projectID, username string) error {
25-
if !emailRegex.MatchString(strings.ToLower(username)) {
26-
return fmt.Errorf("invalid username '%s': must be a valid email address", username)
27-
}
28-
29-
parts := strings.Split(projectID, "-")
30-
if len(parts) != 2 || parts[0] == "" || parts[1] == "" {
31-
return fmt.Errorf("invalid project_id '%s': must be in the form 'program-project'", projectID)
32-
}
33-
34-
return nil
35-
}
36-
3724
func printRequest(r requestor.Request) {
3825
b, err := yaml.Marshal(r)
3926
if err != nil {
@@ -43,17 +30,17 @@ func printRequest(r requestor.Request) {
4330
fmt.Println(string(b))
4431
}
4532

46-
func getRequestorClient() (requestor.RequestorInterface, func()) {
47-
if profile == "" {
48-
fmt.Println("Error: profile is required. Please specify a profile using the --profile flag.")
33+
func getRequestorClient(localProfile string) (requestor.RequestorInterface, func()) {
34+
if localProfile == "" {
35+
fmt.Println("Error: profile is required.")
4936
os.Exit(1)
5037
}
5138

5239
// Initialize logger
53-
logger, logCloser := logs.New(profile)
40+
logger, logCloser := logs.New(localProfile)
5441

5542
// Initialize base Gen3 interface and build requestor client from it.
56-
g3i, err := g3client.NewGen3Interface(profile, logger)
43+
g3i, err := g3client.NewGen3Interface(localProfile, logger)
5744
if err != nil {
5845
fmt.Printf("Error accessing Gen3: %v\n", err)
5946
logCloser()
@@ -64,14 +51,16 @@ func getRequestorClient() (requestor.RequestorInterface, func()) {
6451
}
6552

6653
var collaboratorListCmd = &cobra.Command{
67-
Use: "ls",
54+
Use: "ls [profile]",
6855
Short: "List requests",
56+
Args: cobra.ExactArgs(1),
6957
Run: func(cmd *cobra.Command, args []string) {
58+
p := args[0]
7059
mine, _ := cmd.Flags().GetBool("mine")
7160
active, _ := cmd.Flags().GetBool("active")
7261
username, _ := cmd.Flags().GetString("username")
7362

74-
client, closer := getRequestorClient()
63+
client, closer := getRequestorClient(p)
7564
defer closer()
7665

7766
requests, err := client.ListRequests(cmd.Context(), mine, active, username)
@@ -87,10 +76,12 @@ var collaboratorListCmd = &cobra.Command{
8776
}
8877

8978
var collaboratorPendingCmd = &cobra.Command{
90-
Use: "pending",
79+
Use: "pending [profile]",
9180
Short: "List pending requests",
81+
Args: cobra.ExactArgs(1),
9282
Run: func(cmd *cobra.Command, args []string) {
93-
client, closer := getRequestorClient()
83+
p := args[0]
84+
client, closer := getRequestorClient(p)
9485
defer closer()
9586

9687
// Fetch all requests
@@ -110,22 +101,26 @@ var collaboratorPendingCmd = &cobra.Command{
110101
}
111102

112103
var collaboratorAddUserCmd = &cobra.Command{
113-
Use: "add [project_id] [username]",
104+
Use: "add [profile] [email] [program] [project]",
114105
Short: "Add a user to a project",
115-
Args: func(cmd *cobra.Command, args []string) error {
116-
if err := cobra.ExactArgs(2)(cmd, args); err != nil {
117-
return err
118-
}
119-
return validateProjectAndUser(args[0], args[1])
120-
},
106+
Args: cobra.ExactArgs(4),
121107
Run: func(cmd *cobra.Command, args []string) {
122-
projectID := args[0]
108+
p := args[0]
123109
username := args[1]
110+
program := args[2]
111+
project := args[3]
112+
projectID := fmt.Sprintf("%s-%s", program, project)
113+
114+
if !emailRegex.MatchString(strings.ToLower(username)) {
115+
fmt.Printf("Error: invalid email address '%s'\n", username)
116+
os.Exit(1)
117+
}
118+
124119
write, _ := cmd.Flags().GetBool("write")
125120
guppy, _ := cmd.Flags().GetBool("guppy")
126121
approve, _ := cmd.Flags().GetBool("approve")
127122

128-
client, closer := getRequestorClient()
123+
client, closer := getRequestorClient(p)
129124
defer closer()
130125

131126
reqs, err := client.AddUser(cmd.Context(), projectID, username, write, guppy)
@@ -156,20 +151,24 @@ var collaboratorAddUserCmd = &cobra.Command{
156151
}
157152

158153
var collaboratorRemoveUserCmd = &cobra.Command{
159-
Use: "rm [project_id] [username]",
154+
Use: "rm [profile] [email] [program] [project]",
160155
Short: "Remove a user from a project",
161-
Args: func(cmd *cobra.Command, args []string) error {
162-
if err := cobra.ExactArgs(2)(cmd, args); err != nil {
163-
return err
164-
}
165-
return validateProjectAndUser(args[0], args[1])
166-
},
156+
Args: cobra.ExactArgs(4),
167157
Run: func(cmd *cobra.Command, args []string) {
168-
projectID := args[0]
158+
p := args[0]
169159
username := args[1]
160+
program := args[2]
161+
project := args[3]
162+
projectID := fmt.Sprintf("%s-%s", program, project)
163+
164+
if !emailRegex.MatchString(strings.ToLower(username)) {
165+
fmt.Printf("Error: invalid email address '%s'\n", username)
166+
os.Exit(1)
167+
}
168+
170169
approve, _ := cmd.Flags().GetBool("approve")
171170

172-
client, closer := getRequestorClient()
171+
client, closer := getRequestorClient(p)
173172
defer closer()
174173

175174
reqs, err := client.RemoveUser(cmd.Context(), projectID, username)
@@ -199,13 +198,14 @@ var collaboratorRemoveUserCmd = &cobra.Command{
199198
}
200199

201200
var collaboratorApproveCmd = &cobra.Command{
202-
Use: "approve [request_id]",
201+
Use: "approve [profile] [request_id]",
203202
Short: "Approve a request (sign it)",
204-
Args: cobra.ExactArgs(1),
203+
Args: cobra.ExactArgs(2),
205204
Run: func(cmd *cobra.Command, args []string) {
206-
requestID := args[0]
205+
p := args[0]
206+
requestID := args[1]
207207

208-
client, closer := getRequestorClient()
208+
client, closer := getRequestorClient(p)
209209
defer closer()
210210

211211
req, err := client.UpdateRequest(cmd.Context(), requestID, "SIGNED")
@@ -220,15 +220,16 @@ var collaboratorApproveCmd = &cobra.Command{
220220
}
221221

222222
var collaboratorUpdateCmd = &cobra.Command{
223-
Use: "update [request_id] [status]",
223+
Use: "update [profile] [request_id] [status]",
224224
Short: "Update a request status",
225225
Hidden: true,
226-
Args: cobra.ExactArgs(2),
226+
Args: cobra.ExactArgs(3),
227227
Run: func(cmd *cobra.Command, args []string) {
228-
requestID := args[0]
229-
status := args[1]
228+
p := args[0]
229+
requestID := args[1]
230+
status := args[2]
230231

231-
client, closer := getRequestorClient()
232+
client, closer := getRequestorClient(p)
232233
defer closer()
233234

234235
req, err := client.UpdateRequest(cmd.Context(), requestID, status)
@@ -242,13 +243,13 @@ var collaboratorUpdateCmd = &cobra.Command{
242243
}
243244

244245
func init() {
245-
RootCmd.AddCommand(collaboratorCmd)
246-
collaboratorCmd.AddCommand(collaboratorListCmd)
247-
collaboratorCmd.AddCommand(collaboratorPendingCmd)
248-
collaboratorCmd.AddCommand(collaboratorAddUserCmd)
249-
collaboratorCmd.AddCommand(collaboratorRemoveUserCmd)
250-
collaboratorCmd.AddCommand(collaboratorApproveCmd)
251-
collaboratorCmd.AddCommand(collaboratorUpdateCmd)
246+
RootCmd.AddCommand(collaboratorsCmd)
247+
collaboratorsCmd.AddCommand(collaboratorListCmd)
248+
collaboratorsCmd.AddCommand(collaboratorPendingCmd)
249+
collaboratorsCmd.AddCommand(collaboratorAddUserCmd)
250+
collaboratorsCmd.AddCommand(collaboratorRemoveUserCmd)
251+
collaboratorsCmd.AddCommand(collaboratorApproveCmd)
252+
collaboratorsCmd.AddCommand(collaboratorUpdateCmd)
252253

253254
collaboratorListCmd.Flags().Bool("mine", false, "List my requests")
254255
collaboratorListCmd.Flags().Bool("active", false, "List only active requests")
@@ -259,6 +260,4 @@ func init() {
259260
collaboratorAddUserCmd.Flags().BoolP("approve", "a", false, "Automatically approve the requests")
260261

261262
collaboratorRemoveUserCmd.Flags().BoolP("approve", "a", false, "Automatically approve the revoke requests")
262-
263-
collaboratorCmd.PersistentFlags().StringVar(&profile, "profile", "", "Specify profile to use")
264263
}

0 commit comments

Comments
 (0)