Skip to content

Commit cbe717f

Browse files
committed
Add subcommand for updating applications
1 parent 3cde0f5 commit cbe717f

File tree

4 files changed

+61
-1
lines changed

4 files changed

+61
-1
lines changed

internal/api/mod.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,8 @@ func Get(base, endpoint, username, password string) (interface{}, error) {
9090
func Post(base, endpoint, username, password string, data interface{}) (interface{}, error) {
9191
return Request(base, endpoint, "POST", username, password, true, data)
9292
}
93+
94+
// Put sends a HTTP PUT request to the server.
95+
func Put(base, endpoint, username, password string, data interface{}) (interface{}, error) {
96+
return Request(base, endpoint, "PUT", username, password, true, data)
97+
}

internal/application/create.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ type createCommand struct {
1616
Arguments struct {
1717
Name string `positional-arg-name:"name" description:"The name of the application"`
1818
} `required:"true" positional-args:"true"`
19+
StrictCompatibility bool `long:"compat" description:"Enforce strict compatibility with Gotify"`
1920
}
2021

2122
func (c *createCommand) Execute(args []string) error {
@@ -25,7 +26,8 @@ func (c *createCommand) Execute(args []string) error {
2526

2627
func (c *createCommand) Run(s settings.Settings, password string) {
2728
data := map[string]interface{}{
28-
"name": c.Arguments.Name,
29+
"name": c.Arguments.Name,
30+
"strict_compatibility": c.StrictCompatibility,
2931
}
3032

3133
resp, err := api.Post(s.URL, createEndpoint, s.Username, password, data)

internal/application/mod.go

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

internal/application/update.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package application
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 = "/application/%d"
14+
)
15+
16+
type updateCommand struct {
17+
Arguments struct {
18+
ID uint `required:"true" positional-arg-name:"id" description:"The ID of the application"`
19+
} `positional-args:"true"`
20+
NewName *string `long:"new-name" description:"The new name of the application"`
21+
RefreshToken bool `long:"refresh" description:"Refresh the token of the application"`
22+
StrictCompatibility bool `long:"compat" description:"Enforce strict compatibility with Gotify"`
23+
}
24+
25+
func (c *updateCommand) Execute(args []string) error {
26+
settings.Runner = c
27+
return nil
28+
}
29+
30+
func (c *updateCommand) Run(s settings.Settings, password string) {
31+
if !c.RefreshToken && c.StrictCompatibility {
32+
log.Fatal("Can only enforce compatibility when refreshing the token of the application")
33+
}
34+
35+
data := map[string]interface{}{
36+
"refresh_token": c.RefreshToken,
37+
"strict_compatibility": c.StrictCompatibility,
38+
}
39+
40+
if c.NewName != nil {
41+
data["new_name"] = c.NewName
42+
}
43+
44+
populatedEndpoint := fmt.Sprintf(updateEndpoint, c.Arguments.ID)
45+
46+
resp, err := api.Put(s.URL, populatedEndpoint, s.Username, password, data)
47+
if err != nil {
48+
log.Fatal(err)
49+
}
50+
51+
ui.PrintJSON(resp)
52+
}

0 commit comments

Comments
 (0)