sane-which: follow nix wrappers

This commit is contained in:
2024-02-28 18:07:09 +00:00
parent 40e30cf2f8
commit b515127101
2 changed files with 37 additions and 15 deletions

View File

@@ -196,7 +196,7 @@ let
which = static-nix-shell.mkBash { which = static-nix-shell.mkBash {
pname = "sane-which"; pname = "sane-which";
srcRoot = ./src; srcRoot = ./src;
pkgs = [ "coreutils-full" "file" ]; pkgs = [ "coreutils-full" "file" "gnugrep" ];
}; };
wipe = static-nix-shell.mkBash { wipe = static-nix-shell.mkBash {
pname = "sane-wipe"; pname = "sane-wipe";

View File

@@ -1,25 +1,47 @@
#!/usr/bin/env nix-shell #!/usr/bin/env nix-shell
#!nix-shell -i bash -p coreutils-full -p file #!nix-shell -i bash -p coreutils-full -p file -p gnugrep
# traces a PATH lookup by printing the source, resolution, and any symlinks traversed # traces a PATH lookup by printing the source, resolution, and any symlinks traversed
# finally, prints the content of the file # finally, prints the content of the file
echo $1
v=$(which $1) cur="$1"
# this probably doesn't handle paths with spaces next="$(which "$cur")"
while [ "$(readlink $v || echo $v)" != "$v" ]
do getSymlinked() {
# TODO: this doesn't handle relative symlinks # test if $cur is a symlink
echo '->' "$v" # TODO: handle relative symlinks too!
v=$(readlink "$v") local next_
next_="$(readlink "$cur")"
[ "$?" -eq 0 ] && echo "$next_"
}
getWrapped() {
# test if $cur is a wrapper around a patch matching $1 template
local dir="$(dirname "$cur")"
local base="$(basename "$cur")"
local wrapped="$(printf "$1" "$dir" "$base")"
[ -e "$wrapped" ] && grep -q "$wrapped" "$cur" && echo "$wrapped"
}
getNext() {
getSymlinked \
|| getWrapped "%s/.sandboxed/%s" \
|| getWrapped "%s/.%s-wrapped"
}
echo "$cur"
while [ -n "$next" ]; do
cur="$next"
echo "-> $cur"
next="$(getNext "$cur")"
done done
echo
echo '->' "$v" case $(file --brief --mime "$cur") in
echo ''
case $(file --brief --mime "$v") in
(*text*) (*text*)
cat "$v" cat "$cur"
;; ;;
(*) (*)
echo $(file "$v") echo $(file "$cur")
;; ;;
esac esac