Skip to content

Commit

Permalink
nix(feat): Add bash completion for git references
Browse files Browse the repository at this point in the history
  • Loading branch information
wolfgangwalther committed Nov 27, 2021
1 parent 32ff9df commit 5a8d9b1
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 15 deletions.
15 changes: 7 additions & 8 deletions nix/overlays/checked-shell-script/checked-shell-script.nix
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,18 @@
{ name
, docs
, args ? [ ]
, addCommandCompletion ? false
, positionalCompletion ? ""
, inRootDir ? false
, redirectTixFiles ? true
, withEnv ? null
, withPath ? [ ]
, withTmpDir ? false
}: text:
let
# square brackets are a pain to escape - if even possible. just don't use them...
escape = builtins.replaceStrings [ "\n" ] [ " \\n" ];

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
Expand All @@ -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()
Expand Down Expand Up @@ -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
''
);

Expand Down
2 changes: 1 addition & 1 deletion nix/tools/devTools.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
11 changes: 10 additions & 1 deletion nix/tools/loadtest.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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;
}
''
Expand Down
20 changes: 15 additions & 5 deletions nix/tools/withTools.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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 ];
Expand Down Expand Up @@ -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);
Expand All @@ -129,9 +129,12 @@ let
withPg = builtins.head withPgVersions;

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
Expand All @@ -143,7 +146,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;
}
''
Expand Down Expand Up @@ -229,7 +239,7 @@ let
"ARG_POSITIONAL_SINGLE([command], [Command to run])"
"ARG_LEFTOVERS([command arguments])"
];
addCommandCompletion = true;
positionalCompletion = "_command";
inRootDir = true;
withEnv = postgrest.env;
withTmpDir = true;
Expand Down
2 changes: 2 additions & 0 deletions shell.nix
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ lib.overrideDerivation postgrest.env (
base.buildInputs ++ [
pkgs.cabal-install
pkgs.cabal2nix
pkgs.git
pkgs.postgresql
postgrest.hsie.bin
]
Expand All @@ -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}
''
Expand Down

0 comments on commit 5a8d9b1

Please sign in to comment.