Skip to content

Commit

Permalink
fix: allow aliases with which command and fetch nightly by tag
Browse files Browse the repository at this point in the history
  • Loading branch information
salamaashoush committed May 28, 2024
1 parent db662e6 commit 4f9080d
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 18 deletions.
5 changes: 5 additions & 0 deletions .changeset/four-dancers-ring.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"pactup": patch
---

fix: allow alias for which command and fetch nightly by tag
2 changes: 0 additions & 2 deletions src/choose_version_for_user_input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
9 changes: 3 additions & 6 deletions src/commands/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
})?;

Expand Down Expand Up @@ -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(),
Expand Down
2 changes: 1 addition & 1 deletion src/commands/use.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
31 changes: 26 additions & 5 deletions src/commands/which.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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(())
}
}
Expand Down
11 changes: 11 additions & 0 deletions src/remote_pact_index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,17 @@ pub fn latest(repo_url: &String) -> Result<Release, crate::http::Error> {
Ok(value)
}

/// Prints
/// ```rust
/// use crate::remote_pact_index::get_by_tag;
///
pub fn get_by_tag(repo_url: &String, tag: &String) -> Result<Release, crate::http::Error> {
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 {
Expand Down
12 changes: 8 additions & 4 deletions src/version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
}
}
}

Expand Down

0 comments on commit 4f9080d

Please sign in to comment.