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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
|
pub mod dotup;
use std::path::PathBuf;
use anyhow::Context;
use clap::{Parser, Subcommand};
use dotup::InstallParams;
#[derive(Parser, Debug)]
struct GlobalFlags {
#[clap(long)]
base: Option<PathBuf>,
#[clap(long, default_value = "./dotup")]
config: PathBuf,
}
#[derive(Subcommand, Debug)]
enum SubCommand {
Install(InstallArgs),
Uninstall(UninstallArgs),
Status(StatusArgs),
Format(FormatArgs),
}
#[derive(Parser, Debug)]
struct InstallArgs {
#[clap(short, long)]
force: bool,
groups: Vec<String>,
}
#[derive(Parser, Debug)]
struct UninstallArgs {
groups: Vec<String>,
}
#[derive(Parser, Debug)]
struct StatusArgs {
groups: Vec<String>,
}
#[derive(Parser, Debug)]
struct FormatArgs {}
#[derive(Parser, Debug)]
struct Args {
#[clap(flatten)]
globals: GlobalFlags,
#[clap(subcommand)]
command: SubCommand,
}
fn main() -> anyhow::Result<()> {
env_logger::init();
let args = Args::parse();
match args.command {
SubCommand::Install(install) => command_install(args.globals, install),
SubCommand::Uninstall(uninstall) => command_uninstall(args.globals, uninstall),
SubCommand::Status(status) => command_status(args.globals, status),
SubCommand::Format(format) => command_format(args.globals, format),
}
}
impl GlobalFlags {
fn get_working_dir(&self) -> PathBuf {
self.config.parent().unwrap().to_path_buf()
}
fn base_path_or_default(&self) -> PathBuf {
self.base.clone().unwrap_or_else(|| {
PathBuf::from(std::env::var("HOME").expect("failed to get HOME directory"))
})
}
}
fn command_install(globals: GlobalFlags, args: InstallArgs) -> anyhow::Result<()> {
let context = helper_new_context(&globals)?;
let dotup = dotup::load_file(context, &globals.config).context("failed to parse config")?;
let params = InstallParams { force: args.force };
for group in args.groups {
dotup::install(&dotup, ¶ms, &group)?;
}
Ok(())
}
fn command_uninstall(globals: GlobalFlags, args: UninstallArgs) -> anyhow::Result<()> {
let context = helper_new_context(&globals)?;
let dotup = dotup::load_file(context, &globals.config).context("failed to parse config")?;
for group in args.groups {
dotup::uninstall(&dotup, &group)?;
}
Ok(())
}
fn command_status(globals: GlobalFlags, args: StatusArgs) -> anyhow::Result<()> {
let context = helper_new_context(&globals)?;
let dotup = dotup::load_file(context, &globals.config).context("failed to parse config")?;
for group in args.groups {
dotup::status(&dotup, &group)?;
}
Ok(())
}
fn command_format(globals: GlobalFlags, _args: FormatArgs) -> anyhow::Result<()> {
dotup::format_file_inplace(&globals.config).context("failed to format config")?;
Ok(())
}
fn helper_new_context(globals: &GlobalFlags) -> anyhow::Result<dotup::Context> {
let cwd = globals.get_working_dir();
let home = globals.base_path_or_default();
Ok(dotup::Context::new(cwd, home)?)
}
|