Skip to content
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/).
- The metadata repositories can now contain source files.
- Support for unpacking bzip2 file formats.
- The `msvc` requirement now supports specifiying version bounds using `msvc@<version-bounds>`.
- Add support for path inputs in the `checksum` command.

### Changes
- The test environment now uses a fresh environment for each test and can be used in parallel.
Expand Down
2 changes: 1 addition & 1 deletion docs/commands/util.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ The `--exclude` flag specifies packages to exclude when using the `--all` flag.
The `checksum` command has the following syntax:<br>
`pit util checksum <URL>`

The command calculates the checksum of the file at the given URL. It also shows the size of the downloaded file in bytes.
The command calculates the checksum of the file at the given URL or system path. It also shows the size of the downloaded file in bytes.

## Portable repository
The `portable-repo` command has the following syntax:<br>
Expand Down
45 changes: 30 additions & 15 deletions src/cli/commands/util/checksum.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: GPL-3.0-only
use std::process::exit;
use std::{fs, path::PathBuf, process::exit, str::FromStr};

use clap::Args;
use url::Url;
Expand All @@ -10,24 +10,38 @@ use crate::{
display::{Spinner, logging::error},
},
repositories::types::Checksum,
utils::requests,
utils::{ioerror::IOResultExt, requests, unwrap_or_exit::UnwrapOrExit},
};

/// Calculates the checksum for the file at the given url.
#[derive(Args, Debug)]
pub struct ChecksumArgs {
/// The url to request the file from
url: Url,
/// The url or path to request the file from
url: String,
}

impl HandleCommand for ChecksumArgs {
fn handle(&self) {
println!("Trying to request file from '{}'", self.url);
let bytes = match Url::from_str(&self.url) {
Ok(url) => self.get_bytes_from_url(url),
Err(_) => self.get_bytes_from_path(&PathBuf::from(self.url.clone())),
};

let checksum = Checksum::from_bytes(&bytes);

let spinner = Spinner::new("Downloading file".into());
println!("Checksum: {}", checksum);
println!("File size: {}", bytes.len());
}
}

impl ChecksumArgs {
/// Gets the bytes from a given url.
fn get_bytes_from_url(&self, url: Url) -> Vec<u8> {
let message = format!("Downloading file from {}", self.url);
let spinner = Spinner::new(message);
spinner.show();

let response = match requests::get(self.url.as_str()) {
let response = match requests::get(url.as_str()) {
Ok(response) => response,
Err(e) => {
error!(e, "Unable to request file");
Expand All @@ -40,18 +54,19 @@ impl HandleCommand for ChecksumArgs {
exit(1);
}

let bytes = match response.bytes() {
Ok(bytes) => bytes,
spinner.finish();

match response.bytes() {
Ok(bytes) => bytes.into(),
Err(e) => {
error!(e, "Unable to get file bytes");
exit(1);
},
};

let checksum = Checksum::from_bytes(&bytes);
}
}

spinner.finish();
println!("Checksum: {}", checksum);
println!("File size: {}", bytes.len());
/// Gets the bytes from a given path.
fn get_bytes_from_path(&self, path: &PathBuf) -> Vec<u8> {
fs::read(path).err_with_path("read", path).unwrap_or_exit(1)
}
}
Loading