Skip to content

Commit

Permalink
fix: update logging
Browse files Browse the repository at this point in the history
  • Loading branch information
innobead committed Jan 2, 2025
1 parent bce26f1 commit 4aeb7e6
Show file tree
Hide file tree
Showing 25 changed files with 301 additions and 281 deletions.
2 changes: 0 additions & 2 deletions huber-common/src/model/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,13 +176,11 @@ impl ConfigPath for Config {

fn installed_pkg_dir(&self, pkg: &Package, version: &str) -> anyhow::Result<PathBuf> {
let version = pkg.parse_version_from_tag_name(&version.to_string())?;

dir(self.installed_pkg_base_dir(&pkg)?.join(version))
}

fn installed_pkg_bin_dir(&self, pkg: &Package, version: &str) -> anyhow::Result<PathBuf> {
let version = pkg.parse_version_from_tag_name(&version.to_string())?;

dir(self.installed_pkg_dir(pkg, &version)?.join("bin"))
}

Expand Down
133 changes: 48 additions & 85 deletions huber-common/src/model/package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use std::str::FromStr;
use std::{env, fmt};

use anyhow::anyhow;
use log::{error, warn};
use regex::Regex;
use semver::Version;
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -162,120 +161,84 @@ impl PackageSource {

impl Package {
pub fn target(&self) -> anyhow::Result<PackageManagement> {
// https://doc.rust-lang.org/std/env/consts/index.html
let os = env::consts::OS;
let arch = env::consts::ARCH;
let e = anyhow!("Unsupported OS {} or ARCH {}", os, arch);

let default_pkg_mgmt: Option<PackageManagement> = self
.targets
.iter()
.find(|it| {
if let PackageTargetType::Default(_m) = it {
return true;
}
false
})
.map(|it| match it {
PackageTargetType::Default(m) => Some(m.clone()),
_ => None,
})
.flatten();

let default_pkg_mgmt: Option<_> = self.targets.iter().find_map(|it| match it {
PackageTargetType::Default(m) => Some(m.clone()),
_ => None,
});

self.get_package_management(os, arch, default_pkg_mgmt)
.ok_or(anyhow!("Unsupported OS {} or ARCH {}", os, arch))
}

fn get_package_management(
&self,
os: &str,
arch: &str,
default_pkg_mgmt: Option<PackageManagement>,
) -> Option<PackageManagement> {
match os {
"linux" => match arch {
"x86_64" => self.targets.iter().find_map(|it| {
if let PackageTargetType::LinuxAmd64(m) = it {
Some(m.clone())
} else {
default_pkg_mgmt.clone()
}
"x86_64" => self.targets.iter().find_map(|it| match it {
PackageTargetType::LinuxAmd64(m) => Some(m.clone()),
_ => default_pkg_mgmt.clone(),
}),

"aarch64" => self.targets.iter().find_map(|it| {
if let PackageTargetType::LinuxArm64(m) = it {
Some(m.clone())
} else {
default_pkg_mgmt.clone()
}
"aarch64" => self.targets.iter().find_map(|it| match it {
PackageTargetType::LinuxArm64(m) => Some(m.clone()),
_ => default_pkg_mgmt.clone(),
}),

_ => None,
},

"macos" => match arch {
"x86_64" => self.targets.iter().find_map(|it| {
if let PackageTargetType::MacOSAmd64(m) = it {
Some(m.clone())
} else {
default_pkg_mgmt.clone()
}
"x86_64" => self.targets.iter().find_map(|it| match it {
PackageTargetType::MacOSAmd64(m) => Some(m.clone()),
_ => default_pkg_mgmt.clone(),
}),

"aarch64" => self.targets.iter().find_map(|it| {
if let PackageTargetType::MacOSArm64(m) = it {
Some(m.clone())
} else {
default_pkg_mgmt.clone()
}
"aarch64" => self.targets.iter().find_map(|it| match it {
PackageTargetType::MacOSArm64(m) => Some(m.clone()),
_ => default_pkg_mgmt.clone(),
}),

_ => None,
},

"windows" => match arch {
"x86_64" => self.targets.iter().find_map(|it| {
if let PackageTargetType::WindowsAmd64(m) = it {
Some(m.clone())
} else {
default_pkg_mgmt.clone()
}
"x86_64" => self.targets.iter().find_map(|it| match it {
PackageTargetType::WindowsAmd64(m) => Some(m.clone()),
_ => default_pkg_mgmt.clone(),
}),

_ => None,
},

_ => None,
}
.ok_or(e)
}

pub fn parse_version_from_tag_name(&self, tag_name: &String) -> anyhow::Result<String> {
let version = if let Some(ref str) = self.target()?.tag_version_regex_template {
let mut version = String::new();
let regex = Regex::new(&format!(r"{}", str)).unwrap();

if let Some(c) = regex.captures(tag_name) {
if let Some(m) = c.get(1) {
version = m.as_str().to_string();
} else {
error!(
"Failed to capture the version from {} due to the missing captured group 1",
tag_name.clone()
);
}
}
let mut version = tag_name.clone();

if !version.is_empty() {
version
} else {
if Version::parse(tag_name.trim_start_matches("v")).is_ok() {
warn!(
"Failed to capture the version from {}, because the tag name is a qualified version",
tag_name
);
if let Some(ref template) = self.target()?.tag_version_regex_template {
let regex = Regex::new(&format!(r"{}", template))?;

tag_name.clone()
if let Some(capture) = regex.captures(tag_name) {
if let Some(m) = capture.get(1) {
version = m.as_str().to_string();
} else {
return Err(anyhow!(
"No qualified version captured from tag name. {}",
tag_name
"Failed to capture the version from {} via tag_version_regex_template {}",
tag_name,
template
));
}
}
} else {
tag_name.clone()
};

if Version::parse(version.trim_start_matches("v")).is_err() {
return Err(anyhow!(
"Failed to parse the version {} from tag_name {}",
version,
tag_name
));
}
}

Ok(version)
}
Expand Down
52 changes: 28 additions & 24 deletions huber/src/bin/huber.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::path::PathBuf;
use std::process::exit;
use std::str::FromStr;
use std::sync::Arc;

Expand All @@ -18,7 +19,7 @@ use huber::service::update::HuberUpdateService;
use huber_common::log::Logger;
use huber_common::model::config::Config;
use libcli_rs::output::OutputFormat;
use log::LevelFilter;
use log::{error, LevelFilter};
use simpledi_rs::di::{DIContainer, DIContainerTrait, DependencyInjectTrait};
use simpledi_rs::{create_dep, inject_dep};

Expand Down Expand Up @@ -87,38 +88,41 @@ struct Cli {
}

#[tokio::main]
async fn main() -> anyhow::Result<()> {
async fn main() {
let cli = Cli::parse();

let (config, container) = init(&cli);

match &cli.command {
Commands::Install(args) => args.run(&config, &container).await?,
let result = match &cli.command {
Commands::Install(args) => args.run(&config, &container).await,
Commands::Config(args) => match args.command {
ConfigCommands::Show(ref args) => args.run(&config, &container).await?,
ConfigCommands::Save(ref args) => args.run(&config, &container).await?,
ConfigCommands::Show(ref args) => args.run(&config, &container).await,
ConfigCommands::Save(ref args) => args.run(&config, &container).await,
},
Commands::Repo(args) => match args.command {
RepoCommands::Add(ref args) => args.run(&config, &container).await?,
RepoCommands::Remove(ref args) => args.run(&config, &container).await?,
RepoCommands::List(ref args) => args.run(&config, &container).await?,
RepoCommands::Add(ref args) => args.run(&config, &container).await,
RepoCommands::Remove(ref args) => args.run(&config, &container).await,
RepoCommands::List(ref args) => args.run(&config, &container).await,
},
Commands::Current(args) => args.run(&config, &container).await?,
Commands::Flush(args) => args.run(&config, &container).await?,
Commands::Info(args) => args.run(&config, &container).await?,
Commands::Reset(args) => args.run(&config, &container).await?,
Commands::Search(args) => args.run(&config, &container).await?,
Commands::SelfUpdate(args) => args.run(&config, &container).await?,
Commands::Show(args) => args.run(&config, &container).await?,
Commands::Uninstall(args) => args.run(&config, &container).await?,
Commands::Update(args) => args.run(&config, &container).await?,
Commands::Save(args) => args.run(&config, &container).await?,
Commands::Load(args) => args.run(&config, &container).await?,
Commands::Lock(args) => args.run(&config, &container).await?,
Commands::Unlock(args) => args.run(&config, &container).await?,
}
Commands::Current(args) => args.run(&config, &container).await,
Commands::Flush(args) => args.run(&config, &container).await,
Commands::Info(args) => args.run(&config, &container).await,
Commands::Reset(args) => args.run(&config, &container).await,
Commands::Search(args) => args.run(&config, &container).await,
Commands::SelfUpdate(args) => args.run(&config, &container).await,
Commands::Show(args) => args.run(&config, &container).await,
Commands::Uninstall(args) => args.run(&config, &container).await,
Commands::Update(args) => args.run(&config, &container).await,
Commands::Save(args) => args.run(&config, &container).await,
Commands::Load(args) => args.run(&config, &container).await,
Commands::Lock(args) => args.run(&config, &container).await,
Commands::Unlock(args) => args.run(&config, &container).await,
};

Ok(())
if let Err(e) = result {
error!("{}", e);
exit(1);
}
}

fn init(cli: &Cli) -> (Config, Arc<DIContainer>) {
Expand Down
7 changes: 3 additions & 4 deletions huber/src/cmd/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use huber_common::model::config::{Config, ConfigPath};
use huber_procmacro::process_lock;
use libcli_rs::output;
use libcli_rs::output::{OutputFactory, OutputTrait};
use log::{debug, info};
use log::info;
use simpledi_rs::di::{DIContainer, DIContainerTrait};

use crate::cmd::CommandTrait;
Expand All @@ -30,8 +30,6 @@ impl CommandTrait for ConfigShowArgs {
let config_service = container.get::<ConfigService>().unwrap();
let saved_config = config_service.get()?;

debug!("Runtime config: {:?}", config);

output!(
config.output_format,
.display(
Expand All @@ -56,8 +54,9 @@ impl CommandTrait for ConfigSaveArgs {

process_lock!(lock_file);

info!("Updating {:?}: {:#?}", config_path, config);
info!("Saving config to {:?}: {:#?}", config_path, config);
config_service.update(config)?;
info!("Config saved");

Ok(())
}
Expand Down
14 changes: 12 additions & 2 deletions huber/src/cmd/current.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,20 @@ impl CommandTrait for CurrentArgs {
.into_iter()
.find(|it| it.version == version.to_string())
{
info!("Updating the current version of {}: {}", name, r);
info!("Updating the current version of {} to {}", name, version);
release_service.set_current(&mut r).await?;
info!("{}@{} is now the current version", name, version);
} else {
return Err(anyhow!("Package {}@{} not found", pkg.name, version));
info!(
"No version provided, trying to find the latest version of {}",
name
);
let mut release = release_service.get_latest(&pkg).await?;
release_service.set_current(&mut release).await?;
info!(
"{}@{} is now the current version",
release.package.name, release.version
);
}
}

Expand Down
2 changes: 2 additions & 0 deletions huber/src/cmd/flush.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ impl CommandTrait for FlushArgs {
if !r.current {
info!("Removing {}", r);
release_service.delete_release(&r)?;
info!("{} removed", r);

flushed_releases.push(r);
}
}
Expand Down
Loading

0 comments on commit 4aeb7e6

Please sign in to comment.