mirror of
https://github.com/NotAShelf/stash.git
synced 2026-04-13 14:33:47 +00:00
stash: utilize clap for multicall functionality; simplify CLI handler
Signed-off-by: NotAShelf <raf@notashelf.dev> Change-Id: I84d9f46bb9bba0e893aa4f99d6ff48f76a6a6964
This commit is contained in:
parent
43a3aae496
commit
d59ac77b9f
2 changed files with 26 additions and 65 deletions
53
src/main.rs
53
src/main.rs
|
|
@ -2,7 +2,6 @@ use std::{
|
|||
env,
|
||||
io::{self, IsTerminal},
|
||||
path::PathBuf,
|
||||
process,
|
||||
};
|
||||
|
||||
use clap::{CommandFactory, Parser, Subcommand};
|
||||
|
|
@ -129,14 +128,27 @@ fn report_error<T>(
|
|||
}
|
||||
|
||||
#[allow(clippy::too_many_lines)] // whatever
|
||||
fn main() {
|
||||
// Multicall dispatch: stash-copy, stash-paste, wl-copy, wl-paste
|
||||
if crate::multicall::multicall_dispatch() {
|
||||
// If handled, exit immediately
|
||||
std::process::exit(0);
|
||||
fn main() -> color_eyre::eyre::Result<()> {
|
||||
// Check if we're being called as a multicall binary
|
||||
let program_name = env::args().next().map(|s| {
|
||||
PathBuf::from(s)
|
||||
.file_name()
|
||||
.and_then(|name| name.to_str())
|
||||
.unwrap_or("stash")
|
||||
.to_string()
|
||||
});
|
||||
|
||||
if let Some(ref name) = program_name {
|
||||
if name == "wl-copy" || name == "stash-copy" {
|
||||
crate::multicall::wl_copy::wl_copy_main()?;
|
||||
return Ok(());
|
||||
} else if name == "wl-paste" || name == "stash-paste" {
|
||||
crate::multicall::wl_paste::wl_paste_main()?;
|
||||
return Ok(());
|
||||
}
|
||||
}
|
||||
|
||||
// If not multicall, proceed with normal CLI handling
|
||||
// Normal CLI handling
|
||||
smol::block_on(async {
|
||||
let cli = Cli::parse();
|
||||
env_logger::Builder::new()
|
||||
|
|
@ -151,24 +163,11 @@ fn main() {
|
|||
});
|
||||
|
||||
if let Some(parent) = db_path.parent() {
|
||||
if let Err(e) = std::fs::create_dir_all(parent) {
|
||||
log::error!("Failed to create database directory: {e}");
|
||||
process::exit(1);
|
||||
}
|
||||
std::fs::create_dir_all(parent)?;
|
||||
}
|
||||
|
||||
let conn = rusqlite::Connection::open(&db_path).unwrap_or_else(|e| {
|
||||
log::error!("Failed to open SQLite database: {e}");
|
||||
process::exit(1);
|
||||
});
|
||||
|
||||
let db = match db::SqliteClipboardDb::new(conn) {
|
||||
Ok(db) => db,
|
||||
Err(e) => {
|
||||
log::error!("Failed to initialize SQLite database: {e}");
|
||||
process::exit(1);
|
||||
},
|
||||
};
|
||||
let conn = rusqlite::Connection::open(&db_path)?;
|
||||
let db = db::SqliteClipboardDb::new(conn)?;
|
||||
|
||||
match cli.command {
|
||||
Some(Command::Store) => {
|
||||
|
|
@ -345,12 +344,12 @@ fn main() {
|
|||
&[],
|
||||
);
|
||||
},
|
||||
|
||||
None => {
|
||||
if let Err(e) = Cli::command().print_help() {
|
||||
log::error!("Failed to print help: {e}");
|
||||
}
|
||||
Cli::command().print_help()?;
|
||||
println!();
|
||||
},
|
||||
}
|
||||
});
|
||||
Ok(())
|
||||
})
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,41 +4,3 @@
|
|||
// https://github.com/YaLTeR/wl-clipboard-rs/blob/master/wl-clipboard-rs-tools/src/bin/wl_copy.rs
|
||||
pub mod wl_copy;
|
||||
pub mod wl_paste;
|
||||
|
||||
use std::env;
|
||||
|
||||
/// Extract the base name from argv[0].
|
||||
fn get_base(argv0: &str) -> &str {
|
||||
std::path::Path::new(argv0)
|
||||
.file_name()
|
||||
.and_then(|name| name.to_str())
|
||||
.unwrap_or("")
|
||||
}
|
||||
|
||||
/// Dispatch multicall binary logic based on `argv[0]`.
|
||||
/// Returns `true` if a multicall command was handled and the process should
|
||||
/// exit.
|
||||
pub fn multicall_dispatch() -> bool {
|
||||
let argv0 = env::args().next().unwrap_or_else(|| {
|
||||
log::warn!("unable to determine program name");
|
||||
String::new()
|
||||
});
|
||||
let base = get_base(&argv0);
|
||||
match base {
|
||||
"stash-copy" | "wl-copy" => {
|
||||
if let Err(e) = wl_copy::wl_copy_main() {
|
||||
log::error!("copy failed: {e}");
|
||||
std::process::exit(1);
|
||||
}
|
||||
true
|
||||
},
|
||||
"stash-paste" | "wl-paste" => {
|
||||
if let Err(e) = wl_paste::wl_paste_main() {
|
||||
log::error!("paste failed: {e}");
|
||||
std::process::exit(1);
|
||||
}
|
||||
true
|
||||
},
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue