Skip to content

Commit 09271d5

Browse files
committed
Added “role” flag to “sharing add”
1 parent 08af5e0 commit 09271d5

3 files changed

Lines changed: 50 additions & 5 deletions

File tree

api/sharing.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,12 @@ import (
66
)
77

88
// AddCollaborator invites a collaborator via username or email
9-
func (c *Client) AddCollaborator(cc context.Context, name string) error {
10-
req := c.newRequest(cc, "PUT", "/collaborators/"+url.PathEscape(name), true)
9+
func (c *Client) AddCollaborator(cc context.Context, name, role string) error {
10+
path := "/collaborators/" + url.PathEscape(name)
11+
if role != "" {
12+
path = path + "?role=" + url.QueryEscape(role)
13+
}
14+
req := c.newRequest(cc, "PUT", path, true)
1115
return req.doJSON(nil)
1216
}
1317

cli/sharing.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ func listMembers(cmd *cobra.Command, args []string) error {
6767

6868
// NewCmdSharingAdd generates the Cobra command for "sharing:add"
6969
func NewCmdSharingAdd() *cobra.Command {
70+
var roleFlag string
71+
7072
addCmd := &cobra.Command{
7173
Use: "add EMAIL",
7274
Short: "Add a collaborator",
@@ -84,7 +86,7 @@ func NewCmdSharingAdd() *cobra.Command {
8486

8587
var multiErr *multierror.Error
8688
for _, name := range args {
87-
err := c.AddCollaborator(cc, name)
89+
err := c.AddCollaborator(cc, name, roleFlag)
8890

8991
if err != nil {
9092
multiErr = multierror.Append(multiErr, err)
@@ -99,12 +101,15 @@ func NewCmdSharingAdd() *cobra.Command {
99101
},
100102
}
101103

104+
// Flags and options
105+
addCmd.Flags().StringVar(&roleFlag, "role", "", "Collaborator role")
106+
102107
return addCmd
103108
}
104109

105110
// NewCmdSharingRemove generates the Cobra command for "sharing:add"
106111
func NewCmdSharingRemove() *cobra.Command {
107-
addCmd := &cobra.Command{
112+
rmCmd := &cobra.Command{
108113
Use: "remove EMAIL",
109114
Short: "Remove a collaborator",
110115
RunE: func(cmd *cobra.Command, args []string) error {
@@ -136,7 +141,7 @@ func NewCmdSharingRemove() *cobra.Command {
136141
},
137142
}
138143

139-
return addCmd
144+
return rmCmd
140145
}
141146

142147
// Root for sharing/collaboration subcommands

cli/sharing_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"github.com/gemfury/cli/internal/ctx"
66
"github.com/gemfury/cli/internal/testutil"
77
"github.com/gemfury/cli/pkg/terminal"
8+
"net/http"
89
"strings"
910
"testing"
1011
)
@@ -85,6 +86,41 @@ func TestSharingAddCommandSuccess(t *testing.T) {
8586
}
8687
}
8788

89+
func TestSharingAddWithRoleCommandSuccess(t *testing.T) {
90+
auth := terminal.TestAuther("user", "abc123", nil)
91+
term := terminal.NewForTest()
92+
93+
// Fire up test server
94+
roleQuery := "unchanged-by-server"
95+
path := "/collaborators/owner@example.com"
96+
server := testutil.APIServerCustom(t, func(mux *http.ServeMux) {
97+
mux.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
98+
if m := r.Method; m != "PUT" {
99+
t.Errorf("Incorrect method: %q", m)
100+
}
101+
roleQuery = r.URL.Query().Get("role")
102+
w.Write([]byte("{}"))
103+
})
104+
})
105+
defer server.Close()
106+
107+
cc := cli.TestContext(term, auth)
108+
flags := ctx.GlobalFlags(cc)
109+
flags.Endpoint = server.URL
110+
111+
err := runCommandNoErr(cc, []string{"sharing", "add", "owner@example.com", "--role", "owner"})
112+
if err != nil {
113+
t.Fatal(err)
114+
}
115+
116+
exp := "Invited \"owner@example.com\" as a collaborator\n"
117+
if outStr := string(term.OutBytes()); !strings.HasSuffix(outStr, exp) {
118+
t.Errorf("Expected output to include %q, got %q", exp, outStr)
119+
} else if r := roleQuery; r != "owner" {
120+
t.Errorf(`Expected role to be "owner", got %q`, r)
121+
}
122+
}
123+
88124
func TestSharingAddCommandUnauthorized(t *testing.T) {
89125
path := "/collaborators/added@example.com"
90126
server := testutil.APIServer(t, "PUT", path, "{}", 200)

0 commit comments

Comments
 (0)