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