Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,23 @@ yay -S infisical-bin

Download binaries from [GitHub Releases](https://github.com/Infisical/cli/releases).

## Shell Completions

The CLI can generate autocompletion scripts for bash, zsh, fish, and PowerShell via `infisical completion [shell]`. For example, to enable completions for the current session:

```bash
# bash
source <(infisical completion bash)

# zsh
infisical completion zsh > "${fpath[1]}/_infisical"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Zsh Session Is Not Activated

Writing _infisical into ${fpath[1]} does not reliably enable completion in the current shell when compinit has already run, and that directory may be system-owned. In common zsh setups the command either fails with a permission error or requires another compinit before completion works, contrary to the current-session claim.

Rule Used: When naming sections in documentation, use clear a... (source)

Learned From
Infisical/infisical#3652

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!


# fish
infisical completion fish | source
```

Run `infisical completion --help` for shell-specific installation instructions (e.g. persisting completions across sessions).

## Documentation

- **[CLI Overview](https://infisical.com/docs/cli/overview)** - Complete installation and setup guide
Expand Down
9 changes: 4 additions & 5 deletions packages/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,10 @@ var (
)

var RootCmd = &cobra.Command{
Use: "infisical",
Short: "Infisical CLI is used to inject environment variables into any process",
Long: `Infisical is a simple, end-to-end encrypted service that enables teams to sync and manage their environment variables across their development life cycle.`,
CompletionOptions: cobra.CompletionOptions{HiddenDefaultCmd: true},
Version: util.CLI_VERSION,
Use: "infisical",
Short: "Infisical CLI is used to inject environment variables into any process",
Long: `Infisical is a simple, end-to-end encrypted service that enables teams to sync and manage their environment variables across their development life cycle.`,
Version: util.CLI_VERSION,
}

// rootCmdStderrWriter is a writer wrapper that dynamically reads from RootCmd.ErrOrStderr()
Expand Down
61 changes: 61 additions & 0 deletions packages/cmd/root_completion_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package cmd

import (
"bytes"
"strings"
"testing"
)

// TestCompletionCommandIsDiscoverable ensures the built-in `completion` command
// is listed in `--help` output rather than hidden, since the underlying
// generator works correctly but was previously undiscoverable.
func TestCompletionCommandIsDiscoverable(t *testing.T) {
RootCmd.InitDefaultCompletionCmd()

cmd, _, err := RootCmd.Find([]string{"completion"})
if err != nil {
t.Fatalf("expected to find a 'completion' subcommand, got error: %v", err)
}
if cmd.Hidden {
t.Errorf("expected 'completion' command to be visible, but it is hidden")
}

t.Cleanup(func() {
RootCmd.SetOut(nil)
RootCmd.SetArgs(nil)
})

var out bytes.Buffer
RootCmd.SetOut(&out)
RootCmd.SetArgs([]string{"--help"})
if err := RootCmd.Execute(); err != nil {
t.Fatalf("unexpected error running --help: %v", err)
}

if !strings.Contains(out.String(), "completion") {
t.Errorf("expected 'completion' to appear in --help output, got:\n%s", out.String())
}
}

// TestCompletionGeneratesForAllShells verifies each supported shell produces
// a non-empty completion script without error.
func TestCompletionGeneratesForAllShells(t *testing.T) {
generators := map[string]func(*bytes.Buffer) error{
"bash": func(buf *bytes.Buffer) error { return RootCmd.GenBashCompletionV2(buf, true) },
"zsh": func(buf *bytes.Buffer) error { return RootCmd.GenZshCompletion(buf) },
"fish": func(buf *bytes.Buffer) error { return RootCmd.GenFishCompletion(buf, true) },
"powershell": func(buf *bytes.Buffer) error { return RootCmd.GenPowerShellCompletionWithDesc(buf) },
}

for shell, generate := range generators {
t.Run(shell, func(t *testing.T) {
var out bytes.Buffer
if err := generate(&out); err != nil {
t.Fatalf("expected no error generating %s completion, got: %v", shell, err)
}
if out.Len() == 0 {
t.Errorf("expected non-empty %s completion script", shell)
}
})
}
}