Skip to content

Commit

Permalink
refactor: successful move to use libroast
Browse files Browse the repository at this point in the history
  • Loading branch information
uncomfyhalomacro committed Oct 12, 2024
1 parent 7a2b1cf commit 8c2cda4
Show file tree
Hide file tree
Showing 8 changed files with 162 additions and 308 deletions.
361 changes: 149 additions & 212 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ default-members = [
resolver = "2"

[workspace.package]
version = "1.4.0"
version = "2.0.0"
description = "OBS Source Service and utilities for Rust software packaging."
authors = [
"Soc Virnyl Estela <[email protected]>",
Expand Down
1 change: 1 addition & 0 deletions bulk-updater/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ quick-xml = { workspace = true, features = ["overlapped-lists", "serialize"] }
obs-service-cargo = { path = "../cargo" }
clap = { workspace = true, features = ["derive"] }
serde = { workspace = true, features = ["derive", "alloc"] }
libroast = { git = "https://github.com/openSUSE-Rust/roast", version = "^3" }
tracing.workspace = true
tracing-subscriber.workspace = true
terminfo = "^0.8.0"
Expand Down
3 changes: 2 additions & 1 deletion bulk-updater/src/operations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
// file, You can obtain one at https://mozilla.org/MPL/2.0/.

use crate::services::{Service, Services};
use obs_service_cargo::cli::{Compression, Opts, Src, Vendor};
use libroast::common::Compression;
use obs_service_cargo::cli::{Opts, Src, Vendor};
use std::path::{Path, PathBuf};
use std::process;
use std::process::Output;
Expand Down
2 changes: 1 addition & 1 deletion cargo/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ xz2 = "0.1"
zstd = { version = "0.12", features = ["pkg-config", "zstdmt"] }
bzip2 = { version = "0.4" }
walkdir = "2.4"
libroast = { git = "https://github.com/openSUSE-Rust/roast", version = "1.1.1" }
libroast = { git = "https://github.com/openSUSE-Rust/roast", version = "^3" }

[lints]
workspace = true
96 changes: 5 additions & 91 deletions cargo/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,17 @@
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.

use std::fmt::{self, Display};
use std::io;
use std::path::{Path, PathBuf};

use crate::consts::{
BZ2_MIME, GZ_MIME, SUPPORTED_MIME_TYPES, VENDOR_PATH_PREFIX, XZ_MIME, ZST_MIME,
};
use crate::consts::VENDOR_PATH_PREFIX;
use crate::errors::OBSCargoError;
use crate::errors::OBSCargoErrorKind;
use crate::utils;
use libroast::common::{SupportedFormat, UnsupportedFormat};
use libroast::common::Compression;

use clap::{Parser, ValueEnum};
use infer;
use clap::Parser;
use libroast::decompress;

#[allow(unused_imports)]
Expand Down Expand Up @@ -83,58 +81,6 @@ impl AsRef<Opts> for Opts {
}
}

#[derive(ValueEnum, Default, Debug, Clone, Copy)]
pub enum Compression {
Gz,
Xz,
#[default]
Zst,
Bz2,
}

#[derive(Debug)]
pub enum SupportedFormat {
Compressed(Compression, PathBuf),
Dir(PathBuf),
}

impl Display for SupportedFormat {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let msg = match self {
SupportedFormat::Compressed(comp_type, src) => {
format!("Compression: {}, Src: {}", comp_type, src.display())
}
SupportedFormat::Dir(src) => format!("Directory: {}", src.display()),
};
write!(f, "{}", msg)
}
}

#[derive(Debug)]
pub struct UnsupportedFormat {
pub ext: String,
}

impl Display for UnsupportedFormat {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Unsupported archive format {}", self.ext)
}
}

impl std::error::Error for UnsupportedFormat {}

impl Display for Compression {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let msg = match self {
Compression::Gz => "gz",
Compression::Xz => "xz",
Compression::Zst => "zst",
Compression::Bz2 => "bz2",
};
write!(f, "{}", msg)
}
}

#[derive(clap::Args, Debug, Clone)]
pub struct Src {
#[arg(
Expand Down Expand Up @@ -170,39 +116,7 @@ impl Vendor for Src {
if let Ok(actual_src) = utils::process_globs(&self.src) {
debug!(?actual_src, "Source got from glob pattern");
if actual_src.is_file() {
match infer::get_from_path(&actual_src) {
Ok(kind) => match kind {
Some(known) => {
if SUPPORTED_MIME_TYPES.contains(&known.mime_type()) {
trace!(?known);
if known.mime_type().eq(GZ_MIME) {
Ok(SupportedFormat::Compressed(Compression::Gz, actual_src))
} else if known.mime_type().eq(XZ_MIME) {
Ok(SupportedFormat::Compressed(Compression::Xz, actual_src))
} else if known.mime_type().eq(ZST_MIME) {
Ok(SupportedFormat::Compressed(Compression::Zst, actual_src))
} else if known.mime_type().eq(BZ2_MIME) {
Ok(SupportedFormat::Compressed(Compression::Bz2, actual_src))
} else {
unreachable!()
}
} else {
Err(UnsupportedFormat {
ext: known.mime_type().to_string(),
})
}
}
None => Err(UnsupportedFormat {
ext: "`File type is not known`".to_string(),
}),
},
Err(err) => {
error!(?err);
Err(UnsupportedFormat {
ext: "`Cannot read file`".to_string(),
})
}
}
libroast::is_supported_format(&actual_src)
} else {
Ok(SupportedFormat::Dir(actual_src))
}
Expand Down
3 changes: 2 additions & 1 deletion cargo/src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@ use std::io;
use std::path::Path;
use std::path::PathBuf;

use crate::cli::{Compression, Opts};
use crate::cli::Opts;
use crate::errors::OBSCargoError;
use crate::errors::OBSCargoErrorKind;
use crate::vendor::{self, generate_lockfile, vendor};

use crate::audit::{perform_cargo_audit, process_reports};

use glob::glob;
use libroast::common::Compression;
#[allow(unused_imports)]
use tracing::{debug, error, info, trace, warn, Level};

Expand Down
2 changes: 1 addition & 1 deletion cargo/src/vendor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use std::fs;
use std::io::Write;
use std::path::Path;

use crate::cli::Compression;
use libroast::common::Compression;
use crate::errors::OBSCargoError;
use crate::errors::OBSCargoErrorKind;
use crate::utils::cargo_command;
Expand Down

0 comments on commit 8c2cda4

Please sign in to comment.