-
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
multicall.rs
60 lines (56 loc) · 1.61 KB
/
multicall.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
use std::{
env::args_os,
fs::{hard_link, read_link},
path::{Path, PathBuf},
process::exit,
};
use clap::{Clap, IntoApp};
#[derive(Clap, Debug)]
enum Applets {
True,
False,
Install {
#[clap(default_value = "/usr/local/bin")]
path: PathBuf,
},
}
#[derive(Clap, Debug)]
#[clap(setting = clap::AppSettings::Multicall)]
struct Args {
#[clap(subcommand)]
applet: Applets,
}
fn main() {
match Args::parse().applet {
Applets::True => exit(0),
Applets::False => exit(1),
Applets::Install { path } => {
let app = Args::into_app();
let applets: Vec<_> = app.get_subcommands().map(|c| c.get_name()).collect();
let exec_path = read_link("/proc/self/exe")
.ok()
.or_else(|| {
args_os().next().and_then(|s| {
let p: &Path = s.as_ref();
if p.is_absolute() {
Some(PathBuf::from(s))
} else {
None
}
})
})
.expect(
"Should be able to read /proc/self/exe or argv0 should be present and absolute",
);
let mut dest = PathBuf::from(path);
for applet in applets {
if applet == "install" {
continue;
}
dest.push(applet);
hard_link(&exec_path, &dest).expect("Should be able to hardlink");
dest.pop();
}
}
}
}