-
Notifications
You must be signed in to change notification settings - Fork 141
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cmd): add collab & admin ssh cli api
- Loading branch information
1 parent
4f07abf
commit 53ba263
Showing
3 changed files
with
181 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |