aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--README.md7
-rw-r--r--nix/package.nix6
-rw-r--r--share/bash-completion/completions/enstate64
3 files changed, 75 insertions, 2 deletions
diff --git a/README.md b/README.md
index 3edbfc7..241ad31 100644
--- a/README.md
+++ b/README.md
@@ -35,8 +35,11 @@ The NixOS module is [nix/module.nix](nix/module.nix);
[default.nix](default.nix) has the package and the module's VM test
(`nix-build -A tests.vm`).
-Not yet: converters, derived slots and steps; shell completion, a sandbox for
-programs.
+Bash completion is
+[share/bash-completion/completions/enstate](share/bash-completion/completions/enstate),
+which the package installs.
+
+Not yet: converters, derived slots and steps; a sandbox for programs.
```sh
cargo build
diff --git a/nix/package.nix b/nix/package.nix
index ecaf41b..997d2a3 100644
--- a/nix/package.nix
+++ b/nix/package.nix
@@ -1,5 +1,6 @@
# enstate, the program (README.md).
{
+ installShellFiles,
lib,
runCommand,
rustPlatform,
@@ -21,6 +22,11 @@ rustPlatform.buildRustPackage {
cp -r ${../tests} $out/tests
'';
+ nativeBuildInputs = [ installShellFiles ];
+ postInstall = ''
+ installShellCompletion --bash ${../share/bash-completion/completions/enstate}
+ '';
+
# The tests' fake systemctl looks at the store's lock with flock.
nativeCheckInputs = [ util-linux ];
diff --git a/share/bash-completion/completions/enstate b/share/bash-completion/completions/enstate
new file mode 100644
index 0000000..25a8976
--- /dev/null
+++ b/share/bash-completion/completions/enstate
@@ -0,0 +1,64 @@
+# bash completion for enstate: a command until one is given, then the options it
+# takes (each once, but --config and --unset), and for PATH a submission (a
+# directory) or a source list (a file).
+
+_enstate() {
+ local cur=${COMP_WORDS[COMP_CWORD]} prev=${COMP_WORDS[COMP_CWORD-1]}
+ local commands="--plan --apply --stage --status --materialize --migrate --pack"
+ local settings="--config --unset --state-dir --run-dir --schema --unit --print-config"
+ local i w cmd= path= used=" "
+ # bash splits --option=VALUE into --option, =, VALUE
+ if [ "$prev" = = ] && [ "$COMP_CWORD" -ge 2 ]; then
+ prev=${COMP_WORDS[COMP_CWORD-2]}
+ elif [ "$cur" = = ]; then
+ cur=
+ fi
+ for ((i = 1; i < COMP_CWORD; i++)); do
+ w=${COMP_WORDS[i]}
+ case $w in
+ --plan | --apply | --stage | --status | --materialize | --migrate | --pack)
+ cmd=${w#--} ;;
+ --out | --config | --unset | --state-dir | --run-dir | --schema | --unit)
+ used="$used$w "
+ # its value, written apart or with =
+ if [ "${COMP_WORDS[i+1]}" = = ]; then i=$((i + 2)); else i=$((i + 1)); fi ;;
+ -*) used="$used$w " ;;
+ =) ;;
+ *) path=$w ;;
+ esac
+ done
+ case $prev in
+ --config | --schema) COMPREPLY=($(compgen -f -- "$cur")); return ;;
+ --state-dir | --run-dir | --out) COMPREPLY=($(compgen -d -- "$cur")); return ;;
+ --unset) COMPREPLY=($(compgen -W "state-dir run-dir schema unit" -- "$cur")); return ;;
+ --unit) COMPREPLY=(); return ;;
+ esac
+ local options
+ case $cmd in
+ "") options="$commands -h --help $settings" ;;
+ plan | apply | stage) options="--json $settings" ;;
+ status | materialize) options="--json $settings" ;;
+ migrate) options="--json --dry-run $settings" ;;
+ pack) options="--out $settings" ;;
+ esac
+ local o candidates=
+ for o in $options; do
+ case $o in
+ --config | --unset) ;;
+ *) case $used in *" $o "*) continue ;; esac ;;
+ esac
+ candidates="$candidates $o"
+ done
+ COMPREPLY=()
+ if [ -z "$path" ] && [[ $cur != -* ]]; then
+ case $cmd in
+ plan | apply | stage) COMPREPLY=($(compgen -d -- "$cur")) ;;
+ pack) COMPREPLY=($(compgen -f -- "$cur")) ;;
+ esac
+ fi
+ if [ ${#COMPREPLY[@]} -eq 0 ] || [[ $cur == -* ]]; then
+ COMPREPLY+=($(compgen -W "$candidates" -- "$cur"))
+ fi
+}
+
+complete -o filenames -F _enstate enstate