From 92b05a877eb772985d2f4fc9cd198ca642b69b6a Mon Sep 17 00:00:00 2001 From: diogo464 Date: Tue, 8 Feb 2022 09:21:12 +0000 Subject: removed old code --- dotup_cli/src/commands/init.rs | 22 ----- dotup_cli/src/commands/install.rs | 25 ------ dotup_cli/src/commands/link.rs | 134 --------------------------- dotup_cli/src/commands/mod.rs | 11 --- dotup_cli/src/commands/mv.rs | 53 ----------- dotup_cli/src/commands/status.rs | 175 ------------------------------------ dotup_cli/src/commands/uninstall.rs | 26 ------ dotup_cli/src/commands/unlink.rs | 43 --------- 8 files changed, 489 deletions(-) delete mode 100644 dotup_cli/src/commands/init.rs delete mode 100644 dotup_cli/src/commands/install.rs delete mode 100644 dotup_cli/src/commands/link.rs delete mode 100644 dotup_cli/src/commands/mod.rs delete mode 100644 dotup_cli/src/commands/mv.rs delete mode 100644 dotup_cli/src/commands/status.rs delete mode 100644 dotup_cli/src/commands/uninstall.rs delete mode 100644 dotup_cli/src/commands/unlink.rs (limited to 'dotup_cli/src/commands') diff --git a/dotup_cli/src/commands/init.rs b/dotup_cli/src/commands/init.rs deleted file mode 100644 index 45129bf..0000000 --- a/dotup_cli/src/commands/init.rs +++ /dev/null @@ -1,22 +0,0 @@ -use super::prelude::*; - -/// Creates an empty depot file if one doesnt already exist. -/// -/// By default this will create the file in the current directory -/// but the --depot flag can be used to change this path. -#[derive(Parser)] -pub struct Opts {} - -pub fn main(config: Config, opts: Opts) -> anyhow::Result<()> { - if !dotup::utils::is_file(&config.archive_path)? { - let archive = Archive::default(); - log::info!("Creating archive at {}", &config.archive_path.display()); - utils::write_archive(&config.archive_path, &archive)?; - } else { - log::warn!( - "Archive file already exists : '{}'", - config.archive_path.display() - ); - } - Ok(()) -} diff --git a/dotup_cli/src/commands/install.rs b/dotup_cli/src/commands/install.rs deleted file mode 100644 index 6d9fbf7..0000000 --- a/dotup_cli/src/commands/install.rs +++ /dev/null @@ -1,25 +0,0 @@ -use std::path::PathBuf; - -use super::prelude::*; - -/// Install links. (Creates symlinks). -/// -/// Installing a link will create the necessary directories. -/// If a file or directory already exists at the location a link would be installed this command will fail. -#[derive(Parser)] -pub struct Opts { - /// The files/directories to install. - #[clap(min_values = 1, default_value = ".")] - paths: Vec, -} - -pub fn main(config: Config, opts: Opts) -> anyhow::Result<()> { - let depot = utils::read_depot(&config.archive_path)?; - - for link in utils::collect_links_by_base_paths(&depot, &opts.paths) { - log::info!("Installing link {}", link); - depot.install_link(link, &config.install_path)?; - } - - Ok(()) -} diff --git a/dotup_cli/src/commands/link.rs b/dotup_cli/src/commands/link.rs deleted file mode 100644 index d1f61ea..0000000 --- a/dotup_cli/src/commands/link.rs +++ /dev/null @@ -1,134 +0,0 @@ -use std::{ - fs::{DirEntry, Metadata}, - path::{Path, PathBuf}, -}; - -use super::prelude::*; - -/// Creates links -/// -/// If a link is created for a file that already had a link then the old link will be overwritten. -/// By default creating a link to a directory will recursively link all files under that -/// directory, to actually link a directory use the --directory flag. -#[derive(Parser)] -pub struct Opts { - /// Treats the paths as directories. This will create links to the actual directories instead - /// of recursively linking all files under them. - #[clap(long)] - directory: bool, - - /// The paths to link. The last path is the destination. - paths: Vec, -} - -// TODO: require destination -// remove else branch -pub fn main(config: Config, opts: Opts) -> anyhow::Result<()> { - let mut depot = utils::read_depot(&config.archive_path)?; - - let (origins, destination) = match opts.paths.as_slice() { - p @ [] | p @ [_] => (p, None), - [o @ .., dest] => (o, Some(dest)), - _ => unreachable!(), - }; - - if let Some(destination) = destination { - let params = if opts.directory { - origins - .iter() - .map(|p| LinkCreateParams { - origin: p.to_path_buf(), - destination: destination.clone(), - }) - .collect() - } else { - let mut params = Vec::new(); - for origin in origins { - generate_link_params(&depot, origin, destination, origin, &mut params)?; - } - params - }; - - for link_params in params { - log::info!("Creating link : {}", link_params); - depot.create_link(link_params)?; - } - } else { - let base_path = match origins { - [] => std::env::current_dir()?, - [path] => path.clone(), - _ => unreachable!(), - }; - - for link in utils::collect_links_by_base_paths(&depot, std::iter::once(base_path)) { - log::info!("{}", link); - } - } - - utils::write_depot(&depot)?; - - Ok(()) -} - -fn generate_link_params( - depot: &Depot, - origin: &Path, - destination: &Path, - base: &Path, - params: &mut Vec, -) -> anyhow::Result<()> { - let metadata = std::fs::metadata(origin)?; - if metadata.is_file() { - generate_file_link_params(depot, origin, destination, base, params)?; - } else if metadata.is_dir() { - generate_directory_link_params_recursive(depot, origin, destination, base, params)?; - } - Ok(()) -} - -fn generate_file_link_params( - depot: &Depot, - origin: &Path, - destination: &Path, - base: &Path, - params: &mut Vec, -) -> anyhow::Result<()> { - let origin_canonical = origin - .canonicalize() - .expect("Failed to canonicalize origin path"); - let base_canonical = base - .canonicalize() - .expect("Failed to canonicalize base path"); - - log::debug!("Origin canonical : {}", origin_canonical.display()); - log::debug!("Base : {}", base.display()); - - let partial = origin_canonical - .strip_prefix(base_canonical) - .expect("Failed to remove prefix from origin path"); - let destination = destination.join(partial); - let origin = origin_canonical - .strip_prefix(depot.base_path()) - .unwrap_or(&origin_canonical); - - let link_params = LinkCreateParams { - origin: origin.to_path_buf(), - destination, - }; - params.push(link_params); - Ok(()) -} - -fn generate_directory_link_params_recursive( - depot: &Depot, - dir_path: &Path, - destination: &Path, - base: &Path, - params: &mut Vec, -) -> anyhow::Result<()> { - for origin in dir_path.read_dir()? { - let origin = origin?.path(); - generate_link_params(depot, &origin, destination, base, params)?; - } - Ok(()) -} diff --git a/dotup_cli/src/commands/mod.rs b/dotup_cli/src/commands/mod.rs deleted file mode 100644 index bd92599..0000000 --- a/dotup_cli/src/commands/mod.rs +++ /dev/null @@ -1,11 +0,0 @@ -pub mod init; -pub mod install; -pub mod link; -pub mod mv; -pub mod status; -pub mod uninstall; -pub mod unlink; - -mod prelude { - pub use crate::prelude::*; -} diff --git a/dotup_cli/src/commands/mv.rs b/dotup_cli/src/commands/mv.rs deleted file mode 100644 index aae2715..0000000 --- a/dotup_cli/src/commands/mv.rs +++ /dev/null @@ -1,53 +0,0 @@ -use std::path::{Path, PathBuf}; - -use super::prelude::*; - -/// Install links. (Creates symlinks). -/// -/// Installing a link will create the necessary directories. -/// If a file or directory already exists at the location a link would be installed this command will fail. -#[derive(Parser)] -pub struct Opts { - /// The files/directories to move - #[clap(min_values = 2)] - paths: Vec, -} - -pub fn main(config: Config, opts: Opts) -> anyhow::Result<()> { - let mut depot = utils::read_depot(&config.archive_path)?; - - let (sources, destination) = match opts.paths.as_slice() { - [source, destination] => {} - [sources @ .., destination] => { - let mut curr_destination = destination.to_owned(); - for source in sources { - let filename = match source.file_name() { - Some(filename) => filename, - None => { - log::warn!("Ignoring '{}', unknown file name", source.display()); - continue; - } - }; - curr_destination.push(filename); - std::fs::rename(source, &curr_destination)?; - if let Some(id) = depot.get_link_id_by_path(&source) { - depot.rename_link(id, &curr_destination); - } - curr_destination.pop(); - } - } - _ => unreachable!(), - }; - - utils::write_depot(&depot)?; - - Ok(()) -} - -fn rename(depot: &mut Depot, source: &Path, destination: &Path) -> anyhow::Result<()> { - std::fs::rename(source, &destination)?; - if let Some(id) = depot.get_link_id_by_path(&source) { - depot.rename_link(id, &destination); - } - Ok(()) -} diff --git a/dotup_cli/src/commands/status.rs b/dotup_cli/src/commands/status.rs deleted file mode 100644 index b7221db..0000000 --- a/dotup_cli/src/commands/status.rs +++ /dev/null @@ -1,175 +0,0 @@ -use std::path::{Path, PathBuf}; - -use ansi_term::Colour; -use clap::Parser; -use dotup::{Depot, Link}; - -use crate::{utils, Config}; - -/// Shows information about links -/// -/// If a link is created for a file that already had a link then the old link will be overwritten. -/// By default creating a link to a directory will recursively link all files under that -/// directory, to actually link a directory use the --directory flag. -#[derive(Parser)] -pub struct Opts { - /// The location where links will be installed to. - /// Defaults to the home directory. - #[clap(long)] - install_base: Option, - - /// The paths to show the status of - paths: Vec, -} - -#[derive(Debug)] -struct State { - max_depth: u32, - install_path: PathBuf, -} - -pub fn main(config: Config, opts: Opts) -> anyhow::Result<()> { - let mut depot = utils::read_depot(&config.archive_path)?; - - // walk dir - // if node is file: - // if linked - // print name in green and destination blue - // if invalid - // print name and destination red - // if not linked - // print name in gray - // if node is directory: - // if linked - // print name in green and destination blue - // if invalid - // print name and destination red - // if not linked: - // print name in gray - // if contains files that are linked/invalid: - // recurse into directory - // - - let depot_base = depot.base_path(); - let mut paths = Vec::new(); - for path in opts.paths { - let canonical = dotup::utils::weakly_canonical(&path); - if canonical.starts_with(depot_base) { - paths.push(canonical); - } else { - log::warn!("Path '{}' is outside the depot", path.display()); - } - } - - if paths.is_empty() { - paths.push(PathBuf::from(".")); - } - - let state = State { - max_depth: u32::MAX, - install_path: config.install_path, - }; - - let (directories, files) = utils::collect_read_dir_split(".")?; - for path in directories.into_iter().chain(files.into_iter()) { - display_status_path(&depot, &state, &path, 0); - } - - utils::write_depot(&depot)?; - - Ok(()) -} - -fn display_status_path(depot: &Depot, state: &State, path: &Path, depth: u32) { - if depth == state.max_depth { - return; - } - - if path.is_dir() { - display_status_directory(depot, state, path, depth); - } else { - display_status_file(depot, state, path, depth); - } -} - -fn display_status_directory(depot: &Depot, state: &State, path: &Path, depth: u32) { - assert!(path.is_dir()); - if depth == state.max_depth || !depot.subpath_has_links(path) { - return; - } - - if let Some(link) = depot.get_link_by_path(path) { - print_link(depot, state, link, depth); - } else { - for entry in std::fs::read_dir(path).unwrap() { - let entry = match entry { - Ok(entry) => entry, - Err(_) => continue, - }; - let entry_path = entry.path().canonicalize().unwrap(); - let entry_path_stripped = entry_path - .strip_prefix(std::env::current_dir().unwrap()) - .unwrap(); - - print_identation(depth); - println!( - "{}", - Colour::Yellow.paint(&format!("{}", path.file_name().unwrap().to_string_lossy())) - ); - - display_status_path(depot, state, &entry_path_stripped, depth + 1); - } - } -} - -fn display_status_file(depot: &Depot, state: &State, path: &Path, depth: u32) { - print_identation(depth); - if let Some(link) = depot.get_link_by_path(path) { - print_link(depot, state, link, depth); - } else { - print_unlinked(path, depth); - } -} - -fn print_link(depot: &Depot, state: &State, link: &Link, depth: u32) { - let origin = link.origin(); - let dest = link.destination(); - let filename = match origin.file_name() { - Some(filename) => filename, - None => return, - }; - - print_identation(depth); - if depot.is_link_installed(link, &state.install_path) { - println!( - "{} -> {}", - Colour::Green.paint(&format!("{}", filename.to_string_lossy())), - Colour::Blue.paint(&format!("{}", dest.display())), - ); - } else { - println!( - "{}", - Colour::Red.paint(&format!( - "{} -> {}", - filename.to_string_lossy(), - dest.display() - )) - ); - } -} - -fn print_unlinked(path: &Path, depth: u32) { - let filename = match path.file_name() { - Some(filename) => filename, - None => return, - }; - - print_identation(depth); - println!("{}", filename.to_string_lossy()); -} - -fn print_identation(depth: u32) { - for i in 0..depth { - print!(" "); - } -} diff --git a/dotup_cli/src/commands/uninstall.rs b/dotup_cli/src/commands/uninstall.rs deleted file mode 100644 index fe44bf0..0000000 --- a/dotup_cli/src/commands/uninstall.rs +++ /dev/null @@ -1,26 +0,0 @@ -use std::path::PathBuf; - -use super::prelude::*; - -/// Uninstalls links. (Removes symlinks). -/// -/// Uninstalling a link for a file that didnt have a link will do nothing. -/// Uninstalling a directory will recursively uninstall all files under it. -/// Symlinks are only deleted if they were pointing to the correct file. -#[derive(Parser)] -pub struct Opts { - /// The files/directories to uninstall. - #[clap(min_values = 1, default_value = ".")] - paths: Vec, -} - -pub fn main(config: Config, opts: Opts) -> anyhow::Result<()> { - let depot = utils::read_depot(&config.archive_path)?; - - for link in utils::collect_links_by_base_paths(&depot, &opts.paths) { - log::info!("Uninstalling link : {}", link); - depot.uninstall_link(link, &config.install_path)?; - } - - Ok(()) -} diff --git a/dotup_cli/src/commands/unlink.rs b/dotup_cli/src/commands/unlink.rs deleted file mode 100644 index abe23e3..0000000 --- a/dotup_cli/src/commands/unlink.rs +++ /dev/null @@ -1,43 +0,0 @@ -use std::{ - fs::{DirEntry, Metadata}, - path::{Path, PathBuf}, -}; - -use super::prelude::*; - -/// Unlinks files/directories. -/// -/// This will recursively remove links. If a path is a directory then it will remove all links -/// recursively. -/// The links are not uninstall by default, see the --uninstall parameter. -#[derive(Parser)] -pub struct Opts { - /// Specify the install base if the links are also to be uninstalled. - #[clap(long)] - uninstall: Option, - - /// The paths to unlink. - #[clap(required = true, min_values = 1)] - paths: Vec, -} - -pub fn main(config: Config, opts: Opts) -> anyhow::Result<()> { - let mut depot = utils::read_depot(&config.archive_path)?; - - for link_id in utils::collect_link_ids_by_base_paths(&depot, &opts.paths) { - let link = depot.get_link(link_id).unwrap(); - log::info!( - "Unlinking(uninstall = {}) : {}", - opts.uninstall.is_some(), - link - ); - if let Some(ref install_base) = opts.uninstall { - depot.uninstall_link(link, &install_base)?; - } - depot.remove_link(link_id); - } - - utils::write_depot(&depot)?; - - Ok(()) -} -- cgit