Skip to content

Commit

Permalink
feat: pactup which command to print version path
Browse files Browse the repository at this point in the history
  • Loading branch information
salamaashoush committed May 28, 2024
1 parent 21d6fc5 commit 8038a4c
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/pretty-nails-tan.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"pactup": patch
---

feat: `pactup which` command to print version path
5 changes: 5 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ pub enum SubCommand {
/// > is pointing to, along with the other aliases that point to the same version.
#[clap(name = "uninstall", bin_name = "uninstall")]
Uninstall(commands::uninstall::Uninstall),

/// Print the path to installed Pact version
#[clap(name = "which", bin_name = "which")]
Which(commands::which::Which),
}

impl SubCommand {
Expand All @@ -86,6 +90,7 @@ impl SubCommand {
Self::Exec(cmd) => cmd.call(config),
Self::Uninstall(cmd) => cmd.call(config),
Self::Unalias(cmd) => cmd.call(config),
Self::Which(cmd) => cmd.call(config),
}
}
}
Expand Down
8 changes: 5 additions & 3 deletions src/commands/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,12 +133,11 @@ impl Command for Install {
// Automatically swap Apple Silicon to x64 arch for appropriate versions.
let version = &release.tag_name;
let safe_arch = get_safe_arch(&config.arch, version);
let version_str = format!("Pact {}", &version);
outln!(
config,
Info,
"Installing {} ({})",
version_str.cyan(),
format!("Pact {}", &version).cyan(),
safe_arch.to_string()
);
match install_pact_dist(
Expand All @@ -156,7 +155,10 @@ impl Command for Install {
};

if let UserVersion::Full(Version::Nightly(nightly_type)) = current_version {
let alias_name = Version::Nightly(nightly_type).v_str();
if nightly_type == "nightly" {
return Ok(());
}
let alias_name = "nightly".to_string();
debug!(
"Tagging {} as alias for {}",
alias_name.cyan(),
Expand Down
1 change: 1 addition & 0 deletions src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ pub mod ls_remote;
pub mod unalias;
pub mod uninstall;
pub mod r#use;
pub mod which;
1 change: 0 additions & 1 deletion src/commands/use.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ impl Command for Use {
})
.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");
Expand Down
65 changes: 65 additions & 0 deletions src/commands/which.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
use super::command::Command;
use crate::config::PactupConfig;
use crate::installed_versions;
use crate::user_version_reader::UserVersionReader;
use crate::version_file_strategy::VersionFileStrategy;
use colored::Colorize;
use thiserror::Error;

#[derive(clap::Parser, Debug)]
pub struct Which {
version: Option<UserVersionReader>,
}

impl Command for Which {
type Error = Error;

fn apply(self, config: &PactupConfig) -> Result<(), Self::Error> {
let all_versions = installed_versions::list(config.installations_dir())
.map_err(|source| Error::VersionListingError { source })?;
let requested_version = self
.version
.unwrap_or_else(|| {
let current_dir = std::env::current_dir().unwrap();
UserVersionReader::Path(current_dir)
})
.into_user_version(config)
.ok_or_else(|| match config.version_file_strategy() {
VersionFileStrategy::Local => InferVersionError::Local,
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());
} else {
let error_message = format!(
"Can't find an installed Pact version matching {}.",
requested_version.to_string().italic()
);
eprintln!("{}", error_message.red());
}
Ok(())
}
}

#[derive(Debug, Error)]
pub enum Error {
#[error("Can't get locally installed versions: {}", source)]
VersionListingError { source: installed_versions::Error },

#[error(transparent)]
CantInferVersion {
#[from]
source: InferVersionError,
},
}

#[derive(Debug, Error)]
pub enum InferVersionError {
#[error("Can't find version in dotfiles. Please provide a version manually to the command.")]
Local,
#[error("Could not find any version to use. Maybe you don't have a default version set?\nTry running `pactup default <VERSION>` to set one,\nor create a .pact-version file inside your project to declare a Pact version.")]
Recursive,
}

0 comments on commit 8038a4c

Please sign in to comment.