Skip to content

Commit

Permalink
Merge pull request #3271 from Bravo555/improve/log-level
Browse files Browse the repository at this point in the history
Add `--log-level` to `tedge-write` and `tedge-apt-plugin`
  • Loading branch information
Bravo555 authored Nov 29, 2024
2 parents f640f25 + 4742be8 commit 3b43124
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 11 deletions.
4 changes: 3 additions & 1 deletion Cargo.lock

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

1 change: 1 addition & 0 deletions crates/core/tedge_write/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ nix.workspace = true
path-clean.workspace = true
tedge_config.workspace = true
tedge_utils.workspace = true
tracing.workspace = true
uzers.workspace = true
which.workspace = true

Expand Down
16 changes: 16 additions & 0 deletions crates/core/tedge_write/src/bin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ use anyhow::bail;
use anyhow::Context;
use camino::Utf8PathBuf;
use clap::Parser;
use tedge_config::system_services::log_init;
use tedge_config::system_services::LogConfigArgs;
use tedge_utils::atomic::MaybePermissions;

/// tee-like helper for writing to files which `tedge` user does not have write permissions to.
Expand All @@ -30,9 +32,23 @@ pub struct Args {
/// Group which will become the new owner of the file.
#[arg(long)]
group: Option<Box<str>>,

#[command(flatten)]
log_args: LogConfigArgs,

/// [env: TEDGE_CONFIG_DIR, default: /etc/tedge]
#[clap(
long = "config-dir",
default_value = tedge_config::get_config_dir().into_os_string(),
hide_env_values = true,
hide_default_value = true,
)]
config_dir: Utf8PathBuf,
}

pub fn run(args: Args) -> anyhow::Result<()> {
log_init("tedge-write", &args.log_args, &args.config_dir)?;

// /etc/sudoers can contain rules where sudo permissions are given to `tedge` user depending on
// the files we write to, e.g:
//
Expand Down
3 changes: 2 additions & 1 deletion plugins/tedge_apt_plugin/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@ homepage = { workspace = true }
repository = { workspace = true }

[dependencies]
camino = { workspace = true }
clap = { workspace = true }
csv = { workspace = true }
log = { workspace = true }
regex = { workspace = true }
serde = { workspace = true, features = ["derive"] }
tedge_config = { workspace = true }
thiserror = { workspace = true }
tracing = { workspace = true }

[dev-dependencies]
anyhow = { workspace = true }
Expand Down
32 changes: 23 additions & 9 deletions plugins/tedge_apt_plugin/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,22 @@ mod module_check;

use crate::error::InternalError;
use crate::module_check::PackageMetadata;
use log::warn;
use camino::Utf8PathBuf;
use regex::Regex;
use serde::Deserialize;
use std::io::{self};
use std::path::PathBuf;
use std::io;
use std::path::Path;
use std::process::Command;
use std::process::ExitStatus;
use std::process::Stdio;
use tedge_config::get_config_dir;
use tedge_config::system_services::log_init;
use tedge_config::system_services::LogConfigArgs;
use tedge_config::AptConfig;
use tedge_config::TEdgeConfig;
use tedge_config::TEdgeConfigLocation;
use tracing::error;
use tracing::warn;

#[derive(clap::Parser, Debug)]
#[clap(
Expand All @@ -31,7 +35,10 @@ pub struct AptCli {
hide_env_values = true,
hide_default_value = true,
)]
config_dir: PathBuf,
config_dir: Utf8PathBuf,

#[command(flatten)]
log_args: LogConfigArgs,

#[clap(subcommand)]
operation: PluginOp,
Expand Down Expand Up @@ -93,6 +100,9 @@ struct SoftwareModuleUpdate {
}

fn run_op(apt: AptCli) -> Result<ExitStatus, InternalError> {
if let Err(err) = log_init("tedge-apt-plugin", &apt.log_args, &apt.config_dir) {
error!("Can't enable logging due to error: {err}");
}
let status = match apt.operation {
PluginOp::List { name, maintainer } => {
let dpkg_query = Command::new("dpkg-query")
Expand Down Expand Up @@ -144,7 +154,7 @@ fn run_op(apt: AptCli) -> Result<ExitStatus, InternalError> {
file_path,
} => {
let (installer, _metadata) = get_installer(module, version, file_path)?;
let dpk_option = get_dpk_option(apt.config_dir);
let dpk_option = get_dpk_option(apt.config_dir.as_std_path());
AptGetCmd::Install(dpk_option, vec![installer]).run()?
}

Expand All @@ -170,7 +180,7 @@ fn run_op(apt: AptCli) -> Result<ExitStatus, InternalError> {
// which will get cleaned up once it goes out of scope after this block
let mut metadata_vec = Vec::new();
let mut args: Vec<String> = Vec::new();
let dpk_option = get_dpk_option(apt.config_dir);
let dpk_option = get_dpk_option(apt.config_dir.as_std_path());

for update_module in updates {
match update_module.action {
Expand Down Expand Up @@ -316,7 +326,7 @@ impl AptGetCmd {
}
}

fn get_dpk_option(config_dir: PathBuf) -> AptConfig {
fn get_dpk_option(config_dir: &Path) -> AptConfig {
match get_config(config_dir) {
None => AptConfig::KeepNew,
Some(config) => config.apt.dpk.options.config.clone(),
Expand All @@ -331,7 +341,7 @@ fn get_name_and_version(line: &str) -> (&str, &str) {
(name, version)
}

fn get_config(config_dir: PathBuf) -> Option<TEdgeConfig> {
fn get_config(config_dir: &Path) -> Option<TEdgeConfig> {
let tedge_config_location = TEdgeConfigLocation::from_custom_root(config_dir);

match TEdgeConfig::try_new(tedge_config_location) {
Expand All @@ -354,7 +364,7 @@ pub fn run_and_exit(cli: Result<AptCli, clap::Error>) -> ! {
};

if let PluginOp::List { name, maintainer } = &mut apt.operation {
if let Some(config) = get_config(apt.config_dir.clone()) {
if let Some(config) = get_config(apt.config_dir.as_std_path()) {
if name.is_none() {
*name = config.apt.name.or_none().cloned();
}
Expand Down Expand Up @@ -412,6 +422,10 @@ mod tests {
let apt = AptCli {
config_dir: "".into(),
operation: filters,
log_args: LogConfigArgs {
debug: false,
log_level: None,
},
};
assert!(run_op(apt).is_ok())
}
Expand Down

0 comments on commit 3b43124

Please sign in to comment.