Skip to content

Commit 64d5f7e

Browse files
authored
feat(cli): bash autocomplete affecting on global flags (#76)
1 parent 0a1f3df commit 64d5f7e

File tree

11 files changed

+55
-23
lines changed

11 files changed

+55
-23
lines changed

cli/cmd/cmds/ci.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77

88
type CICmd struct {
99
Artifact string `short:"a" help:"Dump all produced artifacts to the given path."`
10-
Path string `arg:"" default:"" help:"The path to scan from."`
10+
Path string `kong:"arg,predictor=path" default:"" help:"The path to scan from."`
1111
Platform []string `short:"p" help:"Run the target with the given platform."`
1212
}
1313

cli/cmd/cmds/deploy.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
)
99

1010
type DeployCmd struct {
11-
Project string `arg:"" help:"The path to the project to deploy."`
11+
Project string `arg:"" help:"The path to the project to deploy." kong:"arg,predictor=path"`
1212
}
1313

1414
func (c *DeployCmd) Run(ctx run.RunContext) error {

cli/cmd/cmds/dump.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
)
88

99
type DumpCmd struct {
10-
Project string `arg:"" help:"Path to the project."`
10+
Project string `arg:"" help:"Path to the project." kong:"arg,predictor=path"`
1111
Pretty bool `help:"Pretty print JSON output."`
1212
}
1313

cli/cmd/cmds/run.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77

88
type RunCmd struct {
99
Artifact string `short:"a" help:"Dump all produced artifacts to the given path."`
10-
Path string `arg:"" help:"The path to the target to execute (i.e., ./dir1+test)."`
10+
Path string `kong:"arg,predictor=path" help:"The path to the target to execute (i.e., ./dir1+test)."`
1111
Platform []string `short:"p" help:"Run the target with the given platform."`
1212
Pretty bool `help:"Pretty print JSON output."`
1313
TargetArgs []string `arg:"" help:"Arguments to pass to the target." default:""`

cli/cmd/cmds/scan.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ type ScanCmd struct {
1818
Earthfile bool `help:"Return the Earthfile targets for each project."`
1919
Filter []string `short:"f" help:"Filter Earthfile targets by regular expression or blueprint results by path."`
2020
Pretty bool `help:"Pretty print JSON output."`
21-
RootPath string `arg:"" help:"Root path to scan for Earthfiles and their respective targets."`
21+
RootPath string `kong:"arg,predictor=path" help:"Root path to scan for Earthfiles and their respective targets."`
2222
}
2323

2424
func (c *ScanCmd) Run(ctx run.RunContext) error {

cli/cmd/cmds/secret.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@ type Get struct {
1919
Key string `short:"k" help:"The key inside of the secret to get."`
2020
Project string `help:"Path to a project to use for getting secret configuration."`
2121
Provider string `short:"p" help:"The provider of the secret store." default:"aws"`
22-
Path string `arg:"" help:"The path to the secret (or path in a project blueprint if --project is specified)."`
22+
Path string `kong:"arg,predictor=path" help:"The path to the secret (or path in a project blueprint if --project is specified)."`
2323
}
2424

2525
type Set struct {
2626
Field []string `short:"f" help:"A secret field to set."`
2727
Provider string `short:"p" help:"The provider of the secret store." default:"aws"`
28-
Path string `arg:"" help:"The path to the secret (or path in a project blueprint if --project is specified)."`
28+
Path string `kong:"arg,predictor=path" help:"The path to the secret (or path in a project blueprint if --project is specified)."`
2929
Project string `help:"Path to a project to use for getting secret configuration."`
3030
Value string `arg:"" help:"The value to set." default:""`
3131
}

cli/cmd/cmds/tag.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88

99
type TagCmd struct {
1010
Pretty bool `short:"p" help:"Pretty print JSON output."`
11-
Project string `arg:"" help:"The project to generate tags for."`
11+
Project string `kong:"arg,predictor=path" help:"The project to generate tags for."`
1212
Trim bool `short:"t" help:"Trim the project path from the git tag."`
1313
}
1414

cli/cmd/cmds/validate.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
)
66

77
type ValidateCmd struct {
8-
Project string `arg:"" help:"Path to the project."`
8+
Project string `kong:"arg,predictor=path" help:"Path to the project."`
99
}
1010

1111
func (c *ValidateCmd) Run(ctx run.RunContext) error {

cli/cmd/main.go

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,27 @@ import (
1515
"github.com/input-output-hk/catalyst-forge/lib/project/schema"
1616
"github.com/input-output-hk/catalyst-forge/lib/project/secrets"
1717
"github.com/input-output-hk/catalyst-forge/lib/tools/walker"
18+
"github.com/posener/complete"
19+
"github.com/willabides/kongplete"
1820
)
1921

2022
var version = "dev"
2123

2224
var cli struct {
2325
cmds.GlobalArgs
2426

25-
Deploy cmds.DeployCmd `cmd:"" help:"Deploy a project."`
26-
Dump cmds.DumpCmd `cmd:"" help:"Dumps a project's blueprint to JSON."`
27-
CI cmds.CICmd `cmd:"" help:"Simulate a CI run."`
28-
Release cmds.ReleaseCmd `cmd:"" help:"Release a project."`
29-
Run cmds.RunCmd `cmd:"" help:"Run an Earthly target."`
30-
Scan cmds.ScanCmd `cmd:"" help:"Scan for Earthfiles."`
31-
Secret cmds.SecretCmd `cmd:"" help:"Manage secrets."`
32-
Tag cmds.TagCmd `cmd:"" help:"Generate a tag for a project."`
33-
Validate cmds.ValidateCmd `cmd:"" help:"Validates a project."`
34-
Version VersionCmd `cmd:"" help:"Print the version."`
27+
Deploy cmds.DeployCmd `kong:"cmd" help:"Deploy a project."`
28+
Dump cmds.DumpCmd `kong:"cmd" help:"Dumps a project's blueprint to JSON."`
29+
CI cmds.CICmd `kong:"cmd" help:"Simulate a CI run."`
30+
Release cmds.ReleaseCmd `kong:"cmd" help:"Release a project."`
31+
Run cmds.RunCmd `kong:"cmd" help:"Run an Earthly target."`
32+
Scan cmds.ScanCmd `kong:"cmd" help:"Scan for Earthfiles."`
33+
Secret cmds.SecretCmd `kong:"cmd" help:"Manage secrets."`
34+
Tag cmds.TagCmd `kong:"cmd" help:"Generate a tag for a project."`
35+
Validate cmds.ValidateCmd `kong:"cmd" help:"Validates a project."`
36+
Version VersionCmd `kong:"cmd" help:"Print the version."`
37+
38+
InstallCompletions kongplete.InstallCompletions `cmd:"" help:"install shell completions"`
3539
}
3640

3741
type VersionCmd struct{}
@@ -50,10 +54,22 @@ func (c *VersionCmd) Run() error {
5054

5155
// Run is the entrypoint for the CLI tool.
5256
func Run() int {
53-
ctx := kong.Parse(&cli,
57+
cliArgs := os.Args[1:]
58+
59+
parser := kong.Must(&cli,
5460
kong.Name("forge"),
5561
kong.Description("The CLI tool powering Catalyst Forge"))
5662

63+
kongplete.Complete(parser,
64+
kongplete.WithPredictor("path", complete.PredictFiles("*")),
65+
)
66+
67+
ctx, err := parser.Parse(cliArgs)
68+
if err != nil {
69+
fmt.Fprintf(os.Stderr, "forge: %v\n", err)
70+
return 1
71+
}
72+
5773
handler := log.New(os.Stderr)
5874
switch cli.Verbose {
5975
case 0:
@@ -79,9 +95,8 @@ func Run() int {
7995
}
8096
ctx.Bind(runctx)
8197

82-
err := ctx.Run()
83-
if err != nil {
84-
fmt.Fprintf(os.Stderr, "forge: %v", err)
98+
if err := ctx.Run(); err != nil {
99+
fmt.Fprintf(os.Stderr, "forge: %v\n", err)
85100
return 1
86101
}
87102

cli/go.mod

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@ require (
1515
github.com/input-output-hk/catalyst-forge/lib/project v0.0.0
1616
github.com/input-output-hk/catalyst-forge/lib/tools v0.0.0
1717
github.com/migueleliasweb/go-github-mock v1.0.1
18+
github.com/posener/complete v1.2.3
1819
github.com/rogpeppe/go-internal v1.12.1-0.20240709150035-ccf4b4329d21
1920
github.com/spf13/afero v1.11.0
2021
github.com/stretchr/testify v1.9.0
22+
github.com/willabides/kongplete v0.4.0
2123
golang.org/x/exp v0.0.0-20231006140011-7918f672742d
2224
)
2325

@@ -59,6 +61,8 @@ require (
5961
github.com/google/go-querystring v1.1.0 // indirect
6062
github.com/google/uuid v1.6.0 // indirect
6163
github.com/gorilla/mux v1.8.0 // indirect
64+
github.com/hashicorp/errwrap v1.1.0 // indirect
65+
github.com/hashicorp/go-multierror v1.1.1 // indirect
6266
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect
6367
github.com/kevinburke/ssh_config v1.2.0 // indirect
6468
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
@@ -72,6 +76,7 @@ require (
7276
github.com/pkg/errors v0.9.1 // indirect
7377
github.com/pmezard/go-difflib v1.0.0 // indirect
7478
github.com/rivo/uniseg v0.4.7 // indirect
79+
github.com/riywo/loginshell v0.0.0-20200815045211-7d26008be1ab // indirect
7580
github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3 // indirect
7681
github.com/skeema/knownhosts v1.2.2 // indirect
7782
github.com/xanzy/ssh-agent v0.3.3 // indirect

0 commit comments

Comments
 (0)