Skip to content

Commit 49e8064

Browse files
committed
Implement partial update command for users
1 parent cbe717f commit 49e8064

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

internal/user/mod.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ package user
44
type Command struct {
55
Create createCommand `command:"create" alias:"c" description:"Create a new user"`
66
Delete deleteCommand `command:"delete" alias:"d" description:"Delete an existing user"`
7+
Update updateCommand `command:"update" alias:"u" description:"Update an existing user"`
78
List listCommand `command:"list" alias:"l" description:"List all existing users"`
89
Show showCommand `command:"show" alias:"s" description:"Show details of an existing user"`
910
}

internal/user/update.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package user
2+
3+
import (
4+
"fmt"
5+
"log"
6+
7+
"github.com/pushbits/cli/internal/api"
8+
"github.com/pushbits/cli/internal/settings"
9+
"github.com/pushbits/cli/internal/ui"
10+
)
11+
12+
const (
13+
updateEndpoint = "/user/%d"
14+
)
15+
16+
type updateCommand struct {
17+
Arguments struct {
18+
ID uint `positional-arg-name:"id" description:"The ID of the user"`
19+
} `required:"true" positional-args:"true"`
20+
NewName *string `long:"new-name" description:"The new name of the user"`
21+
NewMatrixID *string `long:"new-matrixid" description:"The new Matrix ID of the user"`
22+
}
23+
24+
func (c *updateCommand) Execute(args []string) error {
25+
settings.Runner = c
26+
return nil
27+
}
28+
29+
func (c *updateCommand) Run(s settings.Settings, password string) {
30+
data := map[string]interface{}{}
31+
32+
if c.NewName != nil {
33+
data["name"] = c.NewName
34+
}
35+
36+
if c.NewMatrixID != nil {
37+
data["matrix_id"] = c.NewMatrixID
38+
}
39+
40+
populatedEndpoint := fmt.Sprintf(updateEndpoint, c.Arguments.ID)
41+
42+
resp, err := api.Put(s.URL, populatedEndpoint, s.Username, password, data)
43+
if err != nil {
44+
log.Fatal(err)
45+
}
46+
47+
ui.PrintJSON(resp)
48+
}

0 commit comments

Comments
 (0)