Skip to content

Commit

Permalink
feat(cli): bash autocomplete affecting on global flags (#76)
Browse files Browse the repository at this point in the history
  • Loading branch information
apskhem authored Oct 21, 2024
1 parent 0a1f3df commit 64d5f7e
Show file tree
Hide file tree
Showing 11 changed files with 55 additions and 23 deletions.
2 changes: 1 addition & 1 deletion cli/cmd/cmds/ci.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (

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

Expand Down
2 changes: 1 addition & 1 deletion cli/cmd/cmds/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

type DeployCmd struct {
Project string `arg:"" help:"The path to the project to deploy."`
Project string `arg:"" help:"The path to the project to deploy." kong:"arg,predictor=path"`
}

func (c *DeployCmd) Run(ctx run.RunContext) error {
Expand Down
2 changes: 1 addition & 1 deletion cli/cmd/cmds/dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

type DumpCmd struct {
Project string `arg:"" help:"Path to the project."`
Project string `arg:"" help:"Path to the project." kong:"arg,predictor=path"`
Pretty bool `help:"Pretty print JSON output."`
}

Expand Down
2 changes: 1 addition & 1 deletion cli/cmd/cmds/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (

type RunCmd struct {
Artifact string `short:"a" help:"Dump all produced artifacts to the given path."`
Path string `arg:"" help:"The path to the target to execute (i.e., ./dir1+test)."`
Path string `kong:"arg,predictor=path" help:"The path to the target to execute (i.e., ./dir1+test)."`
Platform []string `short:"p" help:"Run the target with the given platform."`
Pretty bool `help:"Pretty print JSON output."`
TargetArgs []string `arg:"" help:"Arguments to pass to the target." default:""`
Expand Down
2 changes: 1 addition & 1 deletion cli/cmd/cmds/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type ScanCmd struct {
Earthfile bool `help:"Return the Earthfile targets for each project."`
Filter []string `short:"f" help:"Filter Earthfile targets by regular expression or blueprint results by path."`
Pretty bool `help:"Pretty print JSON output."`
RootPath string `arg:"" help:"Root path to scan for Earthfiles and their respective targets."`
RootPath string `kong:"arg,predictor=path" help:"Root path to scan for Earthfiles and their respective targets."`
}

func (c *ScanCmd) Run(ctx run.RunContext) error {
Expand Down
4 changes: 2 additions & 2 deletions cli/cmd/cmds/secret.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ type Get struct {
Key string `short:"k" help:"The key inside of the secret to get."`
Project string `help:"Path to a project to use for getting secret configuration."`
Provider string `short:"p" help:"The provider of the secret store." default:"aws"`
Path string `arg:"" help:"The path to the secret (or path in a project blueprint if --project is specified)."`
Path string `kong:"arg,predictor=path" help:"The path to the secret (or path in a project blueprint if --project is specified)."`
}

type Set struct {
Field []string `short:"f" help:"A secret field to set."`
Provider string `short:"p" help:"The provider of the secret store." default:"aws"`
Path string `arg:"" help:"The path to the secret (or path in a project blueprint if --project is specified)."`
Path string `kong:"arg,predictor=path" help:"The path to the secret (or path in a project blueprint if --project is specified)."`
Project string `help:"Path to a project to use for getting secret configuration."`
Value string `arg:"" help:"The value to set." default:""`
}
Expand Down
2 changes: 1 addition & 1 deletion cli/cmd/cmds/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

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

Expand Down
2 changes: 1 addition & 1 deletion cli/cmd/cmds/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
)

type ValidateCmd struct {
Project string `arg:"" help:"Path to the project."`
Project string `kong:"arg,predictor=path" help:"Path to the project."`
}

func (c *ValidateCmd) Run(ctx run.RunContext) error {
Expand Down
43 changes: 29 additions & 14 deletions cli/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,27 @@ import (
"github.com/input-output-hk/catalyst-forge/lib/project/schema"
"github.com/input-output-hk/catalyst-forge/lib/project/secrets"
"github.com/input-output-hk/catalyst-forge/lib/tools/walker"
"github.com/posener/complete"
"github.com/willabides/kongplete"
)

var version = "dev"

var cli struct {
cmds.GlobalArgs

Deploy cmds.DeployCmd `cmd:"" help:"Deploy a project."`
Dump cmds.DumpCmd `cmd:"" help:"Dumps a project's blueprint to JSON."`
CI cmds.CICmd `cmd:"" help:"Simulate a CI run."`
Release cmds.ReleaseCmd `cmd:"" help:"Release a project."`
Run cmds.RunCmd `cmd:"" help:"Run an Earthly target."`
Scan cmds.ScanCmd `cmd:"" help:"Scan for Earthfiles."`
Secret cmds.SecretCmd `cmd:"" help:"Manage secrets."`
Tag cmds.TagCmd `cmd:"" help:"Generate a tag for a project."`
Validate cmds.ValidateCmd `cmd:"" help:"Validates a project."`
Version VersionCmd `cmd:"" help:"Print the version."`
Deploy cmds.DeployCmd `kong:"cmd" help:"Deploy a project."`
Dump cmds.DumpCmd `kong:"cmd" help:"Dumps a project's blueprint to JSON."`
CI cmds.CICmd `kong:"cmd" help:"Simulate a CI run."`
Release cmds.ReleaseCmd `kong:"cmd" help:"Release a project."`
Run cmds.RunCmd `kong:"cmd" help:"Run an Earthly target."`
Scan cmds.ScanCmd `kong:"cmd" help:"Scan for Earthfiles."`
Secret cmds.SecretCmd `kong:"cmd" help:"Manage secrets."`
Tag cmds.TagCmd `kong:"cmd" help:"Generate a tag for a project."`
Validate cmds.ValidateCmd `kong:"cmd" help:"Validates a project."`
Version VersionCmd `kong:"cmd" help:"Print the version."`

InstallCompletions kongplete.InstallCompletions `cmd:"" help:"install shell completions"`
}

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

// Run is the entrypoint for the CLI tool.
func Run() int {
ctx := kong.Parse(&cli,
cliArgs := os.Args[1:]

parser := kong.Must(&cli,
kong.Name("forge"),
kong.Description("The CLI tool powering Catalyst Forge"))

kongplete.Complete(parser,
kongplete.WithPredictor("path", complete.PredictFiles("*")),
)

ctx, err := parser.Parse(cliArgs)
if err != nil {
fmt.Fprintf(os.Stderr, "forge: %v\n", err)
return 1
}

handler := log.New(os.Stderr)
switch cli.Verbose {
case 0:
Expand All @@ -79,9 +95,8 @@ func Run() int {
}
ctx.Bind(runctx)

err := ctx.Run()
if err != nil {
fmt.Fprintf(os.Stderr, "forge: %v", err)
if err := ctx.Run(); err != nil {
fmt.Fprintf(os.Stderr, "forge: %v\n", err)
return 1
}

Expand Down
5 changes: 5 additions & 0 deletions cli/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ require (
github.com/input-output-hk/catalyst-forge/lib/project v0.0.0
github.com/input-output-hk/catalyst-forge/lib/tools v0.0.0
github.com/migueleliasweb/go-github-mock v1.0.1
github.com/posener/complete v1.2.3
github.com/rogpeppe/go-internal v1.12.1-0.20240709150035-ccf4b4329d21
github.com/spf13/afero v1.11.0
github.com/stretchr/testify v1.9.0
github.com/willabides/kongplete v0.4.0
golang.org/x/exp v0.0.0-20231006140011-7918f672742d
)

Expand Down Expand Up @@ -59,6 +61,8 @@ require (
github.com/google/go-querystring v1.1.0 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/gorilla/mux v1.8.0 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect
github.com/kevinburke/ssh_config v1.2.0 // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
Expand All @@ -72,6 +76,7 @@ require (
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rivo/uniseg v0.4.7 // indirect
github.com/riywo/loginshell v0.0.0-20200815045211-7d26008be1ab // indirect
github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3 // indirect
github.com/skeema/knownhosts v1.2.2 // indirect
github.com/xanzy/ssh-agent v0.3.3 // indirect
Expand Down
12 changes: 12 additions & 0 deletions cli/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,12 @@ github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
github.com/hashicorp/errwrap v1.1.0 h1:OxrOeh75EUXMY8TBjag2fzXGZ40LB6IKw45YeGUDY2I=
github.com/hashicorp/errwrap v1.1.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
github.com/hashicorp/go-multierror v1.0.0/go.mod h1:dHtQlpGsu+cZNNAkkCN/P3hoUDHhCYQXV3UM06sGGrk=
github.com/hashicorp/go-multierror v1.1.1 h1:H5DkEtf6CXdFp0N0Em5UCwQpXMWke8IA0+lD48awMYo=
github.com/hashicorp/go-multierror v1.1.1/go.mod h1:iw975J/qwKPdAO1clOe2L8331t/9/fmwbPZ6JB6eMoM=
github.com/hexops/gotextdiff v1.0.3 h1:gitA9+qJrrTCsiCl7+kh75nPqQt1cx4ZkudSTLoUqJM=
github.com/hexops/gotextdiff v1.0.3/go.mod h1:pSWU5MAI3yDq+fZBTazCSJysOMbxWL1BSow5/V2vxeg=
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 h1:BQSFePA1RWJOlocH6Fxy8MmwDt+yVQYULKfN0RoTN8A=
Expand Down Expand Up @@ -173,11 +179,15 @@ github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINE
github.com/pkg/sftp v1.10.1/go.mod h1:lYOWFsE0bwd1+KfKJaKeuokY15vzFx25BLbzYYoAxZI=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/posener/complete v1.2.3 h1:NP0eAhjcjImqslEwo/1hq7gpajME0fTLTezBKDqfXqo=
github.com/posener/complete v1.2.3/go.mod h1:WZIdtGGp+qx0sLrYKtIRAruyNpv6hFCicSgv7Sy7s/s=
github.com/protocolbuffers/txtpbfmt v0.0.0-20230328191034-3462fbc510c0 h1:sadMIsgmHpEOGbUs6VtHBXRR1OHevnj7hLx9ZcdNGW4=
github.com/protocolbuffers/txtpbfmt v0.0.0-20230328191034-3462fbc510c0/go.mod h1:jgxiZysxFPM+iWKwQwPR+y+Jvo54ARd4EisXxKYpB5c=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=
github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
github.com/riywo/loginshell v0.0.0-20200815045211-7d26008be1ab h1:ZjX6I48eZSFetPb41dHudEyVr5v953N15TsNZXlkcWY=
github.com/riywo/loginshell v0.0.0-20200815045211-7d26008be1ab/go.mod h1:/PfPXh0EntGc3QAAyUaviy4S9tzy4Zp0e2ilq4voC6E=
github.com/rogpeppe/go-internal v1.12.1-0.20240709150035-ccf4b4329d21 h1:igWZJluD8KtEtAgRyF4x6lqcxDry1ULztksMJh2mnQE=
github.com/rogpeppe/go-internal v1.12.1-0.20240709150035-ccf4b4329d21/go.mod h1:RMRJLmBOqWacUkmJHRMiPKh1S1m3PA7Zh4W80/kWPpg=
github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3 h1:n661drycOFuPLCN3Uc8sB6B/s6Z4t2xvBgU1htSHuq8=
Expand All @@ -193,6 +203,8 @@ github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXf
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/willabides/kongplete v0.4.0 h1:eivXxkp5ud5+4+NVN9e4goxC5mSh3n1RHov+gsblM2g=
github.com/willabides/kongplete v0.4.0/go.mod h1:0P0jtWD9aTsqPSUAl4de35DLghrr57XcayPyvqSi2X8=
github.com/xanzy/ssh-agent v0.3.3 h1:+/15pJfg/RsTxqYcX6fHqOXZwwMP+2VyYWJeWM2qQFM=
github.com/xanzy/ssh-agent v0.3.3/go.mod h1:6dzNDKs0J9rVPHPhaGCukekBHKqfl+L3KghI1Bc68Uw=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
Expand Down

0 comments on commit 64d5f7e

Please sign in to comment.