Skip to content

Commit

Permalink
Add is_valid_channel and use it everywhere `version_db.available_chan…
Browse files Browse the repository at this point in the history
…nels` is used
  • Loading branch information
christiangnrd committed Feb 26, 2024
1 parent 5967916 commit d8181c0
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 14 deletions.
7 changes: 4 additions & 3 deletions src/bin/julialauncher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use itertools::Itertools;
use juliaup::config_file::{load_config_db, JuliaupConfig, JuliaupConfigChannel};
use juliaup::global_paths::get_paths;
use juliaup::jsonstructs_versionsdb::JuliaupVersionDB;
use juliaup::operations::is_valid_channel;
use juliaup::versions_file::load_versions_db;
#[cfg(not(windows))]
use nix::{
Expand Down Expand Up @@ -171,21 +172,21 @@ fn get_julia_path_from_channel(
.get(channel)
.ok_or_else(|| match juliaup_channel_source {
JuliaupChannelSource::CmdLine => {
if versions_db.available_channels.contains_key(channel) {
if is_valid_channel(versions_db, &channel.to_string()) {
UserError { msg: format!("`{}` is not installed. Please run `juliaup add {}` to install channel or version.", channel, channel) }
} else {
UserError { msg: format!("ERROR: Invalid Juliaup channel `{}`. Please run `juliaup list` to get a list of valid channels and versions.", channel) }
}
}.into(),
JuliaupChannelSource::EnvVar=> {
if versions_db.available_channels.contains_key(channel) {
if is_valid_channel(versions_db, &channel.to_string()) {
UserError { msg: format!("`{}` for environment variable JULIAUP_CHANNEL is not installed. Please run `juliaup add {}` to install channel or version.", channel, channel) }
} else {
UserError { msg: format!("ERROR: Invalid Juliaup channel `{}` in environment variable JULIAUP_CHANNEL. Please run `juliaup list` to get a list of valid channels and versions.", channel) }
}
}.into(),
JuliaupChannelSource::Override=> {
if versions_db.available_channels.contains_key(channel) {
if is_valid_channel(versions_db, &channel.to_string()) {
UserError { msg: format!("`{}` for directory override is not installed. Please run `juliaup add {}` to install channel or version.", channel, channel) }
} else {
UserError { msg: format!("ERROR: Invalid Juliaup channel `{}` in directory override. Please run `juliaup list` to get a list of valid channels and versions.", channel) }
Expand Down
3 changes: 2 additions & 1 deletion src/command_default.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::operations::is_valid_channel;
use crate::versions_file::load_versions_db;
use crate::{config_file::*, global_paths::GlobalPaths};
use anyhow::{bail, Context, Result};
Expand All @@ -9,7 +10,7 @@ pub fn run_command_default(channel: &str, paths: &GlobalPaths) -> Result<()> {
if !config_file.data.installed_channels.contains_key(channel) {
let version_db = load_versions_db(paths)
.with_context(|| "`default` command failed to load versions db.")?;
if !version_db.available_channels.contains_key(channel) {
if !is_valid_channel(&version_db, &channel.to_string()) {
bail!("'{}' is not a valid Julia version.", channel);
} else {
bail!(
Expand Down
4 changes: 2 additions & 2 deletions src/command_link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::config_file::JuliaupConfigChannel;
use crate::config_file::{load_mut_config_db, save_config_db};
use crate::global_paths::GlobalPaths;
#[cfg(not(windows))]
use crate::operations::create_symlink;
use crate::operations::{create_symlink, is_valid_channel};
use crate::versions_file::load_versions_db;
use anyhow::{bail, Context, Result};
use path_absolutize::Absolutize;
Expand All @@ -24,7 +24,7 @@ pub fn run_command_link(
bail!("Channel name `{}` is already used.", channel)
}

if versiondb_data.available_channels.contains_key(channel) {
if is_valid_channel(&versiondb_data, &channel.to_string()) {

Check failure on line 27 in src/command_link.rs

View workflow job for this annotation

GitHub Actions / test-juliaup (x86_64-pc-windows-msvc)

cannot find function `is_valid_channel` in this scope

Check failure on line 27 in src/command_link.rs

View workflow job for this annotation

GitHub Actions / check-juliaup (x86_64-pc-windows-msvc-windowsappinstaller)

cannot find function `is_valid_channel` in this scope
eprintln!("WARNING: The channel name `{}` is also a system channel. By linking your custom binary to this channel you are hiding this system channel.", channel);
}

Expand Down
11 changes: 3 additions & 8 deletions src/command_list.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::operations::{compatible_nightly_archs, identify_nightly};
use crate::operations::{compatible_nightly_channels, identify_nightly};
use crate::{global_paths::GlobalPaths, versions_file::load_versions_db};
use anyhow::{Context, Result};
use cli_table::{
Expand All @@ -20,13 +20,8 @@ pub fn run_command_list(paths: &GlobalPaths) -> Result<()> {
let versiondb_data =
load_versions_db(paths).with_context(|| "`list` command failed to load versions db.")?;

let nightly_channels: Vec<String> = std::iter::once("nightly".to_string())
.chain(
compatible_nightly_archs()?
.into_iter()
.map(|arch| format!("nightly~{}", arch)),
)
.collect();
let nightly_channels: Vec<String> = compatible_nightly_channels()?;

let nightly_rows: Vec<ChannelRow> = nightly_channels
.into_iter()
.map(|channel| {
Expand Down
28 changes: 28 additions & 0 deletions src/operations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,34 @@ pub fn compatible_nightly_archs() -> Result<Vec<String>> {
}
}

// which nightly channels are compatible with the current system
pub fn compatible_nightly_channels() -> Result<Vec<String>> {
let nightly_archs: Result<Vec<String>> = compatible_nightly_archs();

if nightly_archs.is_ok() {
let nightly_channels: Vec<String> = std::iter::once("nightly".to_string())
.chain(
compatible_nightly_archs()?
.into_iter()
.map(|arch| format!("nightly~{}", arch)),
)
.collect();
Ok(nightly_channels)
} else {
nightly_archs
}
}

// considers the nightly channels as system channels
pub fn is_valid_channel(versions_db: &JuliaupVersionDB, channel: &String) -> bool {
let regular = versions_db.available_channels.contains_key(channel);

let night_chans = compatible_nightly_channels();

let nightly = night_chans.is_ok_and(|night_chans| night_chans.contains(channel));
regular || nightly
}

// Identify the unversioned name of a nightly (e.g., `latest-macos-x86_64`) for a channel
pub fn identify_nightly(channel: &String) -> Result<String> {
let arch = if channel == "nightly" {
Expand Down

0 comments on commit d8181c0

Please sign in to comment.