Skip to content

Commit

Permalink
feat: lock and unlock commands
Browse files Browse the repository at this point in the history
  • Loading branch information
innobead committed Jan 8, 2025
1 parent efbdb12 commit b3deb65
Show file tree
Hide file tree
Showing 34 changed files with 858 additions and 145 deletions.
69 changes: 68 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,5 @@ fs2 = "0.4.3"
clap_complete = "4.5.40"
thiserror = "2.0.9"
scopeguard = "1.1.0"
better-panic = "0.3.0"
filepath = "0.2.0"
30 changes: 20 additions & 10 deletions huber-common/src/model/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub struct Config {
pub github_token: Option<String>,
pub github_key: Option<String>,
pub github_base_uri: Option<String>,
pub lock_pkg_versions: HashMap<String, Vec<String>>,
pub lock_pkg_versions: HashMap<String, String>,
}

impl Config {
Expand All @@ -32,17 +32,27 @@ impl Config {
github_token: Option<String>,
github_key: Option<String>,
github_base_uri: Option<String>,
lock_pkg_versions: HashMap<String, Vec<String>>,
lock_pkg_versions: HashMap<String, String>,
) -> Self {
Self {
log_level,
output_format,
huber_dir: dir(huber_dir).unwrap(),
github_token,
github_key,
github_base_uri,
lock_pkg_versions,
let mut config = Config::default();

config.log_level = log_level;
config.output_format = output_format;
config.huber_dir = huber_dir;
if let Some(token) = github_token {
config.github_token = Some(token);
}
if let Some(key) = github_key {
config.github_key = Some(key);
}
if let Some(uri) = github_base_uri {
config.github_base_uri = Some(uri);
}
if !lock_pkg_versions.is_empty() {
config.lock_pkg_versions = lock_pkg_versions;
}

config
}
}

Expand Down
6 changes: 3 additions & 3 deletions huber-procmacro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub fn process_lock(input: TokenStream) -> TokenStream {
use huber_common::model::config::Config;
use std::fs::File;
use fs2::FileExt;
use log::{error, info};
use log::debug;
use anyhow::anyhow;

let lock_path = #lock_file_pathbuf_expr;
Expand All @@ -25,11 +25,11 @@ pub fn process_lock(input: TokenStream) -> TokenStream {
let r = f.try_lock_exclusive();
match r {
Ok(_) => {
info!("{}: {:?}", "Locking the operation", lock_path);
debug!("{}: {:?}", "Locking the operation", lock_path);
},

Err(e) => {
error!("{:?}: {:?}", lock_path, e);
debug!("{:?}: {:?}", lock_path, e);
return Err(anyhow!("huber is already running by another process for the exclusion operation. Please try after the operation finished. {:?}", e))
}
}
Expand Down
2 changes: 2 additions & 0 deletions huber/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,13 @@ rstest = "0.24.0"
[dependencies]
anyhow.workspace = true
async-trait.workspace = true
better-panic.workspace = true
chrono.workspace = true
clap.workspace = true
clap_complete.workspace = true
compress-tools.workspace = true
dirs.workspace = true
filepath.workspace = true
fs2.workspace = true
fs_extra.workspace = true
futures.workspace = true
Expand Down
11 changes: 3 additions & 8 deletions huber/src/bin/huber.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,19 +140,14 @@ async fn main() {
if let Some(e) = e.downcast_ref::<Error>() {
error!("{}", e);
} else if let Some(e) = e.downcast_ref::<HuberError>() {
match e {
HuberError::ConfigNotFound(_) => {
warn!(
"Config not found, please run `huber config save` to create a new one \
if want to persist the configuration"
);
}
}
warn!("{}", e);
}
}
}

fn init(cli: &Cli) -> (Config, Arc<DIContainer>) {
better_panic::install();

let config = Config::new(
cli.log_level.to_string(),
OutputFormat::from_str(&cli.output_format).unwrap(),
Expand Down
12 changes: 5 additions & 7 deletions huber/src/cmd/current.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ use log::info;
use simpledi_rs::di::{DIContainer, DIContainerTrait};

use crate::cmd::CommandTrait;
use crate::opt::parse_pkg_name_version;
use crate::error::HuberError::PackageNotInstalled;
use crate::opt::parse_pkg_name_semver;
use crate::service::package::PackageService;
use crate::service::release::{ReleaseAsyncTrait, ReleaseService};
use crate::service::{ItemOperationAsyncTrait, ItemOperationTrait};
Expand All @@ -17,24 +18,21 @@ pub struct CurrentArgs {
help = "Package name with version (e.g. 'package-name@version')",
num_args = 1,
value_hint = ValueHint::Unknown,
value_parser = parse_pkg_name_version
value_parser = parse_pkg_name_semver,
required = true,
)]
name_version: Vec<(String, String)>,
}

#[async_trait]
impl CommandTrait for CurrentArgs {
async fn run(&self, _: &Config, container: &DIContainer) -> anyhow::Result<()> {
if self.name_version.is_empty() {
return Err(anyhow!("No package name with version provided"));
}

let release_service = container.get::<ReleaseService>().unwrap();
let pkg_service = container.get::<PackageService>().unwrap();

for (name, version) in self.name_version.iter() {
if !pkg_service.has(name)? {
return Err(anyhow!("{} not installed", name));
return Err(anyhow!(PackageNotInstalled(name.clone())));
}

let pkg = pkg_service.get(name)?;
Expand Down
24 changes: 13 additions & 11 deletions huber/src/cmd/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ use simpledi_rs::di::{DIContainer, DIContainerTrait};
use tokio::task::JoinHandle;

use crate::cmd::CommandTrait;
use crate::error::HuberError::PackageNotFound;
use crate::opt::parse_pkg_name_semver;
use crate::service::cache::{CacheAsyncTrait, CacheService};
use crate::service::package::PackageService;
use crate::service::release::ReleaseService;
Expand All @@ -19,14 +21,15 @@ pub struct InstallArgs {
#[arg(
help = "Package name (e.g. 'package-name' or 'package-name@version')",
num_args = 1,
value_hint = ValueHint::Unknown
required = true,
value_parser = parse_pkg_name_semver,
value_hint = ValueHint::Unknown,
)]
name_version: Vec<String>,
name_version: Vec<(String, String)>,

#[arg(
help = "Set the installed package as current",
long,
num_args = 1,
value_hint = ValueHint::Unknown
)]
current: bool,
Expand All @@ -41,8 +44,7 @@ impl CommandTrait for InstallArgs {
let cache_service = container.get::<CacheService>().unwrap();
cache_service.update_repositories().await?;

let versions: Vec<_> = parse_package_name_versions(&self.name_version);
install_packages(release_service, pkg_service, versions).await?;
install_packages(release_service, pkg_service, &self.name_version).await?;

Ok(())
}
Expand All @@ -64,21 +66,21 @@ pub fn parse_package_name_versions(name_versions: &[String]) -> Vec<(String, Str
pub async fn install_packages(
release_service: Arc<ReleaseService>,
pkg_service: Arc<PackageService>,
versions: Vec<(String, String)>,
pkg_versions: &Vec<(String, String)>,
) -> anyhow::Result<()> {
for (name, _) in versions.iter() {
if !pkg_service.has(name)? {
return Err(anyhow!("{} package not found", name));
for (pkg, _) in pkg_versions.iter() {
if !pkg_service.has(pkg)? {
return Err(anyhow!(PackageNotFound(pkg.clone())));
}
}

let mut join_handles = vec![];
for (name, version) in versions.clone().into_iter() {
for (pkg, version) in pkg_versions.clone().into_iter() {
let pkg_service = pkg_service.clone();
let release_service = release_service.clone();

let handle: JoinHandle<anyhow::Result<()>> = tokio::spawn(async move {
let mut pkg = pkg_service.get(&name)?;
let mut pkg = pkg_service.get(&pkg)?;
pkg.version = if version.is_empty() {
None
} else {
Expand Down
6 changes: 5 additions & 1 deletion huber/src/cmd/load.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ pub struct LoadArgs {
help = "Load a package list to install",
long,
num_args = 1,
default_value = "huber-packages.txt",
value_hint = ValueHint::Unknown
)]
file: String,
Expand All @@ -35,15 +36,18 @@ impl CommandTrait for LoadArgs {
cache_service.update_repositories().await?;

info!("Loading packages from {}", self.file);

let file = File::open(&self.file)?;
let reader = BufReader::new(file);
let versions: Vec<_> = reader.lines().map_while(Result::ok).collect();
let count = versions.len();

info!("Loaded packages: total {}: {:#?}", count, versions);
info!("Installing packages: total {}", count);

let versions: Vec<_> = parse_package_name_versions(&versions);
install_packages(release_service, pkg_service, versions).await?;
install_packages(release_service, pkg_service, &versions).await?;

info!("Installed packages: total {}", count);

Ok(())
Expand Down
Loading

0 comments on commit b3deb65

Please sign in to comment.