From 8ce34ba4288395ea1544e263090906d086b22a6e Mon Sep 17 00:00:00 2001 From: Wolfgang Walther Date: Sun, 18 Apr 2021 22:16:13 +0200 Subject: [PATCH] nix(feat): Add bash completion for git references --- .../checked-shell-script.nix | 15 +++++++------- nix/tools/devTools.nix | 2 +- nix/tools/loadtest.nix | 11 +++++++++- nix/tools/withTools.nix | 20 ++++++++++++++----- shell.nix | 2 ++ 5 files changed, 35 insertions(+), 15 deletions(-) diff --git a/nix/overlays/checked-shell-script/checked-shell-script.nix b/nix/overlays/checked-shell-script/checked-shell-script.nix index cf391e9cd1d..b9665acf7b6 100644 --- a/nix/overlays/checked-shell-script/checked-shell-script.nix +++ b/nix/overlays/checked-shell-script/checked-shell-script.nix @@ -14,7 +14,7 @@ { name , docs , args ? [ ] -, addCommandCompletion ? false +, positionalCompletion ? "" , inRootDir ? false , redirectTixFiles ? true , withEnv ? null @@ -22,11 +22,10 @@ , withTmpDir ? false }: text: let + # square brackets are a pain to escape - if even possible. just don't use them... + escape = str: builtins.replaceStrings [ "\n" ] [ " \\n" ] str; + argsTemplate = - let - # square brackets are a pain to escape - if even possible. just don't use them... - escapedDocs = builtins.replaceStrings [ "\n" ] [ " \\n" ] docs; - in writeTextFile { inherit name; destination = "/${name}.m4"; # destination is needed to have the proper basename for completion @@ -37,7 +36,7 @@ let # stripping the /nix/store/... path for nicer display BASH_ARGV0="$(basename "$0")" - # ARG_HELP([${name}], [${escapedDocs}]) + # ARG_HELP([${name}], [${escape docs}]) ${lib.strings.concatMapStrings (arg: "# " + arg) args} # ARG_POSITIONAL_DOUBLEDASH() # ARG_DEFAULTS_POS() @@ -65,8 +64,8 @@ let ${argbash}/bin/argbash --type completion --strip all ${argsTemplate}/${name}.m4 > $out '' - + lib.optionalString addCommandCompletion '' - sed 's/COMPREPLY.*compgen -o bashdefault .*$/_command/' -i $out + + lib.optionalString (positionalCompletion != "") '' + sed 's#COMPREPLY.*compgen -o bashdefault .*$#${escape positionalCompletion}#' -i $out '' ); diff --git a/nix/tools/devTools.nix b/nix/tools/devTools.nix index 815c0c9b240..5e44858c993 100644 --- a/nix/tools/devTools.nix +++ b/nix/tools/devTools.nix @@ -29,7 +29,7 @@ let "ARG_POSITIONAL_SINGLE([command], [Command to run])" "ARG_LEFTOVERS([command arguments])" ]; - addCommandCompletion = true; + positionalCompletion = "_command"; redirectTixFiles = false; # will be done by sub-command inRootDir = true; } diff --git a/nix/tools/loadtest.nix b/nix/tools/loadtest.nix index d909b8b037c..e9eee7e806b 100644 --- a/nix/tools/loadtest.nix +++ b/nix/tools/loadtest.nix @@ -61,9 +61,12 @@ let ''; loadtestAgainst = + let + name = "postgrest-loadtest-against"; + in checkedShellScript { - name = "postgrest-loadtest-against"; + inherit name; docs = '' Run the vegeta loadtest twice: @@ -74,6 +77,12 @@ let "ARG_POSITIONAL_SINGLE([target], [Commit-ish reference to compare with])" "ARG_LEFTOVERS([additional vegeta arguments])" ]; + positionalCompletion = + '' + if test "$prev" == "${name}"; then + __gitcomp_nl "$(__git_refs)" + fi + ''; inRootDir = true; } '' diff --git a/nix/tools/withTools.nix b/nix/tools/withTools.nix index d801604edbf..51d5a791d6c 100644 --- a/nix/tools/withTools.nix +++ b/nix/tools/withTools.nix @@ -27,7 +27,7 @@ let "ARG_USE_ENV([PGRST_DB_SCHEMAS], [test], [Schema to expose])" "ARG_USE_ENV([PGRST_DB_ANON_ROLE], [postgrest_test_anonymous], [Anonymous PG role])" ]; - addCommandCompletion = true; + positionalCompletion = "_command"; inRootDir = true; redirectTixFiles = false; withPath = [ postgresql ]; @@ -118,7 +118,7 @@ let "ARG_POSITIONAL_SINGLE([command], [Command to run])" "ARG_LEFTOVERS([command arguments])" ]; - addCommandCompletion = true; + positionalCompletion = "_command"; inRootDir = true; } (lib.concatStringsSep "\n\n" runners); @@ -127,9 +127,12 @@ let withVersions = builtins.map withTmpDb postgresqlVersions; withGit = + let + name = "postgrest-with-git"; + in checkedShellScript { - name = "postgrest-with-git"; + inherit name; docs = '' Create a new worktree of the postgrest repo in a temporary directory and @@ -141,7 +144,14 @@ let "ARG_POSITIONAL_SINGLE([command], [Command to run])" "ARG_LEFTOVERS([command arguments])" ]; - addCommandCompletion = true; # TODO: first positional argument needs git commit completion + positionalCompletion = + '' + if test "$prev" == "${name}"; then + __gitcomp_nl "$(__git_refs)" + else + _command_offset 2 + fi + ''; inRootDir = true; } '' @@ -227,7 +237,7 @@ let "ARG_POSITIONAL_SINGLE([command], [Command to run])" "ARG_LEFTOVERS([command arguments])" ]; - addCommandCompletion = true; + positionalCompletion = "_command"; inRootDir = true; withEnv = postgrest.env; withTmpDir = true; diff --git a/shell.nix b/shell.nix index 6e6fb5f971d..95fe15b7f01 100644 --- a/shell.nix +++ b/shell.nix @@ -38,6 +38,7 @@ lib.overrideDerivation postgrest.env ( base.buildInputs ++ [ pkgs.cabal-install pkgs.cabal2nix + pkgs.git pkgs.postgresql postgrest.hsie.bin ] @@ -46,6 +47,7 @@ lib.overrideDerivation postgrest.env ( shellHook = '' source ${pkgs.bashCompletion}/etc/profile.d/bash_completion.sh + source ${pkgs.git}/share/git/contrib/completion/git-completion.bash source ${postgrest.hsie.bashCompletion} ''