61 lines
1.7 KiB
Rust
61 lines
1.7 KiB
Rust
|
use super::Cmd;
|
||
|
use clap::{Arg, ArgAction, Command};
|
||
|
use std::{fs, io};
|
||
|
|
||
|
#[derive(Debug, Default)]
|
||
|
pub struct Link;
|
||
|
|
||
|
impl Cmd for Link {
|
||
|
fn name(&self) -> &str {
|
||
|
"link"
|
||
|
}
|
||
|
|
||
|
fn cli(&self) -> clap::Command {
|
||
|
Command::new("link")
|
||
|
.about("call the link function to create a link to a file")
|
||
|
.author("Nathan Fisher")
|
||
|
.version(env!("CARGO_PKG_VERSION"))
|
||
|
.args([
|
||
|
Arg::new("file1").required(true).index(1),
|
||
|
Arg::new("file2").required(true).index(2),
|
||
|
Arg::new("verbose")
|
||
|
.short('v')
|
||
|
.long("verbose")
|
||
|
.action(ArgAction::SetTrue),
|
||
|
])
|
||
|
}
|
||
|
|
||
|
fn run(&self, matches: Option<&clap::ArgMatches>) -> Result<(), Box<dyn std::error::Error>> {
|
||
|
let Some(matches) = matches else {
|
||
|
return Err(Box::new(io::Error::new(io::ErrorKind::Other, "no input")));
|
||
|
};
|
||
|
let f1 = match matches.get_one::<String>("file1") {
|
||
|
Some(s) => s,
|
||
|
None => {
|
||
|
return Err(Box::new(io::Error::new(
|
||
|
io::ErrorKind::Other,
|
||
|
"missing file 1",
|
||
|
)))
|
||
|
}
|
||
|
};
|
||
|
let f2 = match matches.get_one::<String>("file2") {
|
||
|
Some(s) => s,
|
||
|
None => {
|
||
|
return Err(Box::new(io::Error::new(
|
||
|
io::ErrorKind::Other,
|
||
|
"missing file 2",
|
||
|
)))
|
||
|
}
|
||
|
};
|
||
|
fs::hard_link(f1, f2)?;
|
||
|
if matches.get_flag("verbose") {
|
||
|
println!("{f2} -> {f1}");
|
||
|
}
|
||
|
Ok(())
|
||
|
}
|
||
|
|
||
|
fn path(&self) -> Option<crate::Path> {
|
||
|
Some(crate::Path::UsrBin)
|
||
|
}
|
||
|
}
|