summaryrefslogtreecommitdiffstats
path: root/scripts/removablectl
diff options
context:
space:
mode:
authorJokler <jokler@protonmail.com>2020-06-22 02:15:52 +0200
committerJokler <jokler@protonmail.com>2020-06-22 02:21:55 +0200
commit37ccc5b7f8eaac129cfa5a229ff2c4e16d6fc929 (patch)
treeff1446fe9b369112bdd41aaa02bc99343a9ecdf8 /scripts/removablectl
parent1a357dd0a71d8e6d73cc8af4c93d3005576aff36 (diff)
downloaddotfiles-37ccc5b7f8eaac129cfa5a229ff2c4e16d6fc929.tar.gz
dotfiles-37ccc5b7f8eaac129cfa5a229ff2c4e16d6fc929.zip
Add new scripts and update old ones
Diffstat (limited to 'scripts/removablectl')
-rwxr-xr-xscripts/removablectl89
1 files changed, 89 insertions, 0 deletions
diff --git a/scripts/removablectl b/scripts/removablectl
new file mode 100755
index 0000000..926067f
--- /dev/null
+++ b/scripts/removablectl
@@ -0,0 +1,89 @@
+#!/usr/bin/env scriptisto
+
+// scriptisto-begin
+// script_src: src/main.rs
+// build_cmd: cargo build --release && strip ./target/release/script
+// target_bin: ./target/release/script
+// files:
+// - path: Cargo.toml
+// content: |
+// package = { name = "script", version = "0.1.0", edition = "2018"}
+// [dependencies]
+// structopt="*"
+// cmd_lib = "0.7.8"
+// atty = "0.2"
+// scriptisto-end
+
+use structopt::StructOpt;
+use cmd_lib::*;
+
+#[derive(Debug, StructOpt)]
+#[structopt(name = "removablectl", about = "Manages removable drives.")]
+enum Opt {
+ /// Mount a drive
+ Mount,
+ /// Unmount a drive
+ Unmount,
+}
+
+fn main() -> CmdResult {
+ let opt = Opt::from_args();
+ match opt {
+ Opt::Mount => mount(),
+ Opt::Unmount => unmount(),
+ }
+}
+
+fn mount () -> CmdResult {
+ let unmounted = Process::new("lsblk --noheadings --raw -o NAME,MOUNTPOINT")
+ .pipe("awk '$1~/[[:digit:]]/ && $2 == \"\"'")
+ .wait::<FunResult>()?;
+ let filtered = unmounted
+ .split_whitespace()
+ .filter(|s| !s.starts_with("sda"))
+ .filter(|s| !s.starts_with("loop"))
+ .map(|s| format!("/dev/{}", s))
+ .collect::<Vec<_>>();
+
+ if filtered.is_empty() {
+ die!("No unmounted drives were found");
+ }
+
+ let selected = chooser(filtered)?;
+ run_cmd!("udisksctl mount -b {}", selected)
+}
+
+fn unmount () -> CmdResult {
+ let mounted = Process::new("cat /etc/mtab")
+ .pipe("grep ^/")
+ .pipe("awk '{print $1}'")
+ .wait::<FunResult>()?;
+ let filtered = mounted
+ .split_whitespace()
+ .filter(|s| !s.starts_with("/dev/sda"))
+ .map(|s| s.to_owned())
+ .collect::<Vec<_>>();
+
+ if filtered.is_empty() {
+ die!("No mounted drives were found");
+ }
+
+ let selected = chooser(filtered)?;
+ run_cmd!("udisksctl unmount -b {}", selected)
+}
+
+fn chooser(list: Vec<String>) -> FunResult {
+ let selected = if atty::is(atty::Stream::Stdout) {
+ run_fun!("enquirer select -m \"Choose a drive\" {}", list.join(" "))?
+ } else {
+ run_fun!("echo -e \"{}\" | rofi -dmenu -p \"Choose a drive\"", list.join("\\n"))?
+ };
+
+ if selected.is_empty() {
+ die!("Nothing was selected");
+ }
+
+ Ok(selected)
+}
+
+// vim: filetype=rust