Archived
feat(cli): add autocompletion command for shell completion scripts
Adds `barn autocompletion` command to generate shell completion scripts: - `barn autocompletion --bash` generates bash completion - `barn autocompletion --zsh` generates zsh completion (using bashcompinit) Usage: barn autocompletion --bash > /usr/local/etc/bash_completion.d/barn barn autocompletion --zsh > ~/.zsh/completions/_barn Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
e8ea91268a
commit
028770a2da
3 files changed
+167
-1
No files matched your search
@@ -0,0 +1,73 @@
|
||||
package com.samsonmedia.barn.cli;
|
||||
|
||||
import picocli.AutoComplete;
|
||||
import picocli.CommandLine;
|
||||
import picocli.CommandLine.Command;
|
||||
import picocli.CommandLine.Model.CommandSpec;
|
||||
import picocli.CommandLine.Option;
|
||||
import picocli.CommandLine.Spec;
|
||||
|
||||
/**
|
||||
* Command to generate shell autocompletion scripts.
|
||||
*
|
||||
* <p>Usage:
|
||||
* <pre>
|
||||
* # Generate and install bash completion
|
||||
* barn autocompletion --bash > /usr/local/etc/bash_completion.d/barn
|
||||
*
|
||||
* # Generate and install zsh completion
|
||||
* barn autocompletion --zsh > ~/.zsh/completions/_barn
|
||||
* </pre>
|
||||
*/
|
||||
@Command(
|
||||
name = "autocompletion",
|
||||
mixinStandardHelpOptions = true,
|
||||
description = "Generate shell autocompletion scripts"
|
||||
)
|
||||
public class AutocompletionCommand implements Runnable {
|
||||
|
||||
@Spec
|
||||
private CommandSpec spec;
|
||||
|
||||
@Option(names = {"--bash"}, description = "Generate bash completion script")
|
||||
private boolean bash;
|
||||
|
||||
@Option(names = {"--zsh"}, description = "Generate zsh completion script")
|
||||
private boolean zsh;
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
if (!bash && !zsh) {
|
||||
spec.commandLine().usage(spec.commandLine().getOut());
|
||||
return;
|
||||
}
|
||||
|
||||
// Get the root command
|
||||
CommandLine rootCmd = spec.commandLine().getParent();
|
||||
if (rootCmd == null) {
|
||||
rootCmd = spec.commandLine();
|
||||
}
|
||||
|
||||
String script = AutoComplete.bash(
|
||||
rootCmd.getCommandName(),
|
||||
rootCmd
|
||||
);
|
||||
|
||||
if (zsh) {
|
||||
// Wrap bash completion for zsh compatibility
|
||||
script = generateZshCompletion(rootCmd.getCommandName(), script);
|
||||
}
|
||||
|
||||
spec.commandLine().getOut().println(script);
|
||||
}
|
||||
|
||||
private String generateZshCompletion(String name, String bashScript) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append("#compdef ").append(name).append("\n\n");
|
||||
sb.append("# Zsh completion for ").append(name).append("\n");
|
||||
sb.append("# Generated by barn autocompletion --zsh\n\n");
|
||||
sb.append("autoload -U +X bashcompinit && bashcompinit\n\n");
|
||||
sb.append(bashScript);
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
||||
@@ -23,7 +23,8 @@ import picocli.CommandLine.Spec;
|
||||
KillCommand.class,
|
||||
CleanCommand.class,
|
||||
ServiceCommand.class,
|
||||
ConfigCommand.class
|
||||
ConfigCommand.class,
|
||||
AutocompletionCommand.class
|
||||
}
|
||||
)
|
||||
public class BarnCommand implements Runnable {
|
||||
|
||||
@@ -0,0 +1,92 @@
|
||||
package com.samsonmedia.barn.cli;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import java.io.PrintWriter;
|
||||
import java.io.StringWriter;
|
||||
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Nested;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import picocli.CommandLine;
|
||||
|
||||
/**
|
||||
* Tests for AutocompletionCommand.
|
||||
*/
|
||||
class AutocompletionCommandTest {
|
||||
|
||||
private CommandLine commandLine;
|
||||
private StringWriter out;
|
||||
private StringWriter err;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
commandLine = new CommandLine(new BarnCommand());
|
||||
out = new StringWriter();
|
||||
err = new StringWriter();
|
||||
commandLine.setOut(new PrintWriter(out));
|
||||
commandLine.setErr(new PrintWriter(err));
|
||||
}
|
||||
|
||||
@Nested
|
||||
class BashCompletion {
|
||||
|
||||
@Test
|
||||
void shouldGenerateBashScript() {
|
||||
int exitCode = commandLine.execute("autocompletion", "--bash");
|
||||
|
||||
assertThat(exitCode).isEqualTo(0);
|
||||
assertThat(out.toString()).contains("#!/usr/bin/env bash");
|
||||
assertThat(out.toString()).contains("barn Bash Completion");
|
||||
assertThat(out.toString()).contains("COMPREPLY");
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldIncludeSubcommands() {
|
||||
int exitCode = commandLine.execute("autocompletion", "--bash");
|
||||
|
||||
assertThat(exitCode).isEqualTo(0);
|
||||
String output = out.toString();
|
||||
assertThat(output).contains("run");
|
||||
assertThat(output).contains("status");
|
||||
assertThat(output).contains("describe");
|
||||
assertThat(output).contains("service");
|
||||
}
|
||||
}
|
||||
|
||||
@Nested
|
||||
class ZshCompletion {
|
||||
|
||||
@Test
|
||||
void shouldGenerateZshScript() {
|
||||
int exitCode = commandLine.execute("autocompletion", "--zsh");
|
||||
|
||||
assertThat(exitCode).isEqualTo(0);
|
||||
assertThat(out.toString()).contains("#compdef barn");
|
||||
assertThat(out.toString()).contains("bashcompinit");
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldIncludeBashCompletionForCompatibility() {
|
||||
int exitCode = commandLine.execute("autocompletion", "--zsh");
|
||||
|
||||
assertThat(exitCode).isEqualTo(0);
|
||||
assertThat(out.toString()).contains("COMPREPLY");
|
||||
}
|
||||
}
|
||||
|
||||
@Nested
|
||||
class NoShellSpecified {
|
||||
|
||||
@Test
|
||||
void shouldShowUsage() {
|
||||
int exitCode = commandLine.execute("autocompletion");
|
||||
|
||||
assertThat(exitCode).isEqualTo(0);
|
||||
assertThat(out.toString()).contains("Usage:");
|
||||
assertThat(out.toString()).contains("--bash");
|
||||
assertThat(out.toString()).contains("--zsh");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user