2023-01-12 19:00:16 -05:00
|
|
|
use super::Cmd;
|
2023-02-05 23:50:59 -05:00
|
|
|
use shitbox::args;
|
2023-01-21 18:25:09 -05:00
|
|
|
use clap::{Arg, Command};
|
2023-01-12 19:00:16 -05:00
|
|
|
use std::{fs, io};
|
|
|
|
|
|
|
|
#[derive(Debug, Default)]
|
|
|
|
pub struct Link;
|
|
|
|
|
|
|
|
impl Cmd for 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),
|
2023-01-21 18:25:09 -05:00
|
|
|
args::verbose(),
|
2023-01-12 19:00:16 -05:00
|
|
|
])
|
|
|
|
}
|
|
|
|
|
2023-02-04 08:54:27 -05:00
|
|
|
fn run(&self, matches: &clap::ArgMatches) -> Result<(), Box<dyn std::error::Error>> {
|
2023-01-13 01:08:32 -05:00
|
|
|
let Some(f1) = matches.get_one::<String>("file1") else {
|
|
|
|
return Err(Box::new(io::Error::new(
|
|
|
|
io::ErrorKind::Other,
|
|
|
|
"missing file 1",
|
|
|
|
)))
|
2023-01-12 19:00:16 -05:00
|
|
|
};
|
2023-01-13 01:08:32 -05:00
|
|
|
let Some(f2) = matches.get_one::<String>("file2") else {
|
|
|
|
return Err(Box::new(io::Error::new(
|
|
|
|
io::ErrorKind::Other,
|
|
|
|
"missing file 2",
|
|
|
|
)))
|
2023-01-12 19:00:16 -05:00
|
|
|
};
|
|
|
|
fs::hard_link(f1, f2)?;
|
|
|
|
if matches.get_flag("verbose") {
|
|
|
|
println!("{f2} -> {f1}");
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2023-02-05 23:50:59 -05:00
|
|
|
fn path(&self) -> Option<shitbox::Path> {
|
|
|
|
Some(shitbox::Path::UsrBin)
|
2023-01-12 19:00:16 -05:00
|
|
|
}
|
|
|
|
}
|