Skip to content

Commit

Permalink
feat(cmd): add collab & admin ssh cli api
Browse files Browse the repository at this point in the history
  • Loading branch information
aymanbagabas committed May 2, 2023
1 parent 4f07abf commit 53ba263
Show file tree
Hide file tree
Showing 3 changed files with 181 additions and 0 deletions.
88 changes: 88 additions & 0 deletions server/cmd/admin.go
Original file line number Diff line number Diff line change
@@ -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
}
2 changes: 2 additions & 0 deletions server/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
91 changes: 91 additions & 0 deletions server/cmd/collab.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 53ba263

Please sign in to comment.