Skip to content

Commit

Permalink
Merge pull request #702 from JuliaLang/dpa/cargo-fmt
Browse files Browse the repository at this point in the history
Run `cargo fmt` and check in the results
  • Loading branch information
davidanthoff authored Jul 27, 2023
2 parents 7a9b3ae + c006d45 commit b8a1f8b
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 58 deletions.
39 changes: 21 additions & 18 deletions src/bin/julialauncher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,18 +203,22 @@ fn get_julia_path_from_channel(
}
}

fn get_override_channel(config_file: &juliaup::config_file::JuliaupReadonlyConfigFile) -> Result<Option<String>> {
fn get_override_channel(
config_file: &juliaup::config_file::JuliaupReadonlyConfigFile,
) -> Result<Option<String>> {
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())
.last();

match juliaup_override {
Some(val) => Ok(Some(val.channel.clone())),
None => Ok(None)
None => Ok(None),
}
}

Expand Down Expand Up @@ -245,21 +249,20 @@ fn run_app() -> Result<i32> {
}
}

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,
Expand Down
20 changes: 12 additions & 8 deletions src/bin/juliaup.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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,
Expand Down Expand Up @@ -81,17 +81,17 @@ enum Juliaup {
/// Manage directory overrides
enum OverrideSubCmd {
Status {},
Set {
Set {
channel: String,
#[clap(long, short)]
path: Option<String>
path: Option<String>,
},
Unset {
#[clap(long, short)]
nonexistent: bool,
#[clap(long, short)]
path: Option<String>,
}
},
}

#[derive(Parser)]
Expand Down Expand Up @@ -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),
Expand Down
2 changes: 1 addition & 1 deletion src/command_link.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/command_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
80 changes: 55 additions & 25 deletions src/command_override.rs
Original file line number Diff line number Diff line change
@@ -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<()> {
Expand All @@ -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(),
}
})
Expand All @@ -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<String>) -> Result<()> {
pub fn run_command_override_set(
paths: &GlobalPaths,
channel: String,
path: Option<String>,
) -> Result<()> {
let mut config_file = load_mut_config_db(paths)
.with_context(|| "`override set` command failed to load configuration data.")?;

Expand All @@ -56,40 +71,55 @@ 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.")?;

Ok(())
}

pub fn run_command_override_unset(paths: &GlobalPaths, nonexistent: bool, path: Option<String>) -> Result<()> {
pub fn run_command_override_unset(
paths: &GlobalPaths,
nonexistent: bool,
path: Option<String>,
) -> 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)
Expand Down
7 changes: 6 additions & 1 deletion src/command_remove.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/config_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
6 changes: 3 additions & 3 deletions tests/command_override_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -304,7 +304,7 @@ fn command_override_delete_empty_test() {
.assert()
.success()
.stdout("");

Command::cargo_bin("juliaup")
.unwrap()
.arg("override")
Expand Down Expand Up @@ -377,4 +377,4 @@ fn command_override_delete_empty_test() {
.assert()
.success()
.stdout(" Path Channel \n---------------\n");
}
}

0 comments on commit b8a1f8b

Please sign in to comment.