Skip to content

Commit 5131d0f

Browse files
authored
feat: adds bash autocompletions (#72)
1 parent 92eb76d commit 5131d0f

File tree

11 files changed

+57
-22
lines changed

11 files changed

+57
-22
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
@@ -9,7 +9,7 @@ import (
99

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

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: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,26 @@ import (
1616
"github.com/input-output-hk/catalyst-forge/lib/project/schema"
1717
"github.com/input-output-hk/catalyst-forge/lib/project/secrets"
1818
"github.com/input-output-hk/catalyst-forge/lib/tools/walker"
19+
"github.com/posener/complete"
20+
"github.com/willabides/kongplete"
1921
)
2022

2123
var version = "dev"
2224

2325
var cli struct {
2426
cmds.GlobalArgs
2527

26-
Deploy cmds.DeployCmd `cmd:"" help:"Deploy a project."`
27-
Dump cmds.DumpCmd `cmd:"" help:"Dumps a project's blueprint to JSON."`
28-
CI cmds.CICmd `cmd:"" help:"Simulate a CI run."`
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."`
28+
Deploy cmds.DeployCmd `kong:"cmd" help:"Deploy a project." `
29+
Dump cmds.DumpCmd `kong:"cmd" help:"Dumps a project's blueprint to JSON."`
30+
CI cmds.CICmd `kong:"cmd" help:"Simulate a CI run."`
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,16 @@ 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+
5767
handler := log.New(os.Stderr)
5868
switch cli.Verbose {
5969
case 0:
@@ -66,6 +76,12 @@ func Run() int {
6676
handler.SetLevel(log.DebugLevel)
6777
}
6878

79+
ctx, err := parser.Parse(cliArgs)
80+
if err != nil {
81+
fmt.Fprintf(os.Stderr, "forge: %v\n", err)
82+
return 1
83+
}
84+
6985
logger := slog.New(handler)
7086
loader := project.NewDefaultProjectLoader(logger)
7187
runctx := run.RunContext{
@@ -83,9 +99,8 @@ func Run() int {
8399
}
84100
ctx.Bind(runctx)
85101

86-
err := ctx.Run()
87-
if err != nil {
88-
fmt.Fprintf(os.Stderr, "forge: %v", err)
102+
if err := ctx.Run(); err != nil {
103+
fmt.Fprintf(os.Stderr, "forge: %v\n", err)
89104
return 1
90105
}
91106

cli/go.mod

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,12 @@ require (
1313
github.com/go-git/go-git/v5 v5.12.0
1414
github.com/input-output-hk/catalyst-forge/lib/project v0.0.0
1515
github.com/input-output-hk/catalyst-forge/lib/tools v0.0.0
16+
github.com/posener/complete v1.2.3
1617
github.com/rogpeppe/go-internal v1.12.1-0.20240709150035-ccf4b4329d21
1718
github.com/spf13/afero v1.11.0
1819
github.com/stretchr/testify v1.9.0
20+
github.com/willabides/kongplete v0.4.0
21+
github.com/yuin/goldmark v1.7.4
1922
golang.org/x/exp v0.0.0-20231006140011-7918f672742d
2023
)
2124

@@ -54,6 +57,8 @@ require (
5457
github.com/go-logfmt/logfmt v0.6.0 // indirect
5558
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
5659
github.com/google/uuid v1.6.0 // indirect
60+
github.com/hashicorp/errwrap v1.1.0 // indirect
61+
github.com/hashicorp/go-multierror v1.1.1 // indirect
5762
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect
5863
github.com/kevinburke/ssh_config v1.2.0 // indirect
5964
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
@@ -67,6 +72,7 @@ require (
6772
github.com/pkg/errors v0.9.1 // indirect
6873
github.com/pmezard/go-difflib v1.0.0 // indirect
6974
github.com/rivo/uniseg v0.4.7 // indirect
75+
github.com/riywo/loginshell v0.0.0-20200815045211-7d26008be1ab // indirect
7076
github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3 // indirect
7177
github.com/skeema/knownhosts v1.2.2 // indirect
7278
github.com/xanzy/ssh-agent v0.3.3 // indirect

0 commit comments

Comments
 (0)