rickub/clipublic Fork 0
main
Commits
Clone
git clone https://git.rickub.com/rickub/cli.git
git clone ssh://git@rickub.com/rickub/cli.git
Initial import of the rickub CLI as a standalone public project 1a1d430Unverified · on main · Olivier Girardot · 7h ago
collaborator.go · 109 lines · 2.5 KBGo Blame HistoryRaw
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
package cmd

import (
	"fmt"

	"github.com/spf13/cobra"
)

var collabPermission string

// collaboratorCmd builds the `rickub repo collaborator` subtree.
func collaboratorCmd() *cobra.Command {
	c := &cobra.Command{
		Use:     "collaborator",
		Aliases: []string{"collab"},
		Short:   "Manage repository collaborators",
	}

	listCmd := &cobra.Command{
		Use:   "list <owner/repo>",
		Short: "List collaborators (admin only)",
		Args:  cobra.ExactArgs(1),
		RunE:  runCollabList,
	}

	addCmd := &cobra.Command{
		Use:   "add <owner/repo> <user>",
		Short: "Add or update a collaborator",
		Args:  cobra.ExactArgs(2),
		RunE:  runCollabAdd,
	}
	addCmd.Flags().StringVar(&collabPermission, "permission", "write", "read | write | admin")

	removeCmd := &cobra.Command{
		Use:     "remove <owner/repo> <user>",
		Aliases: []string{"rm"},
		Short:   "Remove a collaborator",
		Args:    cobra.ExactArgs(2),
		RunE:    runCollabRemove,
	}

	c.AddCommand(listCmd, addCmd, removeCmd)
	return c
}

func runCollabList(cmd *cobra.Command, args []string) error {
	client, err := newClient()
	if err != nil {
		return err
	}
	owner, repo, err := parseOwnerRepo(args[0])
	if err != nil {
		return err
	}
	cols, err := client.ListCollaborators(cmd.Context(), owner, repo)
	if err != nil {
		return err
	}
	if flagJSON {
		return printJSON(cmd.OutOrStdout(), cols)
	}
	if len(cols) == 0 {
		fmt.Fprintln(cmd.OutOrStdout(), "No collaborators.")
		return nil
	}
	tw := newTabw(cmd.OutOrStdout())
	fmt.Fprintln(tw, "HANDLE\tPERMISSION\tNAME")
	for _, c := range cols {
		fmt.Fprintf(tw, "%s\t%s\t%s\n", c.Handle, c.Permission, dash(c.DisplayName))
	}
	tw.Flush()
	return nil
}

func runCollabAdd(cmd *cobra.Command, args []string) error {
	client, err := newClient()
	if err != nil {
		return err
	}
	owner, repo, err := parseOwnerRepo(args[0])
	if err != nil {
		return err
	}
	col, err := client.PutCollaborator(cmd.Context(), owner, repo, args[1], collabPermission)
	if err != nil {
		return err
	}
	if flagJSON {
		return printJSON(cmd.OutOrStdout(), col)
	}
	fmt.Fprintf(cmd.OutOrStdout(), "Granted %s %s on %s/%s\n", col.Handle, col.Permission, owner, repo)
	return nil
}

func runCollabRemove(cmd *cobra.Command, args []string) error {
	client, err := newClient()
	if err != nil {
		return err
	}
	owner, repo, err := parseOwnerRepo(args[0])
	if err != nil {
		return err
	}
	if err := client.DeleteCollaborator(cmd.Context(), owner, repo, args[1]); err != nil {
		return err
	}
	fmt.Fprintf(cmd.OutOrStdout(), "Removed %s from %s/%s\n", args[1], owner, repo)
	return nil
}