| Initial import of the rickub CLI as a standalone public project 1a1d430 Olivier Girardot 9h ago | 1 | package cmd |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | |
| 6 | "github.com/spf13/cobra" |
| 7 | ) |
| 8 | |
| 9 | var collabPermission string |
| 10 | |
| 11 | // collaboratorCmd builds the `rickub repo collaborator` subtree. |
| 12 | func collaboratorCmd() *cobra.Command { |
| 13 | c := &cobra.Command{ |
| 14 | Use: "collaborator", |
| 15 | Aliases: []string{"collab"}, |
| 16 | Short: "Manage repository collaborators", |
| 17 | } |
| 18 | |
| 19 | listCmd := &cobra.Command{ |
| 20 | Use: "list <owner/repo>", |
| 21 | Short: "List collaborators (admin only)", |
| 22 | Args: cobra.ExactArgs(1), |
| 23 | RunE: runCollabList, |
| 24 | } |
| 25 | |
| 26 | addCmd := &cobra.Command{ |
| 27 | Use: "add <owner/repo> <user>", |
| 28 | Short: "Add or update a collaborator", |
| 29 | Args: cobra.ExactArgs(2), |
| 30 | RunE: runCollabAdd, |
| 31 | } |
| 32 | addCmd.Flags().StringVar(&collabPermission, "permission", "write", "read | write | admin") |
| 33 | |
| 34 | removeCmd := &cobra.Command{ |
| 35 | Use: "remove <owner/repo> <user>", |
| 36 | Aliases: []string{"rm"}, |
| 37 | Short: "Remove a collaborator", |
| 38 | Args: cobra.ExactArgs(2), |
| 39 | RunE: runCollabRemove, |
| 40 | } |
| 41 | |
| 42 | c.AddCommand(listCmd, addCmd, removeCmd) |
| 43 | return c |
| 44 | } |
| 45 | |
| 46 | func runCollabList(cmd *cobra.Command, args []string) error { |
| 47 | client, err := newClient() |
| 48 | if err != nil { |
| 49 | return err |
| 50 | } |
| 51 | owner, repo, err := parseOwnerRepo(args[0]) |
| 52 | if err != nil { |
| 53 | return err |
| 54 | } |
| 55 | cols, err := client.ListCollaborators(cmd.Context(), owner, repo) |
| 56 | if err != nil { |
| 57 | return err |
| 58 | } |
| 59 | if flagJSON { |
| 60 | return printJSON(cmd.OutOrStdout(), cols) |
| 61 | } |
| 62 | if len(cols) == 0 { |
| 63 | fmt.Fprintln(cmd.OutOrStdout(), "No collaborators.") |
| 64 | return nil |
| 65 | } |
| 66 | tw := newTabw(cmd.OutOrStdout()) |
| 67 | fmt.Fprintln(tw, "HANDLE\tPERMISSION\tNAME") |
| 68 | for _, c := range cols { |
| 69 | fmt.Fprintf(tw, "%s\t%s\t%s\n", c.Handle, c.Permission, dash(c.DisplayName)) |
| 70 | } |
| 71 | tw.Flush() |
| 72 | return nil |
| 73 | } |
| 74 | |
| 75 | func runCollabAdd(cmd *cobra.Command, args []string) error { |
| 76 | client, err := newClient() |
| 77 | if err != nil { |
| 78 | return err |
| 79 | } |
| 80 | owner, repo, err := parseOwnerRepo(args[0]) |
| 81 | if err != nil { |
| 82 | return err |
| 83 | } |
| 84 | col, err := client.PutCollaborator(cmd.Context(), owner, repo, args[1], collabPermission) |
| 85 | if err != nil { |
| 86 | return err |
| 87 | } |
| 88 | if flagJSON { |
| 89 | return printJSON(cmd.OutOrStdout(), col) |
| 90 | } |
| 91 | fmt.Fprintf(cmd.OutOrStdout(), "Granted %s %s on %s/%s\n", col.Handle, col.Permission, owner, repo) |
| 92 | return nil |
| 93 | } |
| 94 | |
| 95 | func runCollabRemove(cmd *cobra.Command, args []string) error { |
| 96 | client, err := newClient() |
| 97 | if err != nil { |
| 98 | return err |
| 99 | } |
| 100 | owner, repo, err := parseOwnerRepo(args[0]) |
| 101 | if err != nil { |
| 102 | return err |
| 103 | } |
| 104 | if err := client.DeleteCollaborator(cmd.Context(), owner, repo, args[1]); err != nil { |
| 105 | return err |
| 106 | } |
| 107 | fmt.Fprintf(cmd.OutOrStdout(), "Removed %s from %s/%s\n", args[1], owner, repo) |
| 108 | return nil |
| 109 | } |