Skip to main content

uu_link/
link.rs

1// This file is part of the uutils coreutils package.
2//
3// For the full copyright and license information, please view the LICENSE
4// file that was distributed with this source code.
5
6use clap::builder::ValueParser;
7use clap::{Arg, Command};
8use std::ffi::OsString;
9use std::fs::hard_link;
10use std::path::Path;
11use uucore::display::Quotable;
12use uucore::error::{FromIo, UResult};
13use uucore::format_usage;
14use uucore::translate;
15
16pub mod options {
17    pub static FILES: &str = "FILES";
18}
19
20#[uucore::main(no_signals)]
21pub fn uumain(args: impl uucore::Args) -> UResult<()> {
22    let matches = uucore::clap_localization::handle_clap_result(uu_app(), args)?;
23    let files: Vec<_> = matches
24        .get_many::<OsString>(options::FILES)
25        .unwrap_or_default()
26        .collect();
27
28    let old = Path::new(files[0]);
29    let new = Path::new(files[1]);
30
31    hard_link(old, new).map_err_context(
32        || translate!("link-error-cannot-create-link", "new" => new.quote(), "old" => old.quote()),
33    )
34}
35
36pub fn uu_app() -> Command {
37    Command::new(uucore::util_name())
38        .version(uucore::crate_version!())
39        .help_template(uucore::localized_help_template(uucore::util_name()))
40        .about(translate!("link-about"))
41        .override_usage(format_usage(&translate!("link-usage")))
42        .infer_long_args(true)
43        .arg(
44            Arg::new(options::FILES)
45                .hide(true)
46                .required(true)
47                .num_args(2)
48                .value_hint(clap::ValueHint::AnyPath)
49                .value_parser(ValueParser::os_string()),
50        )
51}