From f9a368d486abefdfedf24b2f922949971fb04dae Mon Sep 17 00:00:00 2001 From: Michal Filka Date: Fri, 27 Oct 2023 09:46:41 +0200 Subject: [PATCH 01/18] Logs destination option for agama logs store --- rust/agama-cli/src/logs.rs | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/rust/agama-cli/src/logs.rs b/rust/agama-cli/src/logs.rs index 18f7e246cb..a2ce44564b 100644 --- a/rust/agama-cli/src/logs.rs +++ b/rust/agama-cli/src/logs.rs @@ -19,6 +19,9 @@ pub enum LogsCommands { #[clap(long, short = 'v')] /// Verbose output verbose: bool, + #[clap(long, short = 'd')] + /// Destination path + dest: Option, }, /// List logs which will be collected List, @@ -27,11 +30,13 @@ pub enum LogsCommands { // main entry point called from agama CLI main loop pub async fn run(subcommand: LogsCommands) -> anyhow::Result<()> { match subcommand { - LogsCommands::Store { verbose } => { + LogsCommands::Store { verbose, dest } => { // feed internal options structure by what was received from user // for now we always use / add defaults if any + let dest = dest.map_or(PathBuf::from(DEFAULT_RESULT), |d| d); let options = LogOptions { verbose, + dest, ..Default::default() }; @@ -101,6 +106,7 @@ struct LogOptions { paths: Vec, commands: Vec<(String, String)>, verbose: bool, + dest: PathBuf, } impl Default for LogOptions { @@ -112,6 +118,7 @@ impl Default for LogOptions { .map(|(cmd, name)| (cmd.to_string(), name.to_string())) .collect(), verbose: false, + dest: PathBuf::from(DEFAULT_RESULT), } } } @@ -334,7 +341,14 @@ fn store(options: LogOptions) -> Result<(), io::Error> { let commands = options.commands; let paths = options.paths; let verbose = options.verbose; - let result = format!("{}.{}", DEFAULT_RESULT, DEFAULT_COMPRESSION.1); + let opt_dest = options.dest.into_os_string(); + let dest = opt_dest + .to_str() + .ok_or(io::Error::new( + io::ErrorKind::InvalidInput, + "Malformed destination path" + ))?; + let result = format!("{}.{}", dest, DEFAULT_COMPRESSION.1); showln(verbose, "Collecting Agama logs:"); From 38b45cac8aa442ce14de019afe7a97c8b1ee0834 Mon Sep 17 00:00:00 2001 From: Michal Filka Date: Fri, 27 Oct 2023 10:52:07 +0200 Subject: [PATCH 02/18] Added basic destination path input validation --- rust/agama-cli/src/logs.rs | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/rust/agama-cli/src/logs.rs b/rust/agama-cli/src/logs.rs index a2ce44564b..3926545204 100644 --- a/rust/agama-cli/src/logs.rs +++ b/rust/agama-cli/src/logs.rs @@ -33,7 +33,7 @@ pub async fn run(subcommand: LogsCommands) -> anyhow::Result<()> { LogsCommands::Store { verbose, dest } => { // feed internal options structure by what was received from user // for now we always use / add defaults if any - let dest = dest.map_or(PathBuf::from(DEFAULT_RESULT), |d| d); + let dest = parse_dest(dest); let options = LogOptions { verbose, dest, @@ -50,6 +50,28 @@ pub async fn run(subcommand: LogsCommands) -> anyhow::Result<()> { } } +// Whatewer passed in dest formed into an absolute path with archive name +// if dest is none then a default is returned +// if dest is directory then a default file name for the archive will be appended +// if dest is absolute path then it is used as is with no further checks +fn parse_dest(dest: Option) -> PathBuf { + let default = PathBuf::from(DEFAULT_RESULT); + + match dest { + None => default, + Some(mut buff) => { + // existing directory -> append an archive name + if buff.as_path().is_dir() { + buff.push("agama-logs"); + buff + // whatever else -> default (for now) + } else { + default + } + } + } +} + const DEFAULT_COMMANDS: [(&str, &str); 3] = [ // (, ) ("journalctl -u agama", "agama"), From 02ac908787978d88d5ada42466db0f68340a7207 Mon Sep 17 00:00:00 2001 From: Michal Filka Date: Fri, 27 Oct 2023 11:01:18 +0200 Subject: [PATCH 03/18] Added some error handling when validating destination path input --- rust/agama-cli/src/logs.rs | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/rust/agama-cli/src/logs.rs b/rust/agama-cli/src/logs.rs index 3926545204..79916148e4 100644 --- a/rust/agama-cli/src/logs.rs +++ b/rust/agama-cli/src/logs.rs @@ -33,7 +33,7 @@ pub async fn run(subcommand: LogsCommands) -> anyhow::Result<()> { LogsCommands::Store { verbose, dest } => { // feed internal options structure by what was received from user // for now we always use / add defaults if any - let dest = parse_dest(dest); + let dest = parse_dest(dest)?; let options = LogOptions { verbose, dest, @@ -54,19 +54,22 @@ pub async fn run(subcommand: LogsCommands) -> anyhow::Result<()> { // if dest is none then a default is returned // if dest is directory then a default file name for the archive will be appended // if dest is absolute path then it is used as is with no further checks -fn parse_dest(dest: Option) -> PathBuf { +fn parse_dest(dest: Option) -> Result { let default = PathBuf::from(DEFAULT_RESULT); match dest { - None => default, + None => Ok(default), Some(mut buff) => { // existing directory -> append an archive name if buff.as_path().is_dir() { buff.push("agama-logs"); - buff - // whatever else -> default (for now) + Ok(buff) + // whatever else -> input error } else { - default + Err(io::Error::new( + io::ErrorKind::InvalidInput, + "Invalid destination path" + )) } } } From 47e187370a8c49a3f62a5ff8ac9bea9ab90b4daa Mon Sep 17 00:00:00 2001 From: Michal Filka Date: Fri, 27 Oct 2023 11:13:34 +0200 Subject: [PATCH 04/18] Documentation, comments --- rust/agama-cli/src/logs.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/rust/agama-cli/src/logs.rs b/rust/agama-cli/src/logs.rs index 79916148e4..ee81e0b33d 100644 --- a/rust/agama-cli/src/logs.rs +++ b/rust/agama-cli/src/logs.rs @@ -53,7 +53,7 @@ pub async fn run(subcommand: LogsCommands) -> anyhow::Result<()> { // Whatewer passed in dest formed into an absolute path with archive name // if dest is none then a default is returned // if dest is directory then a default file name for the archive will be appended -// if dest is absolute path then it is used as is with no further checks +// TODO: if dest is absolute path then it is used as is with (almost) no further checks fn parse_dest(dest: Option) -> Result { let default = PathBuf::from(DEFAULT_RESULT); @@ -64,6 +64,7 @@ fn parse_dest(dest: Option) -> Result { if buff.as_path().is_dir() { buff.push("agama-logs"); Ok(buff) + //TODO: a path with file name to be handled too // whatever else -> input error } else { Err(io::Error::new( From 623870464b0eeb2f9e089ecdec2db224a1057cbf Mon Sep 17 00:00:00 2001 From: Michal Filka Date: Mon, 30 Oct 2023 10:14:05 +0100 Subject: [PATCH 05/18] Formatting --- rust/agama-cli/src/logs.rs | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/rust/agama-cli/src/logs.rs b/rust/agama-cli/src/logs.rs index ee81e0b33d..2356d6ef6c 100644 --- a/rust/agama-cli/src/logs.rs +++ b/rust/agama-cli/src/logs.rs @@ -69,7 +69,7 @@ fn parse_dest(dest: Option) -> Result { } else { Err(io::Error::new( io::ErrorKind::InvalidInput, - "Invalid destination path" + "Invalid destination path", )) } } @@ -368,12 +368,10 @@ fn store(options: LogOptions) -> Result<(), io::Error> { let paths = options.paths; let verbose = options.verbose; let opt_dest = options.dest.into_os_string(); - let dest = opt_dest - .to_str() - .ok_or(io::Error::new( - io::ErrorKind::InvalidInput, - "Malformed destination path" - ))?; + let dest = opt_dest.to_str().ok_or(io::Error::new( + io::ErrorKind::InvalidInput, + "Malformed destination path" + ))?; let result = format!("{}.{}", dest, DEFAULT_COMPRESSION.1); showln(verbose, "Collecting Agama logs:"); From 841f9703e4d1dbf7f29600f350a3191d931a21ad Mon Sep 17 00:00:00 2001 From: Michal Filka Date: Mon, 30 Oct 2023 10:17:55 +0100 Subject: [PATCH 06/18] Fixed comments according to review --- rust/agama-cli/src/logs.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/rust/agama-cli/src/logs.rs b/rust/agama-cli/src/logs.rs index 2356d6ef6c..da079685e3 100644 --- a/rust/agama-cli/src/logs.rs +++ b/rust/agama-cli/src/logs.rs @@ -20,7 +20,7 @@ pub enum LogsCommands { /// Verbose output verbose: bool, #[clap(long, short = 'd')] - /// Destination path + /// Path to destination directory dest: Option, }, /// List logs which will be collected @@ -53,7 +53,7 @@ pub async fn run(subcommand: LogsCommands) -> anyhow::Result<()> { // Whatewer passed in dest formed into an absolute path with archive name // if dest is none then a default is returned // if dest is directory then a default file name for the archive will be appended -// TODO: if dest is absolute path then it is used as is with (almost) no further checks +// TODO: if dest is path with a file name then it is used as is a name for resulting archive fn parse_dest(dest: Option) -> Result { let default = PathBuf::from(DEFAULT_RESULT); From 27b6ec50babb55ea7d2f7a3425c9cc5798360652 Mon Sep 17 00:00:00 2001 From: Michal Filka Date: Mon, 30 Oct 2023 10:25:32 +0100 Subject: [PATCH 07/18] Formatting --- rust/agama-cli/src/logs.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rust/agama-cli/src/logs.rs b/rust/agama-cli/src/logs.rs index da079685e3..74663b0c1d 100644 --- a/rust/agama-cli/src/logs.rs +++ b/rust/agama-cli/src/logs.rs @@ -370,7 +370,7 @@ fn store(options: LogOptions) -> Result<(), io::Error> { let opt_dest = options.dest.into_os_string(); let dest = opt_dest.to_str().ok_or(io::Error::new( io::ErrorKind::InvalidInput, - "Malformed destination path" + "Malformed destination path", ))?; let result = format!("{}.{}", dest, DEFAULT_COMPRESSION.1); From 03d5d8c5b66f5c0aefe3066d329a103f1ca104a5 Mon Sep 17 00:00:00 2001 From: Michal Filka Date: Wed, 1 Nov 2023 10:00:49 +0100 Subject: [PATCH 08/18] Support for customizing archive name --- rust/agama-cli/src/logs.rs | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/rust/agama-cli/src/logs.rs b/rust/agama-cli/src/logs.rs index 74663b0c1d..46cc1b97cc 100644 --- a/rust/agama-cli/src/logs.rs +++ b/rust/agama-cli/src/logs.rs @@ -20,7 +20,8 @@ pub enum LogsCommands { /// Verbose output verbose: bool, #[clap(long, short = 'd')] - /// Path to destination directory + /// Path to destination directory. Optionally with the archive file name at the end. + /// An extension will be appended automatically depending on used compression. dest: Option, }, /// List logs which will be collected @@ -53,24 +54,34 @@ pub async fn run(subcommand: LogsCommands) -> anyhow::Result<()> { // Whatewer passed in dest formed into an absolute path with archive name // if dest is none then a default is returned // if dest is directory then a default file name for the archive will be appended -// TODO: if dest is path with a file name then it is used as is a name for resulting archive +// if dest is path with a file name then it is used as is a name for resulting archive fn parse_dest(dest: Option) -> Result { let default = PathBuf::from(DEFAULT_RESULT); + let err = io::Error::new(io::ErrorKind::InvalidInput, "Invalid destination path"); match dest { None => Ok(default), Some(mut buff) => { + let path = buff.as_path(); + // existing directory -> append an archive name - if buff.as_path().is_dir() { + if path.is_dir() { buff.push("agama-logs"); Ok(buff) - //TODO: a path with file name to be handled too + // a path with file name + // sadly, is_some_and is unstable + } else if path.parent().is_some() { + // validate if parent directory realy exists + if !path.parent().unwrap().is_dir() { + Err(err) + } else { + // try to validate the file name somehow? + // if file exists, it will be overwritten + Ok(buff) + } // whatever else -> input error } else { - Err(io::Error::new( - io::ErrorKind::InvalidInput, - "Invalid destination path", - )) + Err(err) } } } From 7eb85e706d8e1ba87031cbf2127a0be04beccae8 Mon Sep 17 00:00:00 2001 From: Michal Filka Date: Wed, 1 Nov 2023 10:16:50 +0100 Subject: [PATCH 09/18] Formatting --- rust/agama-cli/src/logs.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rust/agama-cli/src/logs.rs b/rust/agama-cli/src/logs.rs index 46cc1b97cc..00e48a2002 100644 --- a/rust/agama-cli/src/logs.rs +++ b/rust/agama-cli/src/logs.rs @@ -20,7 +20,7 @@ pub enum LogsCommands { /// Verbose output verbose: bool, #[clap(long, short = 'd')] - /// Path to destination directory. Optionally with the archive file name at the end. + /// Path to destination directory. Optionally with the archive file name at the end. /// An extension will be appended automatically depending on used compression. dest: Option, }, From e7eaecd7a938235259c74a4a7740b85017b0ac4e Mon Sep 17 00:00:00 2001 From: Michal Filka Date: Wed, 1 Nov 2023 10:57:42 +0100 Subject: [PATCH 10/18] Updated changelog --- rust/package/agama-cli.changes | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/rust/package/agama-cli.changes b/rust/package/agama-cli.changes index 44ccbf8642..cb07413f73 100644 --- a/rust/package/agama-cli.changes +++ b/rust/package/agama-cli.changes @@ -1,4 +1,11 @@ ------------------------------------------------------------------- +Wed Nov 15 11:27:10 UTC 2023 - Michal Filka + +- Improved "agama logs store" (gh#openSUSE/agama#823) + - added an option which allows to define the archive destination + +------------------------------------------------------------------- +<<<<<<< HEAD Tue Nov 14 15:44:15 UTC 2023 - Jorik Cronenberg - Add support for routing to the network model (gh#openSUSE/agama#824) From 1bc77fe4d905a3779a536f8604a7d95579a0f1f0 Mon Sep 17 00:00:00 2001 From: Michal Filka Date: Wed, 15 Nov 2023 10:59:22 +0100 Subject: [PATCH 11/18] Reformated doc --- rust/agama-cli/src/logs.rs | 43 +++++++++++++++++++++----------------- 1 file changed, 24 insertions(+), 19 deletions(-) diff --git a/rust/agama-cli/src/logs.rs b/rust/agama-cli/src/logs.rs index 00e48a2002..20a2889a4e 100644 --- a/rust/agama-cli/src/logs.rs +++ b/rust/agama-cli/src/logs.rs @@ -28,7 +28,7 @@ pub enum LogsCommands { List, } -// main entry point called from agama CLI main loop +/// Main entry point called from agama CLI main loop pub async fn run(subcommand: LogsCommands) -> anyhow::Result<()> { match subcommand { LogsCommands::Store { verbose, dest } => { @@ -51,10 +51,15 @@ pub async fn run(subcommand: LogsCommands) -> anyhow::Result<()> { } } -// Whatewer passed in dest formed into an absolute path with archive name -// if dest is none then a default is returned -// if dest is directory then a default file name for the archive will be appended -// if dest is path with a file name then it is used as is a name for resulting archive +/// Whatewer passed in dest formed into an absolute path with archive name +/// +/// # Arguments: +/// * dest +/// - if None then a default is returned +/// - if a path to a directory then a default file name for the archive will be appended to the +/// path +/// - if path with a file name then it is used as is for resulting archive, just extension will +/// be appended later on (depends on used compression) fn parse_dest(dest: Option) -> Result { let default = PathBuf::from(DEFAULT_RESULT); let err = io::Error::new(io::ErrorKind::InvalidInput, "Invalid destination path"); @@ -119,7 +124,7 @@ const DEFAULT_RESULT: &str = "/tmp/agama_logs"; const DEFAULT_COMPRESSION: (&str, &str) = ("bzip2", "tar.bz2"); const DEFAULT_TMP_DIR: &str = "agama-logs"; -// A wrapper around println which shows (or not) the text depending on the boolean variable +/// A wrapper around println which shows (or not) the text depending on the boolean variable fn showln(show: bool, text: &str) { if !show { return; @@ -128,7 +133,7 @@ fn showln(show: bool, text: &str) { println!("{}", text); } -// A wrapper around println which shows (or not) the text depending on the boolean variable +/// A wrapper around println which shows (or not) the text depending on the boolean variable fn show(show: bool, text: &str) { if !show { return; @@ -137,8 +142,8 @@ fn show(show: bool, text: &str) { print!("{}", text); } -// Configurable parameters of the "agama logs" which can be -// set by user when calling a (sub)command +/// Configurable parameters of the "agama logs" which can be +/// set by user when calling a (sub)command struct LogOptions { paths: Vec, commands: Vec<(String, String)>, @@ -160,7 +165,7 @@ impl Default for LogOptions { } } -// Struct for log represented by a file +/// Struct for log represented by a file struct LogPath { // log source src_path: String, @@ -178,7 +183,7 @@ impl LogPath { } } -// Struct for log created on demand by a command +/// Struct for log created on demand by a command struct LogCmd { // command which stdout / stderr is logged cmd: String, @@ -278,8 +283,8 @@ impl LogItem for LogCmd { } } -// Collect existing / requested paths which should already exist in the system. -// Turns them into list of log sources +/// Collect existing / requested paths which should already exist in the system. +/// Turns them into list of log sources fn paths_to_log_sources(paths: &Vec, tmp_dir: &TempDir) -> Vec> { let mut log_sources: Vec> = Vec::new(); @@ -293,7 +298,7 @@ fn paths_to_log_sources(paths: &Vec, tmp_dir: &TempDir) -> Vec, tmp_dir: &TempDir, @@ -311,7 +316,7 @@ fn cmds_to_log_sources( log_sources } -// Compress given directory into a tar archive +/// Compress given directory into a tar archive fn compress_logs(tmp_dir: &TempDir, result: &String) -> io::Result<()> { let compression = DEFAULT_COMPRESSION.0; let tmp_path = tmp_dir @@ -349,8 +354,8 @@ fn compress_logs(tmp_dir: &TempDir, result: &String) -> io::Result<()> { } } -// Sets the archive owner to root:root. Also sets the file permissions to read/write for the -// owner only. +/// Sets the archive owner to root:root. Also sets the file permissions to read/write for the +/// owner only. fn set_archive_permissions(archive: &String) -> io::Result<()> { let attr = fs::metadata(archive)?; let mut permissions = attr.permissions(); @@ -368,7 +373,7 @@ fn set_archive_permissions(archive: &String) -> io::Result<()> { Ok(()) } -// Handler for the "agama logs store" subcommand +/// Handler for the "agama logs store" subcommand fn store(options: LogOptions) -> Result<(), io::Error> { if !Uid::effective().is_root() { panic!("No Root, no logs. Sorry."); @@ -423,7 +428,7 @@ fn store(options: LogOptions) -> Result<(), io::Error> { compress_logs(&tmp_dir, &result) } -// Handler for the "agama logs list" subcommand +/// Handler for the "agama logs list" subcommand fn list(options: LogOptions) { for list in [ ("Log paths: ", options.paths), From c4076e26fb8520ff5851d3dd38dcbe2bfbf75a22 Mon Sep 17 00:00:00 2001 From: Michal Filka Date: Wed, 15 Nov 2023 11:16:41 +0100 Subject: [PATCH 12/18] Simplified if-else in parse_dest --- rust/agama-cli/src/logs.rs | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/rust/agama-cli/src/logs.rs b/rust/agama-cli/src/logs.rs index 20a2889a4e..061c9c2839 100644 --- a/rust/agama-cli/src/logs.rs +++ b/rust/agama-cli/src/logs.rs @@ -72,22 +72,19 @@ fn parse_dest(dest: Option) -> Result { // existing directory -> append an archive name if path.is_dir() { buff.push("agama-logs"); - Ok(buff) // a path with file name // sadly, is_some_and is unstable } else if path.parent().is_some() { // validate if parent directory realy exists if !path.parent().unwrap().is_dir() { Err(err) - } else { - // try to validate the file name somehow? - // if file exists, it will be overwritten - Ok(buff) } // whatever else -> input error } else { Err(err) } + + Ok(buff) } } } From b27ebcb9337adfca4db2ebb2aaa699ab002d2db2 Mon Sep 17 00:00:00 2001 From: Michal Filka Date: Wed, 15 Nov 2023 11:17:42 +0100 Subject: [PATCH 13/18] Renamed parse_dest to parse_destination --- rust/agama-cli/src/logs.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/rust/agama-cli/src/logs.rs b/rust/agama-cli/src/logs.rs index 061c9c2839..ae15a88cc9 100644 --- a/rust/agama-cli/src/logs.rs +++ b/rust/agama-cli/src/logs.rs @@ -34,7 +34,7 @@ pub async fn run(subcommand: LogsCommands) -> anyhow::Result<()> { LogsCommands::Store { verbose, dest } => { // feed internal options structure by what was received from user // for now we always use / add defaults if any - let dest = parse_dest(dest)?; + let dest = parse_destination(dest)?; let options = LogOptions { verbose, dest, @@ -60,7 +60,7 @@ pub async fn run(subcommand: LogsCommands) -> anyhow::Result<()> { /// path /// - if path with a file name then it is used as is for resulting archive, just extension will /// be appended later on (depends on used compression) -fn parse_dest(dest: Option) -> Result { +fn parse_destination(dest: Option) -> Result { let default = PathBuf::from(DEFAULT_RESULT); let err = io::Error::new(io::ErrorKind::InvalidInput, "Invalid destination path"); From aad846c2b63eb7d263b44028a26173f41cc3c7e4 Mon Sep 17 00:00:00 2001 From: Michal Filka Date: Wed, 15 Nov 2023 11:19:43 +0100 Subject: [PATCH 14/18] Cleanup in the code: do not use abreviations as var names --- rust/agama-cli/src/logs.rs | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/rust/agama-cli/src/logs.rs b/rust/agama-cli/src/logs.rs index ae15a88cc9..405b18fa04 100644 --- a/rust/agama-cli/src/logs.rs +++ b/rust/agama-cli/src/logs.rs @@ -22,7 +22,7 @@ pub enum LogsCommands { #[clap(long, short = 'd')] /// Path to destination directory. Optionally with the archive file name at the end. /// An extension will be appended automatically depending on used compression. - dest: Option, + destination: Option, }, /// List logs which will be collected List, @@ -31,13 +31,13 @@ pub enum LogsCommands { /// Main entry point called from agama CLI main loop pub async fn run(subcommand: LogsCommands) -> anyhow::Result<()> { match subcommand { - LogsCommands::Store { verbose, dest } => { + LogsCommands::Store { verbose, destination } => { // feed internal options structure by what was received from user // for now we always use / add defaults if any - let dest = parse_destination(dest)?; + let destination = parse_destination(destination)?; let options = LogOptions { verbose, - dest, + destination, ..Default::default() }; @@ -51,27 +51,27 @@ pub async fn run(subcommand: LogsCommands) -> anyhow::Result<()> { } } -/// Whatewer passed in dest formed into an absolute path with archive name +/// Whatewer passed in destination formed into an absolute path with archive name /// /// # Arguments: -/// * dest +/// * destination /// - if None then a default is returned /// - if a path to a directory then a default file name for the archive will be appended to the /// path /// - if path with a file name then it is used as is for resulting archive, just extension will /// be appended later on (depends on used compression) -fn parse_destination(dest: Option) -> Result { +fn parse_destination(destination: Option) -> Result { let default = PathBuf::from(DEFAULT_RESULT); let err = io::Error::new(io::ErrorKind::InvalidInput, "Invalid destination path"); - match dest { + match destination { None => Ok(default), - Some(mut buff) => { - let path = buff.as_path(); + Some(mut buffer) => { + let path = buffer.as_path(); // existing directory -> append an archive name if path.is_dir() { - buff.push("agama-logs"); + buffer.push("agama-logs"); // a path with file name // sadly, is_some_and is unstable } else if path.parent().is_some() { @@ -84,7 +84,7 @@ fn parse_destination(dest: Option) -> Result { Err(err) } - Ok(buff) + Ok(buffer) } } } @@ -145,7 +145,7 @@ struct LogOptions { paths: Vec, commands: Vec<(String, String)>, verbose: bool, - dest: PathBuf, + destination: PathBuf, } impl Default for LogOptions { @@ -157,7 +157,7 @@ impl Default for LogOptions { .map(|(cmd, name)| (cmd.to_string(), name.to_string())) .collect(), verbose: false, - dest: PathBuf::from(DEFAULT_RESULT), + destination: PathBuf::from(DEFAULT_RESULT), } } } @@ -380,12 +380,12 @@ fn store(options: LogOptions) -> Result<(), io::Error> { let commands = options.commands; let paths = options.paths; let verbose = options.verbose; - let opt_dest = options.dest.into_os_string(); - let dest = opt_dest.to_str().ok_or(io::Error::new( + let opt_dest = options.destination.into_os_string(); + let destination = opt_dest.to_str().ok_or(io::Error::new( io::ErrorKind::InvalidInput, "Malformed destination path", ))?; - let result = format!("{}.{}", dest, DEFAULT_COMPRESSION.1); + let result = format!("{}.{}", destination, DEFAULT_COMPRESSION.1); showln(verbose, "Collecting Agama logs:"); From b571a8bcd67b372e45eb39ab019a830e5c476cec Mon Sep 17 00:00:00 2001 From: Michal Filka Date: Wed, 15 Nov 2023 11:27:33 +0100 Subject: [PATCH 15/18] Minor fix --- rust/agama-cli/src/logs.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/rust/agama-cli/src/logs.rs b/rust/agama-cli/src/logs.rs index 405b18fa04..3e89bcc483 100644 --- a/rust/agama-cli/src/logs.rs +++ b/rust/agama-cli/src/logs.rs @@ -77,11 +77,11 @@ fn parse_destination(destination: Option) -> Result } else if path.parent().is_some() { // validate if parent directory realy exists if !path.parent().unwrap().is_dir() { - Err(err) + return Err(err) } // whatever else -> input error } else { - Err(err) + return Err(err) } Ok(buffer) From 308a4fd68851baca0adb3f665c38103bf1271e4b Mon Sep 17 00:00:00 2001 From: Michal Filka Date: Wed, 15 Nov 2023 11:35:08 +0100 Subject: [PATCH 16/18] Formatting --- rust/agama-cli/src/logs.rs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/rust/agama-cli/src/logs.rs b/rust/agama-cli/src/logs.rs index 3e89bcc483..b4ca737bc4 100644 --- a/rust/agama-cli/src/logs.rs +++ b/rust/agama-cli/src/logs.rs @@ -31,7 +31,10 @@ pub enum LogsCommands { /// Main entry point called from agama CLI main loop pub async fn run(subcommand: LogsCommands) -> anyhow::Result<()> { match subcommand { - LogsCommands::Store { verbose, destination } => { + LogsCommands::Store { + verbose, + destination, + } => { // feed internal options structure by what was received from user // for now we always use / add defaults if any let destination = parse_destination(destination)?; @@ -77,11 +80,11 @@ fn parse_destination(destination: Option) -> Result } else if path.parent().is_some() { // validate if parent directory realy exists if !path.parent().unwrap().is_dir() { - return Err(err) + return Err(err); } // whatever else -> input error } else { - return Err(err) + return Err(err); } Ok(buffer) From ea4b05e009aced951afad1fc049d9e841c50cae3 Mon Sep 17 00:00:00 2001 From: Michal Filka Date: Wed, 15 Nov 2023 11:56:48 +0100 Subject: [PATCH 17/18] Refactored parse_destination --- rust/agama-cli/src/logs.rs | 40 ++++++++++++++++---------------------- 1 file changed, 17 insertions(+), 23 deletions(-) diff --git a/rust/agama-cli/src/logs.rs b/rust/agama-cli/src/logs.rs index b4ca737bc4..73e1b3086e 100644 --- a/rust/agama-cli/src/logs.rs +++ b/rust/agama-cli/src/logs.rs @@ -64,32 +64,26 @@ pub async fn run(subcommand: LogsCommands) -> anyhow::Result<()> { /// - if path with a file name then it is used as is for resulting archive, just extension will /// be appended later on (depends on used compression) fn parse_destination(destination: Option) -> Result { - let default = PathBuf::from(DEFAULT_RESULT); let err = io::Error::new(io::ErrorKind::InvalidInput, "Invalid destination path"); - - match destination { - None => Ok(default), - Some(mut buffer) => { - let path = buffer.as_path(); - - // existing directory -> append an archive name - if path.is_dir() { - buffer.push("agama-logs"); - // a path with file name - // sadly, is_some_and is unstable - } else if path.parent().is_some() { - // validate if parent directory realy exists - if !path.parent().unwrap().is_dir() { - return Err(err); - } - // whatever else -> input error - } else { - return Err(err); - } - - Ok(buffer) + let mut buffer = destination.unwrap_or(PathBuf::from(DEFAULT_RESULT)); + let path = buffer.as_path(); + + // existing directory -> append an archive name + if path.is_dir() { + buffer.push("agama-logs"); + // a path with file name + // sadly, is_some_and is unstable + } else if path.parent().is_some() { + // validate if parent directory realy exists + if !path.parent().unwrap().is_dir() { + return Err(err); } } + + // buffer is or / here + // and we know that directory tree which leads to the is valid. + // creation can still fail later on. + Ok(buffer) } const DEFAULT_COMMANDS: [(&str, &str); 3] = [ From d7aa245a93e65b522453b94a4bb88e23e89ca037 Mon Sep 17 00:00:00 2001 From: Michal Filka Date: Wed, 15 Nov 2023 12:32:44 +0100 Subject: [PATCH 18/18] Removed leftover after rebase --- rust/package/agama-cli.changes | 1 - 1 file changed, 1 deletion(-) diff --git a/rust/package/agama-cli.changes b/rust/package/agama-cli.changes index cb07413f73..75e95ad9f3 100644 --- a/rust/package/agama-cli.changes +++ b/rust/package/agama-cli.changes @@ -5,7 +5,6 @@ Wed Nov 15 11:27:10 UTC 2023 - Michal Filka - added an option which allows to define the archive destination ------------------------------------------------------------------- -<<<<<<< HEAD Tue Nov 14 15:44:15 UTC 2023 - Jorik Cronenberg - Add support for routing to the network model (gh#openSUSE/agama#824)