Skyler Grey | 4e23089 | 2024-02-13 22:58:46 +0000 | [diff] [blame] | 1 | #!/usr/bin/env bash |
| 2 | if [ "$1" = "" ] |
| 3 | then |
| 4 | read -ep "What command are you looking for?: " CMD |
| 5 | else |
| 6 | CMD=$1 |
| 7 | fi |
| 8 | |
| 9 | LOCATION=$(command -V "$CMD" 2>&1 | sed -rn "s/.* (.*)$/\1/p") |
| 10 | |
| 11 | if [ "$LOCATION" = "found" ] # Not found |
| 12 | then |
| 13 | echo "The command $CMD wasn't found" |
| 14 | exit 1 |
| 15 | fi |
| 16 | |
| 17 | if [ "$LOCATION" = "builtin" ] # Shell builtin |
| 18 | then |
| 19 | echo "The command $CMD is a shell builtin" |
| 20 | exit 0 |
| 21 | fi |
| 22 | |
| 23 | RESOLVED_LOCATION=$(readlink -f "$LOCATION") |
| 24 | |
| 25 | if [[ ! "$RESOLVED_LOCATION" =~ ^\/nix\/store\/.*-.*-.*$ ]]; |
| 26 | then |
| 27 | echo "The command $CMD is at $RESOLVED_LOCATION" |
| 28 | exit 0 |
| 29 | fi |
| 30 | |
| 31 | PACKAGE=$(echo "$RESOLVED_LOCATION" | sed -rn "s/\/nix\/store\/[^-]*-(.*)-[^-]*/\1/p") |
| 32 | |
| 33 | SEARCHED=$(nix search "nixpkgs#$PACKAGE" 2>&1) |
| 34 | |
| 35 | if [ "$SEARCHED" = "error: no results for the given search term(s)!" ] |
| 36 | then |
| 37 | echo "The command $CMD is at $RESOLVED_LOCATION" |
| 38 | exit 0 |
| 39 | fi |
| 40 | |
| 41 | echo "The command $CMD is from:" |
| 42 | echo "$SEARCHED" |
| 43 | echo "Which is available at $RESOLVED_LOCATION" |