diff --git a/nix/overlays/checked-shell-script/checked-shell-script.nix b/nix/overlays/checked-shell-script/checked-shell-script.nix index 5deb12749c9..942659d6f76 100644 --- a/nix/overlays/checked-shell-script/checked-shell-script.nix +++ b/nix/overlays/checked-shell-script/checked-shell-script.nix @@ -13,18 +13,17 @@ { name , docs , args ? [ ] -, addCommandCompletion ? false +, positionalCompletion ? "" , inRootDir ? false , redirectTixFiles ? true , withEnv ? null , 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 @@ -35,7 +34,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() @@ -63,8 +62,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 0d480d2c736..f5afe0e1634 100644 --- a/nix/tools/devtools.nix +++ b/nix/tools/devtools.nix @@ -28,7 +28,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 dcacfa9abca..7c52c653667 100644 --- a/nix/tools/loadtest.nix +++ b/nix/tools/loadtest.nix @@ -55,9 +55,12 @@ let ''; loadtestAgainst = + let + name = "postgrest-loadtest-against"; + in checkedShellScript { - name = "postgrest-loadtest-against"; + inherit name; docs = '' Run the vegeta loadtest twice: @@ -68,6 +71,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 0aab76d9f43..bc1e28be1c6 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; withTmpDir = true; @@ -121,7 +121,7 @@ let "ARG_POSITIONAL_SINGLE([command], [Command to run])" "ARG_LEFTOVERS([command arguments])" ]; - addCommandCompletion = true; + positionalCompletion = "_command"; inRootDir = true; } (lib.concatStringsSep "\n\n" runners); @@ -130,9 +130,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 @@ -144,7 +147,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; } '' @@ -206,7 +216,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 bcd96002f6c..cdf760c2116 100644 --- a/shell.nix +++ b/shell.nix @@ -43,6 +43,7 @@ lib.overrideDerivation postgrest.env ( base.buildInputs ++ [ pkgs.cabal-install pkgs.cabal2nix + pkgs.git pkgs.postgresql postgrest.hsie.bin ] @@ -51,6 +52,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} ''