1package cmd
 2
 3import (
 4	"github.com/spf13/cobra"
 5)
 6
 7func collabCommand() *cobra.Command {
 8	cmd := &cobra.Command{
 9		Use:     "collab",
10		Aliases: []string{"collabs", "collaborator", "collaborators"},
11		Short:   "Manage collaborators",
12	}
13
14	cmd.AddCommand(
15		collabAddCommand(),
16		collabRemoveCommand(),
17		collabListCommand(),
18	)
19
20	return cmd
21}
22
23func collabAddCommand() *cobra.Command {
24	cmd := &cobra.Command{
25		Use:               "add REPOSITORY USERNAME",
26		Short:             "Add a collaborator to a repo",
27		Args:              cobra.ExactArgs(2),
28		PersistentPreRunE: checkIfAdmin,
29		RunE: func(cmd *cobra.Command, args []string) error {
30			cfg, _ := fromContext(cmd)
31			repo := args[0]
32			username := args[1]
33
34			return cfg.Backend.AddCollaborator(repo, username)
35		},
36	}
37
38	return cmd
39}
40
41func collabRemoveCommand() *cobra.Command {
42	cmd := &cobra.Command{
43		Use:               "remove REPOSITORY USERNAME",
44		Args:              cobra.ExactArgs(2),
45		Short:             "Remove a collaborator from a repo",
46		PersistentPreRunE: checkIfAdmin,
47		RunE: func(cmd *cobra.Command, args []string) error {
48			cfg, _ := fromContext(cmd)
49			repo := args[0]
50			username := args[1]
51
52			return cfg.Backend.RemoveCollaborator(repo, username)
53		},
54	}
55
56	return cmd
57}
58
59func collabListCommand() *cobra.Command {
60	cmd := &cobra.Command{
61		Use:               "list REPOSITORY",
62		Short:             "List collaborators for a repo",
63		Args:              cobra.ExactArgs(1),
64		PersistentPreRunE: checkIfCollab,
65		RunE: func(cmd *cobra.Command, args []string) error {
66			cfg, _ := fromContext(cmd)
67			repo := args[0]
68			collabs, err := cfg.Backend.Collaborators(repo)
69			if err != nil {
70				return err
71			}
72
73			for _, c := range collabs {
74				cmd.Println(c)
75			}
76
77			return nil
78		},
79	}
80
81	return cmd
82}