Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Add --force option to force install version #30

Merged
merged 1 commit into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/heavy-spoons-cry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"pactup": patch
---

Add force install option and fix pact 5 permissions
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"rust-analyzer.check.command": "clippy"
}
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ env_logger = "0.11"
indoc = "2.0"
log = "0.4"
node-semver = "2.1"
sysinfo = "0.30"
sysinfo = "0.31"
tar = "0.4"
tempfile = "3.6"
thiserror = "1.0"
Expand Down
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"url": "[email protected]:kadena-community/pactup.git"
},
"author": "Salama Ashoush <[email protected]>",
"packageManager": "pnpm@9.7.0",
"packageManager": "pnpm@9.10.0",
"license": "MIT",
"description": "Linter for the JavaScript Oxidation Compiler",
"keywords": [
Expand Down Expand Up @@ -44,19 +44,19 @@
},
"devDependencies": {
"@changesets/changelog-github": "0.5.0",
"@changesets/cli": "2.27.7",
"@types/node": "^22.1.0",
"@changesets/cli": "2.27.8",
"@types/node": "^22.5.4",
"@types/shell-escape": "^0.2.3",
"chalk": "^5.3.0",
"cmd-ts": "0.13.0",
"cross-env": "^7.0.3",
"execa": "9.3.0",
"execa": "9.3.1",
"lerna-changelog": "2.2.0",
"prettier": "3.3.3",
"pv": "1.0.1",
"shell-escape": "^0.2.0",
"svg-term-cli": "2.1.1",
"tsx": "^4.16.5",
"typescript": "^5.5.4"
"tsx": "^4.19.0",
"typescript": "^5.6.2"
}
}
550 changes: 236 additions & 314 deletions pnpm-lock.yaml

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions src/archive/extract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ impl From<crate::http::Error> for Error {
}
}

impl From<walkdir::Error> for Error {
fn from(err: walkdir::Error) -> Self {
Self::IoError(err.into())
}
}
pub trait Extract {
fn extract_into<P: AsRef<Path>>(self, path: P) -> Result<(), Error>;
}
Expand Down
54 changes: 53 additions & 1 deletion src/archive/tar_gz.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
use super::extract::{Error, Extract};

use std::{io::Read, path::Path};

#[cfg(unix)]
use std::os::unix::fs::PermissionsExt;

pub struct TarGz<R: Read> {
response: R,
}
Expand All @@ -16,7 +20,55 @@ impl<R: Read> Extract for TarGz<R> {
fn extract_into<P: AsRef<Path>>(self, path: P) -> Result<(), Error> {
let gz_stream = flate2::read::GzDecoder::new(self.response);
let mut tar_archive = tar::Archive::new(gz_stream);
tar_archive.unpack(path)?;
tar_archive.set_preserve_permissions(false);
tar_archive.set_preserve_ownerships(false);
tar_archive.set_overwrite(true);
// First, extract everything, even if the permissions are restrictive
tar_archive.unpack(&path)?;

// Now recursively set permissions for all directories and files
fix_permissions_recursively(path.as_ref())?;

Ok(())
}
}

// Helper function to recursively fix permissions cross-platform
fn fix_permissions_recursively<P: AsRef<Path>>(path: P) -> Result<(), Error> {
// Iterate over all files and directories recursively
for entry in walkdir::WalkDir::new(path) {
let entry = entry?;
let path = entry.path();

// Set permissions for Unix-like systems (Linux, macOS)
#[cfg(unix)]
{
// Set permissions for directories
if entry.file_type().is_dir() {
let mut dir_permissions = std::fs::metadata(path)?.permissions();
dir_permissions.set_mode(0o755); // Default directory permissions (rwxr-xr-x)
std::fs::set_permissions(path, dir_permissions)?;
}
// Set permissions for regular files, preserving executable bits for all
else if entry.file_type().is_file() {
let metadata = std::fs::metadata(path)?;
let mut file_permissions = metadata.permissions();

// Default file permissions (rw-r--r--)
let mut mode = 0o644;

// Check if the file is executable (user, group, or others)
if file_permissions.mode() & 0o111 != 0 {
// If any of the executable bits are set, keep them for user, group, and others
mode |= 0o111; // Retain all executable bits (for user, group, and others)
}

// Apply the computed mode
file_permissions.set_mode(mode);
std::fs::set_permissions(path, file_permissions)?;
}
}
}

Ok(())
}
12 changes: 10 additions & 2 deletions src/commands/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,14 @@ pub struct Install {
#[clap(long, conflicts_with_all = &["version", "nightly"])]
pub latest: bool,

/// Show an interactive progress bar for the download
/// status.
/// Show an interactive progress bar for the download status.
#[clap(long, default_value_t)]
#[arg(value_enum)]
pub progress: ProgressConfig,

/// force install even if the version is already installed
#[clap(long)]
pub force: bool,
}

impl Install {
Expand Down Expand Up @@ -66,6 +69,7 @@ impl Command for Install {
fn apply(self, config: &PactupConfig) -> Result<(), Self::Error> {
let current_dir = std::env::current_dir().unwrap();
let show_progress = self.progress.enabled(config);
let force_install = self.force;
let current_version = self
.version()?
.or_else(|| get_user_version_for_directory(current_dir, config))
Expand Down Expand Up @@ -144,6 +148,7 @@ impl Command for Install {
config.installations_dir(),
&config.arch,
show_progress,
force_install,
) {
Err(err @ DownloaderError::VersionAlreadyInstalled { .. }) => {
outln!(config, Error, "{} {}", "warning:".bold().yellow(), err);
Expand Down Expand Up @@ -229,6 +234,7 @@ mod tests {
version: UserVersion::from_str("4.11.0").ok(),
nightly: false,
latest: false,
force: false,
progress: ProgressConfig::Never,
}
.apply(&config)
Expand Down Expand Up @@ -256,6 +262,7 @@ mod tests {
version: None,
nightly: false,
latest: true,
force: false,
progress: ProgressConfig::Never,
}
.apply(&config)
Expand All @@ -282,6 +289,7 @@ mod tests {
version: None,
nightly: true,
latest: false,
force: false,
progress: ProgressConfig::Never,
}
.apply(&config)
Expand Down
19 changes: 14 additions & 5 deletions src/commands/ls_remote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub struct LsRemote {
#[arg(long)]
filter: Option<UserVersion>,

/// Show nightly versions
/// Include nightly versions
#[arg(long)]
nightly: bool,

Expand Down Expand Up @@ -47,15 +47,24 @@ impl super::command::Command for LsRemote {
} else {
remote_pact_index::list(&config.pact_4x_repo)?
};
// print latest version
if self.latest {
let latest = if self.nightly {
all_versions
.iter()
.find(|v| v.tag_name.is_nightly())
.unwrap()
} else {
&all_versions[0]
};
println!("{}", latest.tag_name);
return Ok(());
}

if let Some(filter) = &self.filter {
all_versions.retain(|v| filter.matches(&v.tag_name, config));
}

if self.latest {
all_versions.drain(0..all_versions.len() - 1);
}

if let SortingMethod::Descending = self.sort {
all_versions.reverse();
}
Expand Down
33 changes: 23 additions & 10 deletions src/downloader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,21 +57,34 @@ pub fn install_pact_dist<P: AsRef<Path>>(
installations_dir: P,
arch: &Arch,
show_progress: bool,
force: bool,
) -> Result<(), Error> {
let installation_dir = PathBuf::from(installations_dir.as_ref()).join(version.v_str());

if installation_dir.exists() {
return Err(Error::VersionAlreadyInstalled {
path: installation_dir,
});
let version_installation_dir = PathBuf::from(installations_dir.as_ref()).join(version.v_str());

if version_installation_dir.exists() {
if force {
debug!("Removing directory {:?}", version_installation_dir);
std::fs::remove_dir_all(&version_installation_dir)?;
} else {
return Err(Error::VersionAlreadyInstalled {
path: version_installation_dir,
});
}
}
if !installations_dir.as_ref().exists() {
debug!("Creating directory {:?}", installations_dir.as_ref());
std::fs::create_dir_all(installations_dir.as_ref())?;
}

std::fs::create_dir_all(installations_dir.as_ref())?;

let temp_installations_dir = installations_dir.as_ref().join(".downloads");
if temp_installations_dir.exists() {
debug!("Removing directory {:?}", temp_installations_dir);
std::fs::remove_dir_all(&temp_installations_dir)?;
}
std::fs::create_dir_all(&temp_installations_dir)?;

let portal = DirectoryPortal::new_in(&temp_installations_dir, installation_dir);
debug!("Creating directory portal");
let portal = DirectoryPortal::new_in(&temp_installations_dir, version_installation_dir);

debug!("Going to call for {}", download_url);
let response = crate::http::get(download_url.as_str())?;
Expand Down Expand Up @@ -144,7 +157,7 @@ mod tests {
"https://github.com/kadena-io/pact/releases/download/v4.11.0/pact-4.11.0-linux-20.04.zip",
)
.unwrap();
install_pact_dist(&version, &pact_dist_mirror, path, &arch, false)
install_pact_dist(&version, &pact_dist_mirror, path, &arch, false, false)
.expect("Can't install Pact 4.11.0");

let mut location_path = path.join(version.v_str());
Expand Down