Skip to content

Commit 146d6e3

Browse files
committed
Use kong to rework CLI
1 parent 335b10b commit 146d6e3

File tree

19 files changed

+172
-184
lines changed

19 files changed

+172
-184
lines changed

cmd/pbcli/main.go

Lines changed: 10 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,23 @@
11
package main
22

33
import (
4-
"os"
5-
6-
"github.com/jessevdk/go-flags"
4+
"github.com/alecthomas/kong"
75

86
"github.com/pushbits/cli/internal/application"
97
"github.com/pushbits/cli/internal/commands"
10-
"github.com/pushbits/cli/internal/settings"
11-
"github.com/pushbits/cli/internal/ui"
8+
"github.com/pushbits/cli/internal/options"
129
"github.com/pushbits/cli/internal/user"
1310
)
1411

15-
type options struct {
16-
settings.Settings
17-
Application application.Command `command:"application" alias:"a" description:"Configure applications"`
18-
User user.Command `command:"user" alias:"u" description:"Configure users"`
19-
Version commands.VersionCommand `command:"version" alias:"v" description:"Print the program version"`
12+
var cmd struct {
13+
options.Options
14+
Application application.Command `cmd:"application" aliases:"a" help:"Configure applications"`
15+
User user.Command `cmd:"user" aliases:"u" help:"Configure users"`
16+
Version commands.VersionCommand `cmd:"version" aliases:"v" help:"Print the program version"`
2017
}
2118

22-
var (
23-
cmds options
24-
parser = flags.NewParser(&cmds, flags.Default)
25-
)
26-
2719
func main() {
28-
_, err := parser.Parse()
29-
if err != nil {
30-
os.Exit(1)
31-
}
32-
33-
s := &settings.Settings{
34-
URL: cmds.URL,
35-
Username: cmds.Username,
36-
Proxy: cmds.Proxy,
37-
}
38-
39-
password := ui.GetPassword("Current password of user " + s.Username + ": ")
40-
41-
settings.Runner.Run(s, password)
20+
ctx := kong.Parse(&cmd)
21+
err := ctx.Run(&cmd.Options)
22+
ctx.FatalIfErrorf(err)
4223
}

go.mod

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@ module github.com/pushbits/cli
33
go 1.17
44

55
require (
6-
github.com/jessevdk/go-flags v1.4.0
6+
github.com/alecthomas/kong v0.5.0
77
golang.org/x/term v0.0.0-20210503060354-a79de5458b56
88
)
99

10-
require golang.org/x/sys v0.0.0-20210603125802-9665404d3644 // indirect
10+
require (
11+
github.com/pkg/errors v0.9.1 // indirect
12+
golang.org/x/sys v0.0.0-20210603125802-9665404d3644 // indirect
13+
)

go.sum

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,23 @@
1-
github.com/jessevdk/go-flags v1.4.0 h1:4IU2WS7AumrZ/40jfhf4QVDMsQwqA7VEHozFRrGARJA=
2-
github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
1+
github.com/alecthomas/kong v0.5.0 h1:u8Kdw+eeml93qtMZ04iei0CFYve/WPcA5IFh+9wSskE=
2+
github.com/alecthomas/kong v0.5.0/go.mod h1:uzxf/HUh0tj43x1AyJROl3JT7SgsZ5m+icOv1csRhc0=
3+
github.com/alecthomas/repr v0.0.0-20210801044451-80ca428c5142 h1:8Uy0oSf5co/NZXje7U1z8Mpep++QJOldL2hs/sBQf48=
4+
github.com/alecthomas/repr v0.0.0-20210801044451-80ca428c5142/go.mod h1:2kn6fqh/zIyPLmm3ugklbEi5hg5wS435eygvNfaDQL8=
5+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
6+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
7+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
8+
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
9+
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
10+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
11+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
12+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
13+
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
14+
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
315
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
416
golang.org/x/sys v0.0.0-20210603125802-9665404d3644 h1:CA1DEQ4NdKphKeL70tvsWNdT5oFh1lOjihRcEDROi0I=
517
golang.org/x/sys v0.0.0-20210603125802-9665404d3644/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
618
golang.org/x/term v0.0.0-20210503060354-a79de5458b56 h1:b8jxX3zqjpqb2LklXPzKSGJhzyxCOZSz8ncv8Nv+y7w=
719
golang.org/x/term v0.0.0-20210503060354-a79de5458b56/go.mod h1:tfny5GFUkzUvx4ps4ajbZsCe5lw1metzhBm9T3x7oIY=
20+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
21+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
22+
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo=
23+
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

internal/application/create.go

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44
"log"
55

66
"github.com/pushbits/cli/internal/api"
7-
"github.com/pushbits/cli/internal/settings"
7+
"github.com/pushbits/cli/internal/options"
88
"github.com/pushbits/cli/internal/ui"
99
)
1010

@@ -13,27 +13,25 @@ const (
1313
)
1414

1515
type createCommand struct {
16-
Arguments struct {
17-
Name string `positional-arg-name:"name" description:"The name of the application"`
18-
} `required:"true" positional-args:"true"`
19-
StrictCompatibility bool `long:"compat" description:"Enforce strict compatibility with Gotify"`
16+
options.AuthOptions
17+
Name string `arg:"name" help:"The name of the application"`
18+
StrictCompatibility bool `long:"compat" help:"Enforce strict compatibility with Gotify"`
2019
}
2120

22-
func (c *createCommand) Execute(args []string) error {
23-
settings.Runner = c
24-
return nil
25-
}
21+
func (c *createCommand) Run(s *options.Options) error {
22+
password := ui.GetCurrentPassword(c.Username)
2623

27-
func (c *createCommand) Run(s *settings.Settings, password string) {
2824
data := map[string]interface{}{
29-
"name": c.Arguments.Name,
25+
"name": c.Name,
3026
"strict_compatibility": c.StrictCompatibility,
3127
}
3228

33-
resp, err := api.Post(s.URL, createEndpoint, s.Proxy, s.Username, password, data)
29+
resp, err := api.Post(c.URL, createEndpoint, c.Proxy, c.Username, password, data)
3430
if err != nil {
3531
log.Fatal(err)
3632
}
3733

3834
ui.PrintJSON(resp)
35+
36+
return nil
3937
}

internal/application/delete.go

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"log"
66

77
"github.com/pushbits/cli/internal/api"
8-
"github.com/pushbits/cli/internal/settings"
8+
"github.com/pushbits/cli/internal/options"
99
"github.com/pushbits/cli/internal/ui"
1010
)
1111

@@ -14,23 +14,21 @@ const (
1414
)
1515

1616
type deleteCommand struct {
17-
Arguments struct {
18-
ID uint `positional-arg-name:"id" description:"The ID of the application"`
19-
} `required:"true" positional-args:"true"`
17+
options.AuthOptions
18+
ID uint `arg:"" help:"The ID of the application"`
2019
}
2120

22-
func (c *deleteCommand) Execute(args []string) error {
23-
settings.Runner = c
24-
return nil
25-
}
21+
func (c *deleteCommand) Run(s *options.Options) error {
22+
password := ui.GetCurrentPassword(c.Username)
2623

27-
func (c *deleteCommand) Run(s *settings.Settings, password string) {
28-
populatedEndpoint := fmt.Sprintf(deleteEndpoint, c.Arguments.ID)
24+
populatedEndpoint := fmt.Sprintf(deleteEndpoint, c.ID)
2925

30-
resp, err := api.Delete(s.URL, populatedEndpoint, s.Proxy, s.Username, password)
26+
resp, err := api.Delete(c.URL, populatedEndpoint, c.Proxy, c.Username, password)
3127
if err != nil {
3228
log.Fatal(err)
3329
}
3430

3531
ui.PrintJSON(resp)
32+
33+
return nil
3634
}

internal/application/list.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,27 @@ import (
44
"log"
55

66
"github.com/pushbits/cli/internal/api"
7-
"github.com/pushbits/cli/internal/settings"
7+
"github.com/pushbits/cli/internal/options"
88
"github.com/pushbits/cli/internal/ui"
99
)
1010

1111
const (
1212
listEndpoint = "/application"
1313
)
1414

15-
type listCommand struct{}
16-
17-
func (c *listCommand) Execute(args []string) error {
18-
settings.Runner = c
19-
return nil
15+
type listCommand struct {
16+
options.AuthOptions
2017
}
2118

22-
func (c *listCommand) Run(s *settings.Settings, password string) {
23-
resp, err := api.Get(s.URL, listEndpoint, s.Proxy, s.Username, password)
19+
func (c *listCommand) Run(s *options.Options) error {
20+
password := ui.GetCurrentPassword(c.Username)
21+
22+
resp, err := api.Get(c.URL, listEndpoint, c.Proxy, c.Username, password)
2423
if err != nil {
2524
log.Fatal(err)
2625
}
2726

2827
ui.PrintJSON(resp)
28+
29+
return nil
2930
}

internal/application/mod.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ package application
22

33
// Command contains all subcommands provided by this package.
44
type Command struct {
5-
Create createCommand `command:"create" alias:"c" description:"Create a new application for a user"`
6-
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"`
8-
List listCommand `command:"list" alias:"l" description:"List all existing applications of the user"`
9-
Show showCommand `command:"show" alias:"s" description:"Show details of an existing application of a user"`
5+
Create createCommand `cmd:"create" aliases:"c" help:"Create a new application for a user"`
6+
Delete deleteCommand `cmd:"delete" aliases:"d" help:"Delete an existing application for a user"`
7+
Update updateCommand `cmd:"update" aliases:"u" help:"Update an existing application for a user"`
8+
List listCommand `cmd:"list" aliases:"l" help:"List all existing applications of the user"`
9+
Show showCommand `cmd:"show" aliases:"s" help:"Show details of an existing application of a user"`
1010
}

internal/application/show.go

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"log"
66

77
"github.com/pushbits/cli/internal/api"
8-
"github.com/pushbits/cli/internal/settings"
8+
"github.com/pushbits/cli/internal/options"
99
"github.com/pushbits/cli/internal/ui"
1010
)
1111

@@ -14,23 +14,21 @@ const (
1414
)
1515

1616
type showCommand struct {
17-
Arguments struct {
18-
ID uint `positional-arg-name:"id" description:"The ID of the application"`
19-
} `required:"true" positional-args:"true"`
17+
options.AuthOptions
18+
ID uint `arg:"" help:"The ID of the application"`
2019
}
2120

22-
func (c *showCommand) Execute(args []string) error {
23-
settings.Runner = c
24-
return nil
25-
}
21+
func (c *showCommand) Run(s *options.Options) error {
22+
password := ui.GetCurrentPassword(c.Username)
2623

27-
func (c *showCommand) Run(s *settings.Settings, password string) {
28-
populatedEndpoint := fmt.Sprintf(showEndpoint, c.Arguments.ID)
24+
populatedEndpoint := fmt.Sprintf(showEndpoint, c.ID)
2925

30-
resp, err := api.Get(s.URL, populatedEndpoint, s.Proxy, s.Username, password)
26+
resp, err := api.Get(c.URL, populatedEndpoint, c.Proxy, c.Username, password)
3127
if err != nil {
3228
log.Fatal(err)
3329
}
3430

3531
ui.PrintJSON(resp)
32+
33+
return nil
3634
}

internal/application/update.go

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"log"
66

77
"github.com/pushbits/cli/internal/api"
8-
"github.com/pushbits/cli/internal/settings"
8+
"github.com/pushbits/cli/internal/options"
99
"github.com/pushbits/cli/internal/ui"
1010
)
1111

@@ -14,20 +14,16 @@ const (
1414
)
1515

1616
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"`
17+
options.AuthOptions
18+
ID uint `arg:"" help:"The ID of the application"`
19+
NewName string `long:"new-name" help:"The new name of the application" optional:""`
20+
RefreshToken bool `long:"refresh" help:"Refresh the token of the application"`
21+
StrictCompatibility bool `long:"compat" help:"Enforce strict compatibility with Gotify"`
2322
}
2423

25-
func (c *updateCommand) Execute(args []string) error {
26-
settings.Runner = c
27-
return nil
28-
}
24+
func (c *updateCommand) Run(s *options.Options) error {
25+
password := ui.GetCurrentPassword(c.Username)
2926

30-
func (c *updateCommand) Run(s *settings.Settings, password string) {
3127
if !c.RefreshToken && c.StrictCompatibility {
3228
log.Fatal("Can only enforce compatibility when refreshing the token of the application")
3329
}
@@ -37,16 +33,18 @@ func (c *updateCommand) Run(s *settings.Settings, password string) {
3733
"strict_compatibility": c.StrictCompatibility,
3834
}
3935

40-
if c.NewName != nil {
36+
if len(c.NewName) > 0 {
4137
data["new_name"] = c.NewName
4238
}
4339

44-
populatedEndpoint := fmt.Sprintf(updateEndpoint, c.Arguments.ID)
40+
populatedEndpoint := fmt.Sprintf(updateEndpoint, c.ID)
4541

46-
resp, err := api.Put(s.URL, populatedEndpoint, s.Proxy, s.Username, password, data)
42+
resp, err := api.Put(c.URL, populatedEndpoint, c.Proxy, c.Username, password, data)
4743
if err != nil {
4844
log.Fatal(err)
4945
}
5046

5147
ui.PrintJSON(resp)
48+
49+
return nil
5250
}

internal/commands/version.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,13 @@ import (
44
"fmt"
55

66
"github.com/pushbits/cli/internal/buildconfig"
7-
"github.com/pushbits/cli/internal/settings"
7+
"github.com/pushbits/cli/internal/options"
88
)
99

1010
type VersionCommand struct{}
1111

12-
func (c *VersionCommand) Execute(args []string) error {
13-
settings.Runner = c
14-
return nil
15-
}
16-
17-
func (c *VersionCommand) Run(s *settings.Settings, password string) {
12+
func (c *VersionCommand) Run(s *options.Options) error {
1813
fmt.Printf("pbcli %s\n", buildconfig.Version)
14+
15+
return nil
1916
}

0 commit comments

Comments
 (0)