From c006d4578421c5101b1146cf78b87a7b6afd47ab Mon Sep 17 00:00:00 2001 From: Dilum Aluthge Date: Wed, 26 Jul 2023 19:06:07 -0400 Subject: [PATCH] Run `cargo fmt` and check in the results --- src/bin/julialauncher.rs | 39 +++++++++-------- src/bin/juliaup.rs | 20 +++++---- src/command_link.rs | 2 +- src/command_list.rs | 2 +- src/command_override.rs | 80 +++++++++++++++++++++++----------- src/command_remove.rs | 7 ++- src/config_file.rs | 2 +- tests/command_override_test.rs | 6 +-- 8 files changed, 100 insertions(+), 58 deletions(-) diff --git a/src/bin/julialauncher.rs b/src/bin/julialauncher.rs index b8115e6d..388da0f9 100644 --- a/src/bin/julialauncher.rs +++ b/src/bin/julialauncher.rs @@ -203,10 +203,14 @@ fn get_julia_path_from_channel( } } -fn get_override_channel(config_file: &juliaup::config_file::JuliaupReadonlyConfigFile) -> Result> { +fn get_override_channel( + config_file: &juliaup::config_file::JuliaupReadonlyConfigFile, +) -> Result> { let curr_dir = std::env::current_dir()?.canonicalize()?; - let juliaup_override = config_file.data.overrides + let juliaup_override = config_file + .data + .overrides .iter() .filter(|i| curr_dir.starts_with(&i.path)) .sorted_by_key(|i| i.path.len()) @@ -214,7 +218,7 @@ fn get_override_channel(config_file: &juliaup::config_file::JuliaupReadonlyConfi match juliaup_override { Some(val) => Ok(Some(val.channel.clone())), - None => Ok(None) + None => Ok(None), } } @@ -245,21 +249,20 @@ fn run_app() -> Result { } } - let (julia_channel_to_use, juliaup_channel_source) = if let Some(channel) = channel_from_cmd_line { - (channel, JuliaupChannelSource::CmdLine) - } - else if let Ok(channel) = std::env::var("JULIAUP_CHANNEL") { - (channel, JuliaupChannelSource::EnvVar) - } - else if let Ok(Some(channel)) = get_override_channel(&config_file) { - (channel, JuliaupChannelSource::Override) - } - else if let Some(channel) = config_file.data.default.clone() { - (channel, JuliaupChannelSource::Default) - } - else { - return Err(anyhow!("The Julia launcher failed to figure out which juliaup channel to use.")); - }; + let (julia_channel_to_use, juliaup_channel_source) = + if let Some(channel) = channel_from_cmd_line { + (channel, JuliaupChannelSource::CmdLine) + } else if let Ok(channel) = std::env::var("JULIAUP_CHANNEL") { + (channel, JuliaupChannelSource::EnvVar) + } else if let Ok(Some(channel)) = get_override_channel(&config_file) { + (channel, JuliaupChannelSource::Override) + } else if let Some(channel) = config_file.data.default.clone() { + (channel, JuliaupChannelSource::Default) + } else { + return Err(anyhow!( + "The Julia launcher failed to figure out which juliaup channel to use." + )); + }; let (julia_path, julia_args) = get_julia_path_from_channel( &versiondb_data, diff --git a/src/bin/juliaup.rs b/src/bin/juliaup.rs index b7574c92..7337bf69 100644 --- a/src/bin/juliaup.rs +++ b/src/bin/juliaup.rs @@ -1,6 +1,5 @@ use anyhow::{Context, Result}; use clap::Parser; -use juliaup::{command_add::run_command_add, command_override::run_command_override_set}; use juliaup::command_api::run_command_api; #[cfg(not(windows))] use juliaup::command_config_symlinks::run_command_config_symlinks; @@ -18,6 +17,7 @@ use juliaup::command_status::run_command_status; use juliaup::command_update::run_command_update; use juliaup::command_update_version_db::run_command_update_version_db; use juliaup::global_paths::get_paths; +use juliaup::{command_add::run_command_add, command_override::run_command_override_set}; #[cfg(feature = "selfupdate")] use juliaup::{ command_config_backgroundselfupdate::run_command_config_backgroundselfupdate, @@ -81,17 +81,17 @@ enum Juliaup { /// Manage directory overrides enum OverrideSubCmd { Status {}, - Set { + Set { channel: String, #[clap(long, short)] - path: Option + path: Option, }, Unset { #[clap(long, short)] nonexistent: bool, #[clap(long, short)] path: Option, - } + }, } #[derive(Parser)] @@ -205,10 +205,14 @@ fn main() -> Result<()> { Juliaup::InitialSetupFromLauncher {} => run_command_initial_setup_from_launcher(&paths), Juliaup::UpdateVersionDb {} => run_command_update_version_db(&paths), Juliaup::OverrideSubCmd(subcmd) => match subcmd { - OverrideSubCmd::Status { } => run_command_override_status(&paths), - OverrideSubCmd::Set { channel, path } => run_command_override_set(&paths, channel, path), - OverrideSubCmd::Unset { nonexistent, path } => run_command_override_unset(&paths, nonexistent, path), - } + OverrideSubCmd::Status {} => run_command_override_status(&paths), + OverrideSubCmd::Set { channel, path } => { + run_command_override_set(&paths, channel, path) + } + OverrideSubCmd::Unset { nonexistent, path } => { + run_command_override_unset(&paths, nonexistent, path) + } + }, Juliaup::Info {} => run_command_info(&paths), #[cfg(feature = "selfupdate")] Juliaup::SecretSelfUpdate {} => run_command_selfupdate(&paths), diff --git a/src/command_link.rs b/src/command_link.rs index fe2ed88e..c7fcb2e4 100644 --- a/src/command_link.rs +++ b/src/command_link.rs @@ -1,4 +1,3 @@ -use std::path::Path; use crate::config_file::JuliaupConfigChannel; use crate::config_file::{load_mut_config_db, save_config_db}; use crate::global_paths::GlobalPaths; @@ -7,6 +6,7 @@ use crate::operations::create_symlink; use crate::versions_file::load_versions_db; use anyhow::{bail, Context, Result}; use path_absolutize::Absolutize; +use std::path::Path; pub fn run_command_link( channel: &str, diff --git a/src/command_list.rs b/src/command_list.rs index aa49493e..bac2aa24 100644 --- a/src/command_list.rs +++ b/src/command_list.rs @@ -4,8 +4,8 @@ use cli_table::{ format::{Border, HorizontalLine, Separator}, print_stdout, ColorChoice, Table, WithTitle, }; -use itertools::Itertools; use human_sort::compare; +use itertools::Itertools; #[derive(Table)] struct ChannelRow { diff --git a/src/command_override.rs b/src/command_override.rs index 2128e9f5..e6122101 100644 --- a/src/command_override.rs +++ b/src/command_override.rs @@ -1,17 +1,26 @@ -use std::{path::{PathBuf, Path}, env::{current_dir}}; - -use anyhow::{Result, Context, bail}; -use cli_table::{Table, print_stdout, WithTitle, format::{Separator, HorizontalLine, Border}, ColorChoice}; +use std::{ + env::current_dir, + path::{Path, PathBuf}, +}; + +use anyhow::{bail, Context, Result}; +use cli_table::{ + format::{Border, HorizontalLine, Separator}, + print_stdout, ColorChoice, Table, WithTitle, +}; use itertools::Itertools; -use crate::{config_file::{load_config_db, load_mut_config_db, JuliaupOverride, save_config_db}, global_paths::GlobalPaths}; +use crate::{ + config_file::{load_config_db, load_mut_config_db, save_config_db, JuliaupOverride}, + global_paths::GlobalPaths, +}; #[derive(Table)] struct OverrideRow { #[table(title = "Path")] path: String, #[table(title = "Channel")] - channel: String + channel: String, } pub fn run_command_override_status(paths: &GlobalPaths) -> Result<()> { @@ -25,7 +34,9 @@ pub fn run_command_override_status(paths: &GlobalPaths) -> Result<()> { .sorted_by_key(|i| i.path.to_string()) .map(|i| -> OverrideRow { OverrideRow { - path: dunce::simplified(&PathBuf::from(&i.path)).to_string_lossy().to_string(), + path: dunce::simplified(&PathBuf::from(&i.path)) + .to_string_lossy() + .to_string(), channel: i.channel.to_string(), } }) @@ -42,11 +53,15 @@ pub fn run_command_override_status(paths: &GlobalPaths) -> Result<()> { .build(), ), )?; - + Ok(()) } -pub fn run_command_override_set(paths: &GlobalPaths, channel: String, path: Option) -> Result<()> { +pub fn run_command_override_set( + paths: &GlobalPaths, + channel: String, + path: Option, +) -> Result<()> { let mut config_file = load_mut_config_db(paths) .with_context(|| "`override set` command failed to load configuration data.")?; @@ -56,16 +71,23 @@ pub fn run_command_override_set(paths: &GlobalPaths, channel: String, path: Opti let path = match path { Some(path) => PathBuf::from(path), - None => { - current_dir()? - } - }.canonicalize()?; + None => current_dir()?, + } + .canonicalize()?; - if config_file.data.overrides.iter().any(|i| i.path == path.to_string_lossy().to_string()) { + if config_file + .data + .overrides + .iter() + .any(|i| i.path == path.to_string_lossy().to_string()) + { bail!("'{}' path already has an override configured.", &channel); } - - config_file.data.overrides.push(JuliaupOverride { path: path.to_string_lossy().to_string(), channel: channel.clone() }); + + config_file.data.overrides.push(JuliaupOverride { + path: path.to_string_lossy().to_string(), + channel: channel.clone(), + }); save_config_db(&mut config_file) .with_context(|| "Failed to save configuration file from `override add` command.")?; @@ -73,23 +95,31 @@ pub fn run_command_override_set(paths: &GlobalPaths, channel: String, path: Opti Ok(()) } -pub fn run_command_override_unset(paths: &GlobalPaths, nonexistent: bool, path: Option) -> Result<()> { +pub fn run_command_override_unset( + paths: &GlobalPaths, + nonexistent: bool, + path: Option, +) -> Result<()> { let mut config_file = load_mut_config_db(paths) .with_context(|| "`override unset` command failed to load configuration data.")?; let path = match path { Some(path) => PathBuf::from(path), - None => { - current_dir()? - } - }.canonicalize()?; + None => current_dir()?, + } + .canonicalize()?; if nonexistent { - config_file.data.overrides.retain(|x| Path::new(&x.path).is_dir()); - } - else { + config_file + .data + .overrides + .retain(|x| Path::new(&x.path).is_dir()); + } else { // First remove any duplicates - config_file.data.overrides.retain(|x| Path::new(&x.path) != path); + config_file + .data + .overrides + .retain(|x| Path::new(&x.path) != path); } save_config_db(&mut config_file) diff --git a/src/command_remove.rs b/src/command_remove.rs index 3665aadf..bd490b5c 100644 --- a/src/command_remove.rs +++ b/src/command_remove.rs @@ -27,7 +27,12 @@ pub fn run_command_remove(channel: &str, paths: &GlobalPaths) -> Result<()> { } } - if config_file.data.overrides.iter().any(|i| i.channel == channel) { + if config_file + .data + .overrides + .iter() + .any(|i| i.channel == channel) + { bail!( "'{}' cannot be removed because it is currently used in a directory override.", channel diff --git a/src/config_file.rs b/src/config_file.rs index 6d05361b..b7a2cca4 100644 --- a/src/config_file.rs +++ b/src/config_file.rs @@ -71,7 +71,7 @@ pub struct JuliaupOverride { #[serde(rename = "Path")] pub path: String, #[serde(rename = "Channel")] - pub channel: String + pub channel: String, } #[derive(Serialize, Deserialize, Clone)] diff --git a/tests/command_override_test.rs b/tests/command_override_test.rs index 0c9e0aae..637d4e59 100644 --- a/tests/command_override_test.rs +++ b/tests/command_override_test.rs @@ -206,7 +206,7 @@ fn command_override_overlap_test() { let depot_dir = assert_fs::TempDir::new().unwrap(); let or_dir_parent = assert_fs::TempDir::new().unwrap(); - let or_dir_child= or_dir_parent.join("child"); + let or_dir_child = or_dir_parent.join("child"); std::fs::create_dir_all(&or_dir_child).unwrap(); Command::cargo_bin("juliaup") @@ -304,7 +304,7 @@ fn command_override_delete_empty_test() { .assert() .success() .stdout(""); - + Command::cargo_bin("juliaup") .unwrap() .arg("override") @@ -377,4 +377,4 @@ fn command_override_delete_empty_test() { .assert() .success() .stdout(" Path Channel \n---------------\n"); -} \ No newline at end of file +}