@@ -0,0 +1,88 @@
+package cmd
+
+import (
+ "strings"
+
+ "github.com/charmbracelet/soft-serve/server/backend"
+ "github.com/spf13/cobra"
+)
+
+func adminCommand() *cobra.Command {
+ cmd := &cobra.Command{
+ Use: "admin",
+ Aliases: []string{"admins"},
+ Short: "Manage admins",
+ }
+
+ cmd.AddCommand(
+ adminAddCommand(),
+ adminRemoveCommand(),
+ adminListCommand(),
+ )
+
+ return cmd
+}
+
+func adminAddCommand() *cobra.Command {
+ cmd := &cobra.Command{
+ Use: "add AUTHORIZED_KEY",
+ Short: "Add an admin",
+ Args: cobra.MinimumNArgs(1),
+ PersistentPreRunE: checkIfAdmin,
+ RunE: func(cmd *cobra.Command, args []string) error {
+ cfg, _ := fromContext(cmd)
+ pk, c, err := backend.ParseAuthorizedKey(strings.Join(args, " "))
+ if err != nil {
+ return err
+ }
+
+ return cfg.Backend.AddAdmin(pk, c)
+ },
+ }
+
+ return cmd
+}
+
+func adminRemoveCommand() *cobra.Command {
+ cmd := &cobra.Command{
+ Use: "remove AUTHORIZED_KEY",
+ Args: cobra.MinimumNArgs(1),
+ Short: "Remove an admin",
+ PersistentPreRunE: checkIfAdmin,
+ RunE: func(cmd *cobra.Command, args []string) error {
+ cfg, _ := fromContext(cmd)
+ pk, _, err := backend.ParseAuthorizedKey(strings.Join(args, " "))
+ if err != nil {
+ return err
+ }
+
+ return cfg.Backend.RemoveAdmin(pk)
+ },
+ }
+
+ return cmd
+}
+
+func adminListCommand() *cobra.Command {
+ cmd := &cobra.Command{
+ Use: "list",
+ Args: cobra.NoArgs,
+ Short: "List admins",
+ PersistentPreRunE: checkIfAdmin,
+ RunE: func(cmd *cobra.Command, _ []string) error {
+ cfg, _ := fromContext(cmd)
+ admins, err := cfg.Backend.Admins()
+ if err != nil {
+ return err
+ }
+
+ for _, admin := range admins {
+ cmd.Println(admin)
+ }
+
+ return nil
+ },
+ }
+
+ return cmd
+}
@@ -46,7 +46,9 @@ func rootCommand() *cobra.Command {
// TODO: use command usage template to include hostname and port
rootCmd.CompletionOptions.DisableDefaultCmd = true
rootCmd.AddCommand(
+ adminCommand(),
branchCommand(),
+ collabCommand(),
createCommand(),
deleteCommand(),
descriptionCommand(),
@@ -0,0 +1,91 @@
+package cmd
+
+import (
+ "strings"
+
+ "github.com/charmbracelet/soft-serve/server/backend"
+ "github.com/spf13/cobra"
+)
+
+func collabCommand() *cobra.Command {
+ cmd := &cobra.Command{
+ Use: "collab",
+ Aliases: []string{"collaborator", "collaborators"},
+ Short: "Manage collaborators",
+ }
+
+ cmd.AddCommand(
+ collabAddCommand(),
+ collabRemoveCommand(),
+ collabListCommand(),
+ )
+
+ return cmd
+}
+
+func collabAddCommand() *cobra.Command {
+ cmd := &cobra.Command{
+ Use: "add REPOSITORY AUTHORIZED_KEY",
+ Short: "Add a collaborator to a repo",
+ Args: cobra.MinimumNArgs(2),
+ PersistentPreRunE: checkIfAdmin,
+ RunE: func(cmd *cobra.Command, args []string) error {
+ cfg, _ := fromContext(cmd)
+ repo := args[0]
+ pk, c, err := backend.ParseAuthorizedKey(strings.Join(args[1:], " "))
+ if err != nil {
+ return err
+ }
+
+ return cfg.Backend.AddCollaborator(pk, c, repo)
+ },
+ }
+
+ return cmd
+}
+
+func collabRemoveCommand() *cobra.Command {
+ cmd := &cobra.Command{
+ Use: "remove REPOSITORY AUTHORIZED_KEY",
+ Args: cobra.MinimumNArgs(2),
+ Short: "Remove a collaborator from a repo",
+ PersistentPreRunE: checkIfAdmin,
+ RunE: func(cmd *cobra.Command, args []string) error {
+ cfg, _ := fromContext(cmd)
+ repo := args[0]
+ pk, _, err := backend.ParseAuthorizedKey(strings.Join(args[1:], " "))
+ if err != nil {
+ return err
+ }
+
+ return cfg.Backend.RemoveCollaborator(pk, repo)
+ },
+ }
+
+ return cmd
+}
+
+func collabListCommand() *cobra.Command {
+ cmd := &cobra.Command{
+ Use: "list REPOSITORY",
+ Short: "List collaborators for a repo",
+ Args: cobra.ExactArgs(1),
+ PersistentPreRunE: checkIfCollab,
+ RunE: func(cmd *cobra.Command, args []string) error {
+ cfg, _ := fromContext(cmd)
+ repo := args[0]
+ collabs, err := cfg.Backend.Collaborators(repo)
+ if err != nil {
+ return err
+ }
+
+ for _, c := range collabs {
+ cmd.Println(c)
+ }
+
+ return nil
+ },
+ }
+
+ return cmd
+}