about summary refs log tree commit diff stats
path: root/pkgs/by-name/au/aumo
diff options
context:
space:
mode:
Diffstat (limited to '')
-rwxr-xr-xpkgs/by-name/au/aumo/aumo.sh47
-rw-r--r--pkgs/by-name/au/aumo/package.nix18
2 files changed, 53 insertions, 12 deletions
diff --git a/pkgs/by-name/au/aumo/aumo.sh b/pkgs/by-name/au/aumo/aumo.sh
index 84d39deb..991f257c 100755
--- a/pkgs/by-name/au/aumo/aumo.sh
+++ b/pkgs/by-name/au/aumo/aumo.sh
@@ -1,28 +1,63 @@
 #! /usr/bin/env dash
 
-# shellcheck source=/dev/null
-SHELL_LIBRARY_VERSION="2.1.2" . %SHELL_LIBRARY_PATH
+NAME="aumo"
+
+error() {
+    printf "\033[1;91m==> ERROR:\033[0m \033[1;93m%s\033[0m\n" "$*" >&2
+}
+
+die() {
+    error "$1"
+    exit "${2-1}"
+}
+
+usage() {
+    echo "Usage: $NAME mount|unmount"
+}
+
+get_mounted_labels() {
+    findmnt --output label --json | jq '.filesystems | map(.label) | sort | unique | map(select(. != null))'
+}
+get_unmounted_labels() {
+    first=true
+
+    find /dev/disk/by-label -printf "%P\n" | while read -r label; do
+        if ! get_mounted_labels | jq 'join("\n")' --raw-output | grep "$label" --quiet; then
+            if [ "$first" = "true" ]; then
+                first=false
+            else
+                printf "|"
+            fi
+            printf "%s" "$label"
+        fi
+    done
+}
 
 unmounting() {
-    disk_name="$(find /dev/disk/by-label -type l -printf "%P|" | rofi -sep "|" -dmenu -p "Select disk to mount")"
+    disk_name="$(get_mounted_labels | jq 'join("|")' --join-output | rofi -sep "|" -dmenu -p "Select disk to unmount")"
 
     udisksctl unmount --block-device "/dev/disk/by-label/$disk_name"
 }
 
 mounting() {
-    disk_name="$(find /dev/disk/by-label -type l -printf "%P|" | rofi -sep "|" -dmenu -p "Select disk to mount")"
+    disk_name="$(get_unmounted_labels | rofi -sep "|" -dmenu -p "Select disk to mount")"
 
     udisksctl mount --block-device "/dev/disk/by-label/$disk_name"
 }
 
-case "$1" in
+case "${1-unset}" in
 "mount")
     mounting
     ;;
 "unmount" | "umount")
     unmounting
     ;;
+"unset")
+    usage
+    die "You need to provide one argument."
+    ;;
 *)
-    die "Usage: $NAME mount|unmount"
+    usage
+    die "Unknown command: '$1'"
     ;;
 esac
diff --git a/pkgs/by-name/au/aumo/package.nix b/pkgs/by-name/au/aumo/package.nix
index 20054bb5..5ced60dc 100644
--- a/pkgs/by-name/au/aumo/package.nix
+++ b/pkgs/by-name/au/aumo/package.nix
@@ -1,17 +1,23 @@
 {
-  sysLib,
+  writeShellApplication,
+  # Dependencies
   udisks,
   findutils,
   rofi,
+  jq,
+  gnugrep,
+  util-linux,
 }:
-sysLib.writeShellScript {
+writeShellApplication {
   name = "aumo";
-  src = ./aumo.sh;
-  generateCompletions = false;
-  keepPath = false;
-  dependencies = [
+  text = builtins.readFile ./aumo.sh;
+  inheritPath = false;
+  runtimeInputs = [
     udisks
     findutils
     rofi
+    jq
+    gnugrep
+    util-linux # for findmnt
   ];
 }