-
-
Notifications
You must be signed in to change notification settings - Fork 12
feat(cli): add completion command #1427
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
MunifTanjim
wants to merge
1
commit into
getsentry:main
Choose a base branch
from
MunifTanjim:feat/cli-completion-command
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| /** | ||
| * sentry cli completion <shell> | ||
| * | ||
| * Print the shell completion script to stdout. Unlike `cli setup`, which writes | ||
| * completion files to disk, this command lets users and package managers install | ||
| * completions however they like. | ||
| * | ||
| * Usage: | ||
| * sentry cli completion zsh > ~/.local/share/zsh/site-functions/_sentry | ||
| * eval "$(sentry cli completion bash)" | ||
| * | ||
| * When no shell is given, the current shell is detected from $SHELL. | ||
| */ | ||
|
|
||
| import type { SentryContext } from "../../context.js"; | ||
| import { buildCommand } from "../../lib/command.js"; | ||
| import { getCompletionScript } from "../../lib/completions.js"; | ||
| import { ValidationError } from "../../lib/errors.js"; | ||
| import { CommandOutput } from "../../lib/formatters/output.js"; | ||
| import { detectShellType } from "../../lib/shell.js"; | ||
|
|
||
| export const completionCommand = buildCommand({ | ||
| auth: false, | ||
| docs: { | ||
| brief: "Print the shell completion script", | ||
| fullDescription: | ||
| "Print the shell completion script for the given shell to stdout.\n\n" + | ||
| "Supported shells: bash, zsh, fish.\n" + | ||
| "When no shell is given, it is detected from the $SHELL environment variable.\n\n" + | ||
| "Examples:\n" + | ||
| " sentry cli completion zsh > ~/.local/share/zsh/site-functions/_sentry\n" + | ||
| ' eval "$(sentry cli completion bash)"', | ||
| }, | ||
| output: { | ||
| human: (script: string) => script, | ||
| }, | ||
| parameters: { | ||
| flags: {}, | ||
| positional: { | ||
| kind: "tuple", | ||
| parameters: [ | ||
| { | ||
| placeholder: "shell", | ||
| brief: "Shell to generate completions for (bash, zsh, or fish)", | ||
| parse: String, | ||
| optional: true, | ||
| }, | ||
| ], | ||
| }, | ||
| }, | ||
| // biome-ignore lint/suspicious/useAwait: Stricli requires AsyncGenerator but script generation is synchronous | ||
| async *func( | ||
| this: SentryContext, | ||
| _flags: Record<string, never>, | ||
| shell?: string | ||
| ) { | ||
| const shellType = shell | ||
| ? detectShellType(shell) | ||
| : detectShellType(this.env.SHELL); | ||
|
|
||
| const script = getCompletionScript(shellType); | ||
| if (script === null) { | ||
| throw new ValidationError( | ||
| `Unsupported shell: ${shell || shellType}. Supported shells: bash, zsh, fish`, | ||
| "shell" | ||
| ); | ||
|
sentry[bot] marked this conversation as resolved.
|
||
| } | ||
|
|
||
| yield new CommandOutput<string>(script); | ||
| }, | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| /** | ||
| * Tests for `sentry cli completion` command. | ||
| * | ||
| * Verifies each supported shell prints a script, an unsupported shell errors | ||
| * without emitting a script, and the shell is auto-detected from $SHELL. | ||
| */ | ||
|
|
||
| import { run } from "@stricli/core"; | ||
| import { describe, expect, test } from "vitest"; | ||
| import { app } from "../../../src/app.js"; | ||
| import type { SentryContext } from "../../../src/context.js"; | ||
|
|
||
| /** | ||
| * Run the completion command via Stricli's `run()` and capture stdout. | ||
| * | ||
| * @param args - Args after `cli completion` (e.g. `["bash"]`) | ||
| * @param shellEnv - Value to inject as $SHELL for auto-detection tests | ||
| */ | ||
| async function runCompletion( | ||
| args: string[], | ||
| shellEnv?: string | ||
| ): Promise<{ output: string; exitCode: number | undefined }> { | ||
| let output = ""; | ||
| const env = { ...process.env, SHELL: shellEnv }; | ||
| const mockContext: SentryContext = { | ||
| process: { | ||
| ...process, | ||
| exitCode: undefined, | ||
| } as typeof process, | ||
| env, | ||
| cwd: process.cwd(), | ||
| homeDir: "/tmp", | ||
| configDir: "/tmp", | ||
| stdout: { | ||
| write(data: string | Uint8Array) { | ||
| output += | ||
| typeof data === "string" ? data : new TextDecoder().decode(data); | ||
| return true; | ||
| }, | ||
| }, | ||
| stderr: { | ||
| write() { | ||
| return true; | ||
| }, | ||
| }, | ||
| stdin: process.stdin, | ||
| }; | ||
|
|
||
| await run(app, ["cli", "completion", ...args], mockContext); | ||
| return { output, exitCode: mockContext.process.exitCode }; | ||
| } | ||
|
|
||
| describe("sentry cli completion", () => { | ||
| test("bash prints a bash completion script", async () => { | ||
| const { output, exitCode } = await runCompletion(["bash"]); | ||
| expect(output).toContain("complete -F _sentry_completions sentry"); | ||
| expect(exitCode ?? 0).toBe(0); | ||
| }); | ||
|
|
||
| test("zsh prints a zsh completion script", async () => { | ||
| const { output, exitCode } = await runCompletion(["zsh"]); | ||
| expect(output).toContain("#compdef sentry"); | ||
| expect(exitCode ?? 0).toBe(0); | ||
| }); | ||
|
|
||
| test("fish prints a fish completion script", async () => { | ||
| const { output, exitCode } = await runCompletion(["fish"]); | ||
| expect(output).toContain("complete -c sentry"); | ||
| expect(exitCode ?? 0).toBe(0); | ||
| }); | ||
|
|
||
| test("unsupported shell errors and prints no script", async () => { | ||
| const { output, exitCode } = await runCompletion(["nonsense"]); | ||
| expect(output).toBe(""); | ||
| expect(exitCode).toBeGreaterThan(0); | ||
| }); | ||
|
|
||
| test("auto-detects the shell from $SHELL when no arg is given", async () => { | ||
| const { output, exitCode } = await runCompletion([], "/bin/zsh"); | ||
| expect(output).toContain("#compdef sentry"); | ||
| expect(exitCode ?? 0).toBe(0); | ||
| }); | ||
| }); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.