diff --git a/.changeset/four-dancers-ring.md b/.changeset/four-dancers-ring.md new file mode 100644 index 0000000..528ff8d --- /dev/null +++ b/.changeset/four-dancers-ring.md @@ -0,0 +1,5 @@ +--- +"pactup": patch +--- + +fix: allow alias for which command and fetch nightly by tag diff --git a/src/choose_version_for_user_input.rs b/src/choose_version_for_user_input.rs index 80a0024..7c0c573 100644 --- a/src/choose_version_for_user_input.rs +++ b/src/choose_version_for_user_input.rs @@ -36,8 +36,6 @@ pub fn choose_version_for_user_input<'a>( let result = if let Some(version) = current_version { info!("Using Pact {}", version.to_string().cyan()); let path = config.installations_dir().join(version.to_string()); - // .join("installation"); - Some(ApplicableVersion { path, version: version.clone(), diff --git a/src/commands/install.rs b/src/commands/install.rs index b41f6dd..6c73ac0 100644 --- a/src/commands/install.rs +++ b/src/commands/install.rs @@ -94,8 +94,8 @@ impl Command for Install { return Err(Error::UninstallableVersion { version: v }); } UserVersion::Full(Version::Nightly(nightly_tag)) => { - let picked_release = - remote_pact_index::latest(&config.pact_5x_repo).map_err(|_| Error::CantFindNightly { + let picked_release = remote_pact_index::get_by_tag(&config.pact_5x_repo, &nightly_tag) + .map_err(|_| Error::CantFindNightly { nightly_tag: nightly_tag.clone(), })?; @@ -155,10 +155,7 @@ impl Command for Install { }; if let UserVersion::Full(Version::Nightly(nightly_type)) = current_version { - if nightly_type == "nightly" { - return Ok(()); - } - let alias_name = "nightly".to_string(); + let alias_name = nightly_type.to_string(); debug!( "Tagging {} as alias for {}", alias_name.cyan(), diff --git a/src/commands/use.rs b/src/commands/use.rs index 69dd020..77d7937 100644 --- a/src/commands/use.rs +++ b/src/commands/use.rs @@ -50,10 +50,10 @@ impl Command for Use { VersionFileStrategy::Recursive => InferVersionError::Recursive, }) .map_err(|source| Error::CantInferVersion { source })?; + let current_version = requested_version.to_version(&all_versions, config); let (message, version_path) = if let Some(version) = current_version { let version_path = config.installations_dir().join(version.to_string()); - // .join("installation"); let message = format!("Using Pact {}", version.to_string().cyan()); (message, version_path) } else if let UserVersion::Full(Version::Bypassed) = requested_version { diff --git a/src/commands/which.rs b/src/commands/which.rs index 1039908..725013f 100644 --- a/src/commands/which.rs +++ b/src/commands/which.rs @@ -1,8 +1,10 @@ use super::command::Command; use crate::config::PactupConfig; -use crate::installed_versions; +use crate::user_version::UserVersion; use crate::user_version_reader::UserVersionReader; +use crate::version::Version; use crate::version_file_strategy::VersionFileStrategy; +use crate::{fs, installed_versions, system_version}; use colored::Colorize; use thiserror::Error; @@ -29,17 +31,36 @@ impl Command for Which { VersionFileStrategy::Recursive => InferVersionError::Recursive, }) .map_err(|source| Error::CantInferVersion { source })?; - let current_version = requested_version.to_version(&all_versions, config); - if let Some(version) = current_version { - println!("{}", version.installation_path(config).display()); + let version_path = if let Some(version) = current_version { + config.installations_dir().join(version.to_string()) + } else if let UserVersion::Full(Version::Bypassed) = requested_version { + system_version::path() + } else if let Some(alias_name) = requested_version.alias_name() { + let alias_path = config.aliases_dir().join(alias_name); + let system_path = system_version::path(); + if matches!(fs::shallow_read_symlink(&alias_path), Ok(shallow_path) if shallow_path == system_path) + { + system_path + } else if alias_path.exists() { + alias_path + } else { + let error_message = format!( + "Can't find an installed Pact version matching {}.", + requested_version.to_string().italic() + ); + eprintln!("{}", error_message.red()); + return Ok(()); + } } else { let error_message = format!( "Can't find an installed Pact version matching {}.", requested_version.to_string().italic() ); eprintln!("{}", error_message.red()); - } + return Ok(()); + }; + println!("{}", version_path.to_string_lossy()); Ok(()) } } diff --git a/src/remote_pact_index.rs b/src/remote_pact_index.rs index 3c23e88..adc7957 100644 --- a/src/remote_pact_index.rs +++ b/src/remote_pact_index.rs @@ -198,6 +198,17 @@ pub fn latest(repo_url: &String) -> Result { Ok(value) } +/// Prints +/// ```rust +/// use crate::remote_pact_index::get_by_tag; +/// +pub fn get_by_tag(repo_url: &String, tag: &String) -> Result { + let index_json_url = format!("https://api.github.com/repos/{repo_url}/releases/tags/{tag}"); + let resp = handle_github_rate_limit(crate::http::get(&index_json_url)?); + let value: Release = resp.json()?; + Ok(value) +} + #[cfg(target_os = "linux")] #[cfg(test)] mod tests { diff --git a/src/version.rs b/src/version.rs index 1cfb74a..4f80f30 100644 --- a/src/version.rs +++ b/src/version.rs @@ -68,11 +68,15 @@ impl Version { pub fn installation_path(&self, config: &config::PactupConfig) -> std::path::PathBuf { match self { Self::Bypassed => system_version::path(), - v @ (Self::Nightly(_) | Self::Alias(_) | Self::Latest) => { - config.aliases_dir().join(v.alias_name().unwrap()) - } + v @ (Self::Alias(_) | Self::Latest) => config.aliases_dir().join(v.alias_name().unwrap()), v @ Self::Semver(_) => config.installations_dir().join(v.v_str()), - // .join("installation"), + v @ Self::Nightly(_) => { + if config.installations_dir().join(v.v_str()).exists() { + config.installations_dir().join(v.v_str()) + } else { + config.aliases_dir().join(v.alias_name().unwrap()) + } + } } }