Skip to content

Commit

Permalink
Merge pull request #2275 from rsteube/doc-creating-completers
Browse files Browse the repository at this point in the history
doc: creating completers
  • Loading branch information
rsteube authored Feb 29, 2024
2 parents 6556e80 + 43c6fdd commit beab5ec
Show file tree
Hide file tree
Showing 11 changed files with 1,338 additions and 6 deletions.
2 changes: 1 addition & 1 deletion completers/ln_completer/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package main

import "github.com/rsteube/carapace-bin/completers/ln_completer/cmd"
import "github.com/rsteube/carapace-bin/completers/manually_completer/cmd"

Check failure on line 3 in completers/ln_completer/main.go

View workflow job for this annotation

GitHub Actions / build

no required module provides package github.com/rsteube/carapace-bin/completers/manually_completer/cmd; to add it:

func main() {
cmd.Execute()
Expand Down
8 changes: 7 additions & 1 deletion docs/src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,13 @@
- [Project Layout](./development/projectLayout.md)
- [Build](./development/build.md)
- [Docker](./development/docker.md)
- [Creating completers](./development/creatingCompleters.md)
- [Creating Completers](./development/creatingCompleters.md)
- [Manually](./development/creatingCompleters/manually.md)
- [Parsing](./development/creatingCompleters/parsing.md)
- [Scraping](./development/creatingCompleters/scraping.md)
- [Updating Completers](./development/updatingCompleters.md)
- [Best Practices](./development/bestPractices.md)
- [Coupled Actions](./development/bestPractices/coupledActions.md)
- [Tools](./development/tools.md)
- [carapace-fmt](./development/tools/carapace-fmt.md)
- [carapace-lint](./development/tools/carapace-lint.md)
Expand Down
1 change: 1 addition & 0 deletions docs/src/development/bestPractices.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Best Practices
43 changes: 43 additions & 0 deletions docs/src/development/bestPractices/coupledActions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Coupled Actions

Use coupled actions to avoid repetition.

Sometimes an [Action] depends on the same flag values for multiple subcommands.
Since [ActionCallback] is needed to access these the code can become a bit cumbersome and bloated.

```go
carapace.Gen(get_allCmd).PositionalCompletion(
carapace.ActionCallback(func(c carapace.Context) carapace.Action {
return helm.ActionReleases(helm.ReleasesOpts{
Namespace: rootCmd.Flag("namespace").Value.String(),
KubeContext: rootCmd.Flag("kube-context").Value.String(),
})
}),
)
```

An alternative to this is creating a local [Action] that is coupled to the command.
Meaning, passing it the command and expecting specific flags to be present.

```go
// completers/helm_completer/cmd/action/release.go
func ActionReleases(cmd *cobra.Command) carapace.Action {
return carapace.ActionCallback(func(c carapace.Context) carapace.Action {
return helm.ActionReleases(helm.ReleasesOpts{
Namespace: cmd.Root().Flag("namespace").Value.String(),
KubeContext: cmd.Root().Flag("kube-context").Value.String(),
})
})
}
```

Thus the call becomes quite compact.

```go
carapace.Gen(get_allCmd).PositionalCompletion(
action.ActionReleases(get_allCmd),
)
```

[Action]:https://rsteube.github.io/carapace/carapace/action.html
[ActionCallback]:https://rsteube.github.io/carapace/carapace/defaultActions/actionCallback.html
1 change: 0 additions & 1 deletion docs/src/development/build.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ Completers can also be built (and thus maintained) separately.
```sh
cd completers/ln_completer
go install -ldflags="-s -w"
ln_completer _carapace
```

![](./build-separate.cast)
Expand Down
1,248 changes: 1,248 additions & 0 deletions docs/src/development/creatingCompleters/manually.cast

Large diffs are not rendered by default.

32 changes: 32 additions & 0 deletions docs/src/development/creatingCompleters/manually.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Manually

- Copy a basic completer for simplicity.

```sh
cp -r completers/ln_completer completers/manually_completer
```

- Update the package name in `main.go`.

```diff
-import "github.com/rsteube/carapace-bin/completers/ln_completer/cmd"
+import "github.com/rsteube/carapace-bin/completers/manually_completer/cmd"
```

- Create the root command.

```sh
echo | carapace-parse -n manually > root.go
```

- Add subcommands.

```sh
echo | carapace-parse -n subcommand -p root > subcommand.go
```

- Define flags and completions.

- [Build and install](../build.md#development).

![](./manually.cast)
1 change: 1 addition & 0 deletions docs/src/development/creatingCompleters/parsing.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Parsing
1 change: 1 addition & 0 deletions docs/src/development/creatingCompleters/scraping.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Scraping
6 changes: 3 additions & 3 deletions docs/src/development/projectLayout.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
├── cmd
│ ├── carapace # main application
│ ├── carapace-fmt # simple formatter
│ ├── carapace-generate # called by `go generate`
│ ├── carapace-generate # executed by `go generate`
│ ├── carapace-lint # simple linter
│ ├── carapace-parse # simple help output parser
│ └── carapace-shim # binary for runnable specs in windows
Expand All @@ -16,12 +16,12 @@
├── completers_release # optimized completers
├── dist # goreleaser dist folder
├── docs # documentation
├── internal # some internal packages
├── internal # internal packages
└── pkg # public packages
├── actions # shared actions that are also exposed as macros
│ └── tools # shared actions specific to tools
├── conditions # conditions for environment variable completion
├── env # environment variables
├── styles # style configurations
└── util # some util functions
└── util # util functions
```
1 change: 1 addition & 0 deletions docs/src/development/updatingCompleters.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Updating Completers

0 comments on commit beab5ec

Please sign in to comment.