diff options
| author | diogo464 <[email protected]> | 2021-12-24 21:09:44 +0000 |
|---|---|---|
| committer | diogo464 <[email protected]> | 2021-12-24 21:09:44 +0000 |
| commit | 6817631ddee3795af3a558bf38e477da4ae5b1cb (patch) | |
| tree | 96b54bc5fcef87046e065726574a473e10989124 | |
| parent | ffcb43df8f39a55be468cf4bdfecd72dd026d940 (diff) | |
made install-path a global option
| -rw-r--r-- | dotup_cli/src/commands/install.rs | 11 | ||||
| -rw-r--r-- | dotup_cli/src/commands/status.rs | 8 | ||||
| -rw-r--r-- | dotup_cli/src/commands/uninstall.rs | 11 | ||||
| -rw-r--r-- | dotup_cli/src/config.rs | 1 | ||||
| -rw-r--r-- | dotup_cli/src/main.rs | 14 | ||||
| -rw-r--r-- | dotup_cli/tests/cli.rs | 6 |
6 files changed, 22 insertions, 29 deletions
diff --git a/dotup_cli/src/commands/install.rs b/dotup_cli/src/commands/install.rs index e166a7b..6d9fbf7 100644 --- a/dotup_cli/src/commands/install.rs +++ b/dotup_cli/src/commands/install.rs | |||
| @@ -8,26 +8,17 @@ use super::prelude::*; | |||
| 8 | /// If a file or directory already exists at the location a link would be installed this command will fail. | 8 | /// If a file or directory already exists at the location a link would be installed this command will fail. |
| 9 | #[derive(Parser)] | 9 | #[derive(Parser)] |
| 10 | pub struct Opts { | 10 | pub struct Opts { |
| 11 | /// The location where links will be installed to. | ||
| 12 | /// Defaults to the home directory. | ||
| 13 | #[clap(long)] | ||
| 14 | install_base: Option<PathBuf>, | ||
| 15 | |||
| 16 | /// The files/directories to install. | 11 | /// The files/directories to install. |
| 17 | #[clap(min_values = 1, default_value = ".")] | 12 | #[clap(min_values = 1, default_value = ".")] |
| 18 | paths: Vec<PathBuf>, | 13 | paths: Vec<PathBuf>, |
| 19 | } | 14 | } |
| 20 | 15 | ||
| 21 | pub fn main(config: Config, opts: Opts) -> anyhow::Result<()> { | 16 | pub fn main(config: Config, opts: Opts) -> anyhow::Result<()> { |
| 22 | let install_base = match opts.install_base { | ||
| 23 | Some(path) => path, | ||
| 24 | None => utils::home_directory()?, | ||
| 25 | }; | ||
| 26 | let depot = utils::read_depot(&config.archive_path)?; | 17 | let depot = utils::read_depot(&config.archive_path)?; |
| 27 | 18 | ||
| 28 | for link in utils::collect_links_by_base_paths(&depot, &opts.paths) { | 19 | for link in utils::collect_links_by_base_paths(&depot, &opts.paths) { |
| 29 | log::info!("Installing link {}", link); | 20 | log::info!("Installing link {}", link); |
| 30 | depot.install_link(link, &install_base)?; | 21 | depot.install_link(link, &config.install_path)?; |
| 31 | } | 22 | } |
| 32 | 23 | ||
| 33 | Ok(()) | 24 | Ok(()) |
diff --git a/dotup_cli/src/commands/status.rs b/dotup_cli/src/commands/status.rs index e05ada9..b7221db 100644 --- a/dotup_cli/src/commands/status.rs +++ b/dotup_cli/src/commands/status.rs | |||
| @@ -25,7 +25,7 @@ pub struct Opts { | |||
| 25 | #[derive(Debug)] | 25 | #[derive(Debug)] |
| 26 | struct State { | 26 | struct State { |
| 27 | max_depth: u32, | 27 | max_depth: u32, |
| 28 | install_base: PathBuf, | 28 | install_path: PathBuf, |
| 29 | } | 29 | } |
| 30 | 30 | ||
| 31 | pub fn main(config: Config, opts: Opts) -> anyhow::Result<()> { | 31 | pub fn main(config: Config, opts: Opts) -> anyhow::Result<()> { |
| @@ -67,9 +67,7 @@ pub fn main(config: Config, opts: Opts) -> anyhow::Result<()> { | |||
| 67 | 67 | ||
| 68 | let state = State { | 68 | let state = State { |
| 69 | max_depth: u32::MAX, | 69 | max_depth: u32::MAX, |
| 70 | install_base: opts | 70 | install_path: config.install_path, |
| 71 | .install_base | ||
| 72 | .unwrap_or(utils::home_directory().unwrap()), | ||
| 73 | }; | 71 | }; |
| 74 | 72 | ||
| 75 | let (directories, files) = utils::collect_read_dir_split(".")?; | 73 | let (directories, files) = utils::collect_read_dir_split(".")?; |
| @@ -142,7 +140,7 @@ fn print_link(depot: &Depot, state: &State, link: &Link, depth: u32) { | |||
| 142 | }; | 140 | }; |
| 143 | 141 | ||
| 144 | print_identation(depth); | 142 | print_identation(depth); |
| 145 | if depot.is_link_installed(link, &state.install_base) { | 143 | if depot.is_link_installed(link, &state.install_path) { |
| 146 | println!( | 144 | println!( |
| 147 | "{} -> {}", | 145 | "{} -> {}", |
| 148 | Colour::Green.paint(&format!("{}", filename.to_string_lossy())), | 146 | Colour::Green.paint(&format!("{}", filename.to_string_lossy())), |
diff --git a/dotup_cli/src/commands/uninstall.rs b/dotup_cli/src/commands/uninstall.rs index fdb1a0a..fe44bf0 100644 --- a/dotup_cli/src/commands/uninstall.rs +++ b/dotup_cli/src/commands/uninstall.rs | |||
| @@ -9,26 +9,17 @@ use super::prelude::*; | |||
| 9 | /// Symlinks are only deleted if they were pointing to the correct file. | 9 | /// Symlinks are only deleted if they were pointing to the correct file. |
| 10 | #[derive(Parser)] | 10 | #[derive(Parser)] |
| 11 | pub struct Opts { | 11 | pub struct Opts { |
| 12 | /// The location where links will be uninstalled from. | ||
| 13 | /// Defaults to home directory. | ||
| 14 | #[clap(long)] | ||
| 15 | install_base: Option<PathBuf>, | ||
| 16 | |||
| 17 | /// The files/directories to uninstall. | 12 | /// The files/directories to uninstall. |
| 18 | #[clap(min_values = 1, default_value = ".")] | 13 | #[clap(min_values = 1, default_value = ".")] |
| 19 | paths: Vec<PathBuf>, | 14 | paths: Vec<PathBuf>, |
| 20 | } | 15 | } |
| 21 | 16 | ||
| 22 | pub fn main(config: Config, opts: Opts) -> anyhow::Result<()> { | 17 | pub fn main(config: Config, opts: Opts) -> anyhow::Result<()> { |
| 23 | let install_base = match opts.install_base { | ||
| 24 | Some(path) => path, | ||
| 25 | None => utils::home_directory()?, | ||
| 26 | }; | ||
| 27 | let depot = utils::read_depot(&config.archive_path)?; | 18 | let depot = utils::read_depot(&config.archive_path)?; |
| 28 | 19 | ||
| 29 | for link in utils::collect_links_by_base_paths(&depot, &opts.paths) { | 20 | for link in utils::collect_links_by_base_paths(&depot, &opts.paths) { |
| 30 | log::info!("Uninstalling link : {}", link); | 21 | log::info!("Uninstalling link : {}", link); |
| 31 | depot.uninstall_link(link, &install_base)?; | 22 | depot.uninstall_link(link, &config.install_path)?; |
| 32 | } | 23 | } |
| 33 | 24 | ||
| 34 | Ok(()) | 25 | Ok(()) |
diff --git a/dotup_cli/src/config.rs b/dotup_cli/src/config.rs index ac4fc66..2046ad5 100644 --- a/dotup_cli/src/config.rs +++ b/dotup_cli/src/config.rs | |||
| @@ -3,4 +3,5 @@ use std::path::PathBuf; | |||
| 3 | #[derive(Debug)] | 3 | #[derive(Debug)] |
| 4 | pub struct Config { | 4 | pub struct Config { |
| 5 | pub archive_path: PathBuf, | 5 | pub archive_path: PathBuf, |
| 6 | pub install_path: PathBuf, | ||
| 6 | } | 7 | } |
diff --git a/dotup_cli/src/main.rs b/dotup_cli/src/main.rs index 05fa850..6161ca7 100644 --- a/dotup_cli/src/main.rs +++ b/dotup_cli/src/main.rs | |||
| @@ -45,6 +45,11 @@ struct Opts { | |||
| 45 | #[clap(short, long, parse(from_occurrences))] | 45 | #[clap(short, long, parse(from_occurrences))] |
| 46 | verbose: i32, | 46 | verbose: i32, |
| 47 | 47 | ||
| 48 | /// The location where links will be installed to. | ||
| 49 | /// Defaults to the home directory. | ||
| 50 | #[clap(short, long)] | ||
| 51 | install_path: Option<PathBuf>, | ||
| 52 | |||
| 48 | #[clap(subcommand)] | 53 | #[clap(subcommand)] |
| 49 | subcmd: SubCommand, | 54 | subcmd: SubCommand, |
| 50 | } | 55 | } |
| @@ -80,9 +85,16 @@ fn main() -> anyhow::Result<()> { | |||
| 80 | Some(path) => path, | 85 | Some(path) => path, |
| 81 | None => utils::find_archive_path()?, | 86 | None => utils::find_archive_path()?, |
| 82 | }; | 87 | }; |
| 88 | let install_path = match opts.install_path { | ||
| 89 | Some(path) => path, | ||
| 90 | None => utils::home_directory()?, | ||
| 91 | }; | ||
| 83 | log::debug!("Archive path : {}", archive_path.display()); | 92 | log::debug!("Archive path : {}", archive_path.display()); |
| 84 | 93 | ||
| 85 | let config = Config { archive_path }; | 94 | let config = Config { |
| 95 | archive_path, | ||
| 96 | install_path, | ||
| 97 | }; | ||
| 86 | 98 | ||
| 87 | match opts.subcmd { | 99 | match opts.subcmd { |
| 88 | SubCommand::Init(opts) => commands::init::main(config, opts), | 100 | SubCommand::Init(opts) => commands::init::main(config, opts), |
diff --git a/dotup_cli/tests/cli.rs b/dotup_cli/tests/cli.rs index 3b8d657..c836f63 100644 --- a/dotup_cli/tests/cli.rs +++ b/dotup_cli/tests/cli.rs | |||
| @@ -89,7 +89,7 @@ fn test_cli_install_uninstall_unlink() { | |||
| 89 | let install_base = format!("{}", install_dir.path().display()); | 89 | let install_base = format!("{}", install_dir.path().display()); |
| 90 | run_command( | 90 | run_command( |
| 91 | &dir, | 91 | &dir, |
| 92 | &format!("install --install-base {} o1 o2", install_base), | 92 | &format!("--install-path {} install o1 o2", install_base), |
| 93 | ) | 93 | ) |
| 94 | .success(); | 94 | .success(); |
| 95 | 95 | ||
| @@ -108,7 +108,7 @@ fn test_cli_install_uninstall_unlink() { | |||
| 108 | 108 | ||
| 109 | run_command( | 109 | run_command( |
| 110 | &dir, | 110 | &dir, |
| 111 | &format!("uninstall --install-base {} o1/file.txt", install_base), | 111 | &format!("--install-path {} uninstall o1/file.txt", install_base), |
| 112 | ) | 112 | ) |
| 113 | .success(); | 113 | .success(); |
| 114 | assert!(!install_dir.path().join(".config/file.txt").exists()); | 114 | assert!(!install_dir.path().join(".config/file.txt").exists()); |
| @@ -117,7 +117,7 @@ fn test_cli_install_uninstall_unlink() { | |||
| 117 | 117 | ||
| 118 | run_command( | 118 | run_command( |
| 119 | &dir, | 119 | &dir, |
| 120 | &format!("uninstall --install-base {} o1", install_base), | 120 | &format!("--install-path {} uninstall o1", install_base), |
| 121 | ) | 121 | ) |
| 122 | .success(); | 122 | .success(); |
| 123 | assert!(!install_dir.path().join(".config/file.txt").exists()); | 123 | assert!(!install_dir.path().join(".config/file.txt").exists()); |
