aboutsummaryrefslogtreecommitdiff
path: root/src/main.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/main.rs')
-rw-r--r--src/main.rs57
1 files changed, 26 insertions, 31 deletions
diff --git a/src/main.rs b/src/main.rs
index 80a03b9..f035a45 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,12 +1,13 @@
1#![feature(drain_filter)] 1#![feature(drain_filter)]
2#![feature(io_error_other)]
2 3
3//pub mod config;
4pub mod dotup; 4pub mod dotup;
5 5
6use std::path::PathBuf; 6use std::path::PathBuf;
7 7
8use anyhow::Context; 8use anyhow::Context;
9use clap::{Parser, Subcommand}; 9use clap::{Parser, Subcommand};
10use dotup::InstallParams;
10 11
11#[derive(Parser, Debug)] 12#[derive(Parser, Debug)]
12struct GlobalFlags { 13struct GlobalFlags {
@@ -27,6 +28,9 @@ enum SubCommand {
27 28
28#[derive(Parser, Debug)] 29#[derive(Parser, Debug)]
29struct InstallArgs { 30struct InstallArgs {
31 #[clap(short, long)]
32 force: bool,
33
30 groups: Vec<String>, 34 groups: Vec<String>,
31} 35}
32 36
@@ -47,6 +51,7 @@ struct FormatArgs {}
47struct Args { 51struct Args {
48 #[clap(flatten)] 52 #[clap(flatten)]
49 globals: GlobalFlags, 53 globals: GlobalFlags,
54
50 #[clap(subcommand)] 55 #[clap(subcommand)]
51 command: SubCommand, 56 command: SubCommand,
52} 57}
@@ -64,6 +69,10 @@ fn main() -> anyhow::Result<()> {
64} 69}
65 70
66impl GlobalFlags { 71impl GlobalFlags {
72 fn get_working_dir(&self) -> PathBuf {
73 self.config.parent().unwrap().to_path_buf()
74 }
75
67 fn base_path_or_default(&self) -> PathBuf { 76 fn base_path_or_default(&self) -> PathBuf {
68 self.base.clone().unwrap_or_else(|| { 77 self.base.clone().unwrap_or_else(|| {
69 PathBuf::from(std::env::var("HOME").expect("failed to get HOME directory")) 78 PathBuf::from(std::env::var("HOME").expect("failed to get HOME directory"))
@@ -72,49 +81,29 @@ impl GlobalFlags {
72} 81}
73 82
74fn command_install(globals: GlobalFlags, args: InstallArgs) -> anyhow::Result<()> { 83fn command_install(globals: GlobalFlags, args: InstallArgs) -> anyhow::Result<()> {
75 let dotup = dotup::load_file(&globals.config).context("failed to parse config")?; 84 let context = helper_new_context(&globals)?;
76 let cwd = std::env::current_dir().context("failed to get current directory")?; 85 let dotup = dotup::load_file(context, &globals.config).context("failed to parse config")?;
77 let install_params = dotup::InstallParams { 86 let params = InstallParams { force: args.force };
78 cwd: &cwd,
79 home: &globals.base_path_or_default(),
80 };
81 for group in args.groups { 87 for group in args.groups {
82 match dotup.find_group_by_name(&group) { 88 dotup::install(&dotup, &params, &group)?;
83 Some(group_id) => dotup.install(install_params, group_id)?,
84 None => log::error!("group not found: {}", group),
85 };
86 } 89 }
87 Ok(()) 90 Ok(())
88} 91}
89 92
90fn command_uninstall(globals: GlobalFlags, args: UninstallArgs) -> anyhow::Result<()> { 93fn command_uninstall(globals: GlobalFlags, args: UninstallArgs) -> anyhow::Result<()> {
91 let dotup = dotup::load_file(&globals.config).context("failed to parse config")?; 94 let context = helper_new_context(&globals)?;
92 let cwd = std::env::current_dir().context("failed to get current directory")?; 95 let dotup = dotup::load_file(context, &globals.config).context("failed to parse config")?;
93 let uninstall_params = dotup::UninstallParams {
94 cwd: &cwd,
95 home: &globals.base_path_or_default(),
96 };
97 for group in args.groups { 96 for group in args.groups {
98 match dotup.find_group_by_name(&group) { 97 dotup::uninstall(&dotup, &group)?;
99 Some(group_id) => dotup.uninstall(uninstall_params, group_id)?,
100 None => log::error!("group not found: {}", group),
101 };
102 } 98 }
103 Ok(()) 99 Ok(())
104} 100}
105 101
106fn command_status(globals: GlobalFlags, args: StatusArgs) -> anyhow::Result<()> { 102fn command_status(globals: GlobalFlags, args: StatusArgs) -> anyhow::Result<()> {
107 let dotup = dotup::load_file(&globals.config).context("failed to parse config")?; 103 let context = helper_new_context(&globals)?;
108 let cwd = std::env::current_dir().context("failed to get current directory")?; 104 let dotup = dotup::load_file(context, &globals.config).context("failed to parse config")?;
109 let install_params = dotup::InstallParams {
110 cwd: &cwd,
111 home: &globals.base_path_or_default(),
112 };
113 for group in args.groups { 105 for group in args.groups {
114 match dotup.find_group_by_name(&group) { 106 dotup::status(&dotup, &group)?;
115 Some(group_id) => dotup.status(install_params, group_id)?,
116 None => log::error!("group not found: {}", group),
117 };
118 } 107 }
119 Ok(()) 108 Ok(())
120} 109}
@@ -123,3 +112,9 @@ fn command_format(globals: GlobalFlags, _args: FormatArgs) -> anyhow::Result<()>
123 dotup::format_file_inplace(&globals.config).context("failed to format config")?; 112 dotup::format_file_inplace(&globals.config).context("failed to format config")?;
124 Ok(()) 113 Ok(())
125} 114}
115
116fn helper_new_context(globals: &GlobalFlags) -> anyhow::Result<dotup::Context> {
117 let cwd = globals.get_working_dir();
118 let home = globals.base_path_or_default();
119 Ok(dotup::Context::new(cwd, home)?)
120}