From 62715512faca3942ca803e85f5ccb3019cd62c30 Mon Sep 17 00:00:00 2001 From: Josef Reidinger Date: Mon, 17 Jul 2023 15:14:55 +0200 Subject: [PATCH 01/11] update proxies with new question interface --- rust/agama-lib/src/proxies.rs | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/rust/agama-lib/src/proxies.rs b/rust/agama-lib/src/proxies.rs index d2f31e00da..ddf2e82167 100644 --- a/rust/agama-lib/src/proxies.rs +++ b/rust/agama-lib/src/proxies.rs @@ -111,30 +111,32 @@ trait Locale1 { fn set_vconsole_keyboard(&self, value: &str) -> zbus::Result<()>; } -#[dbus_proxy( - interface = "org.opensuse.Agama.Questions1", - default_service = "org.opensuse.Agama.Questions1", - default_path = "/org/opensuse/Agama/Questions1" -)] +#[dbus_proxy(interface = "org.opensuse.Agama.Questions1", assume_defaults = true)] trait Questions1 { /// Delete method fn delete(&self, question: &zbus::zvariant::ObjectPath<'_>) -> zbus::Result<()>; /// New method #[dbus_proxy(name = "New")] - fn create( + fn new_generic( &self, + class: &str, text: &str, options: &[&str], - default_option: &[&str], + default_option: &str, + data: std::collections::HashMap<&str, &str>, ) -> zbus::Result; - /// NewLuksActivation method - fn new_luks_activation( + /// NewWithPassword method + fn new_with_password( &self, - device: &str, - label: &str, - size: &str, - attempt: u8, + class: &str, + text: &str, + options: &[&str], + default_option: &str, + data: std::collections::HashMap<&str, &str>, ) -> zbus::Result; -} + + /// UseDefaultAnswer method + fn use_default_answer(&self) -> zbus::Result<()>; +} \ No newline at end of file From f1bc1b03d29a2eb1041870d13e4a62586b6eeb9c Mon Sep 17 00:00:00 2001 From: Josef Reidinger Date: Mon, 17 Jul 2023 21:58:02 +0200 Subject: [PATCH 02/11] Add command to set default answers for questions --- rust/agama-cli/src/commands.rs | 4 ++++ rust/agama-cli/src/main.rs | 3 +++ rust/agama-cli/src/questions.rs | 28 ++++++++++++++++++++++++++++ 3 files changed, 35 insertions(+) create mode 100644 rust/agama-cli/src/questions.rs diff --git a/rust/agama-cli/src/commands.rs b/rust/agama-cli/src/commands.rs index de2a386907..9c07516988 100644 --- a/rust/agama-cli/src/commands.rs +++ b/rust/agama-cli/src/commands.rs @@ -1,5 +1,6 @@ use crate::config::ConfigCommands; use crate::profile::ProfileCommands; +use crate::questions::QuestionsCommands; use clap::Subcommand; #[derive(Subcommand, Debug)] @@ -20,4 +21,7 @@ pub enum Commands { /// Autoinstallation profile handling #[command(subcommand)] Profile(ProfileCommands), + /// Questions handling + #[command(subcommand)] + Questions(QuestionsCommands), } diff --git a/rust/agama-cli/src/main.rs b/rust/agama-cli/src/main.rs index c3b05dde56..bdb1c31b78 100644 --- a/rust/agama-cli/src/main.rs +++ b/rust/agama-cli/src/main.rs @@ -6,6 +6,7 @@ mod error; mod printers; mod profile; mod progress; +mod questions; use crate::error::CliError; use agama_lib::error::ServiceError; @@ -22,6 +23,7 @@ use std::{ thread::sleep, time::Duration, }; +use questions::run as run_questions_cmd; #[derive(Parser)] #[command(name = "agama", version, about, long_about = None)] @@ -128,6 +130,7 @@ async fn run_command(cli: Cli) -> anyhow::Result<()> { let manager = build_manager().await?; block_on(install(&manager, 3)) } + Commands::Questions(subcommand) => block_on(run_questions_cmd(subcommand)), _ => unimplemented!(), } } diff --git a/rust/agama-cli/src/questions.rs b/rust/agama-cli/src/questions.rs new file mode 100644 index 0000000000..7074f97c92 --- /dev/null +++ b/rust/agama-cli/src/questions.rs @@ -0,0 +1,28 @@ +use anyhow::{Ok,Context}; +use clap::Subcommand; +use agama_lib::connection; +use agama_lib::proxies::Questions1Proxy; + +#[derive(Subcommand, Debug)] +pub enum QuestionsCommands { + /// To use default answers for questions. + UseDefaults, +} + +// TODO when more commands is added, refactor and add it to agama-lib and share a bit of functionality +async fn use_defaults() -> anyhow::Result<()> { + let connection = connection().await.context("Connection to DBus failed.")?; + let proxy = Questions1Proxy::new(&connection).await.context("Failed to connect to Questions service")?; + + // TODO: how to print dbus error in that anyhow? + proxy.use_default_answer().await.context("Failed to set default answer")?; + + + Ok(()) +} + +pub async fn run(subcommand: QuestionsCommands) -> anyhow::Result<()> { + match subcommand { + QuestionsCommands::UseDefaults => use_defaults().await, + } +} \ No newline at end of file From 12ee8899ed79da38a96b8fc84489db5f2ae71be8 Mon Sep 17 00:00:00 2001 From: Josef Reidinger Date: Mon, 17 Jul 2023 22:04:19 +0200 Subject: [PATCH 03/11] formatting --- rust/agama-cli/src/main.rs | 2 +- rust/agama-cli/src/questions.rs | 16 ++++++++++------ rust/agama-lib/src/proxies.rs | 2 +- 3 files changed, 12 insertions(+), 8 deletions(-) diff --git a/rust/agama-cli/src/main.rs b/rust/agama-cli/src/main.rs index bdb1c31b78..5baabc713c 100644 --- a/rust/agama-cli/src/main.rs +++ b/rust/agama-cli/src/main.rs @@ -18,12 +18,12 @@ use config::run as run_config_cmd; use printers::Format; use profile::run as run_profile_cmd; use progress::InstallerProgress; +use questions::run as run_questions_cmd; use std::{ process::{ExitCode, Termination}, thread::sleep, time::Duration, }; -use questions::run as run_questions_cmd; #[derive(Parser)] #[command(name = "agama", version, about, long_about = None)] diff --git a/rust/agama-cli/src/questions.rs b/rust/agama-cli/src/questions.rs index 7074f97c92..0e3293c0c8 100644 --- a/rust/agama-cli/src/questions.rs +++ b/rust/agama-cli/src/questions.rs @@ -1,7 +1,7 @@ -use anyhow::{Ok,Context}; -use clap::Subcommand; use agama_lib::connection; use agama_lib::proxies::Questions1Proxy; +use anyhow::{Context, Ok}; +use clap::Subcommand; #[derive(Subcommand, Debug)] pub enum QuestionsCommands { @@ -12,11 +12,15 @@ pub enum QuestionsCommands { // TODO when more commands is added, refactor and add it to agama-lib and share a bit of functionality async fn use_defaults() -> anyhow::Result<()> { let connection = connection().await.context("Connection to DBus failed.")?; - let proxy = Questions1Proxy::new(&connection).await.context("Failed to connect to Questions service")?; + let proxy = Questions1Proxy::new(&connection) + .await + .context("Failed to connect to Questions service")?; // TODO: how to print dbus error in that anyhow? - proxy.use_default_answer().await.context("Failed to set default answer")?; - + proxy + .use_default_answer() + .await + .context("Failed to set default answer")?; Ok(()) } @@ -25,4 +29,4 @@ pub async fn run(subcommand: QuestionsCommands) -> anyhow::Result<()> { match subcommand { QuestionsCommands::UseDefaults => use_defaults().await, } -} \ No newline at end of file +} diff --git a/rust/agama-lib/src/proxies.rs b/rust/agama-lib/src/proxies.rs index ddf2e82167..d6678c0701 100644 --- a/rust/agama-lib/src/proxies.rs +++ b/rust/agama-lib/src/proxies.rs @@ -139,4 +139,4 @@ trait Questions1 { /// UseDefaultAnswer method fn use_default_answer(&self) -> zbus::Result<()>; -} \ No newline at end of file +} From 55815d6c5279b9a554f5188741d181909bb8dd3e Mon Sep 17 00:00:00 2001 From: Josef Reidinger Date: Tue, 18 Jul 2023 09:15:02 +0200 Subject: [PATCH 04/11] Add example to object path method --- rust/agama-lib/src/questions.rs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/rust/agama-lib/src/questions.rs b/rust/agama-lib/src/questions.rs index d50c72f07e..5e77f2d10b 100644 --- a/rust/agama-lib/src/questions.rs +++ b/rust/agama-lib/src/questions.rs @@ -43,6 +43,24 @@ impl GenericQuestion { } } + /// Gets object path of given question. It is useful as parameter + /// for deleting it. + /// + /// # Examples + /// + /// ``` + /// use std::collections::HashMap; + /// use agama_lib::questions::GenericQuestion; + /// let question = GenericQuestion::new( + /// 2, + /// "test_class".to_string(), + /// "Really?".to_string(), + /// vec!["Yes".to_string(), "No".to_string()], + /// "No".to_string(), + /// HashMap::new() + /// ); + /// assert_eq!(question.object_path(), "/org/opensuse/Agama/Questions1/2".to_string()); + /// ``` pub fn object_path(&self) -> String { format!("/org/opensuse/Agama/Questions1/{}", self.id) } From 2e2c1081ebc9a5a849d0fde384a90a0f65418362 Mon Sep 17 00:00:00 2001 From: Josef Reidinger Date: Tue, 18 Jul 2023 14:03:46 +0200 Subject: [PATCH 05/11] Change commands for questions --- rust/Cargo.lock | 1 + rust/agama-cli/Cargo.toml | 1 + rust/agama-cli/src/questions.rs | 45 +++++++++++++++++++++++---------- rust/agama-lib/src/questions.rs | 4 +-- 4 files changed, 35 insertions(+), 16 deletions(-) diff --git a/rust/Cargo.lock b/rust/Cargo.lock index 0b77eada04..745095d4c7 100644 --- a/rust/Cargo.lock +++ b/rust/Cargo.lock @@ -19,6 +19,7 @@ dependencies = [ "console", "convert_case", "indicatif", + "log", "serde", "serde_json", "serde_yaml", diff --git a/rust/agama-cli/Cargo.toml b/rust/agama-cli/Cargo.toml index 4b3078034b..38765fee9a 100644 --- a/rust/agama-cli/Cargo.toml +++ b/rust/agama-cli/Cargo.toml @@ -17,6 +17,7 @@ thiserror = "1.0.39" convert_case = "0.6.0" console = "0.15.7" anyhow = "1.0.71" +log = "0.4" [[bin]] name = "agama" diff --git a/rust/agama-cli/src/questions.rs b/rust/agama-cli/src/questions.rs index 0e3293c0c8..02535f8e0f 100644 --- a/rust/agama-cli/src/questions.rs +++ b/rust/agama-cli/src/questions.rs @@ -1,32 +1,49 @@ use agama_lib::connection; use agama_lib::proxies::Questions1Proxy; use anyhow::{Context, Ok}; -use clap::Subcommand; +use clap::{Args, Subcommand, ValueEnum}; +use log; #[derive(Subcommand, Debug)] pub enum QuestionsCommands { - /// To use default answers for questions. - UseDefaults, + /// Set mode for answering questions. + Mode(ModesArgs), } +#[derive(Args, Debug)] +pub struct ModesArgs { + #[arg(value_enum)] + value: Modes, +} + +#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, ValueEnum)] +pub enum Modes { + Interactive, + NonInteractive, +} // TODO when more commands is added, refactor and add it to agama-lib and share a bit of functionality -async fn use_defaults() -> anyhow::Result<()> { - let connection = connection().await.context("Connection to DBus failed.")?; - let proxy = Questions1Proxy::new(&connection) - .await - .context("Failed to connect to Questions service")?; +async fn set_mode(value: Modes) -> anyhow::Result<()> { + match value { + Modes::NonInteractive => { + let connection = connection().await?; + let proxy = Questions1Proxy::new(&connection) + .await + .context("Failed to connect to Questions service")?; - // TODO: how to print dbus error in that anyhow? - proxy - .use_default_answer() - .await - .context("Failed to set default answer")?; + // TODO: how to print dbus error in that anyhow? + proxy + .use_default_answer() + .await + .context("Failed to set default answer")?; + } + Modes::Interactive => log::info!("not implemented"), //TODO do it + } Ok(()) } pub async fn run(subcommand: QuestionsCommands) -> anyhow::Result<()> { match subcommand { - QuestionsCommands::UseDefaults => use_defaults().await, + QuestionsCommands::Mode(value) => set_mode(value.value).await, } } diff --git a/rust/agama-lib/src/questions.rs b/rust/agama-lib/src/questions.rs index 5e77f2d10b..b5e6b75970 100644 --- a/rust/agama-lib/src/questions.rs +++ b/rust/agama-lib/src/questions.rs @@ -45,9 +45,9 @@ impl GenericQuestion { /// Gets object path of given question. It is useful as parameter /// for deleting it. - /// + /// /// # Examples - /// + /// /// ``` /// use std::collections::HashMap; /// use agama_lib::questions::GenericQuestion; From d507928888fe53efc230550a05bb194901cd5897 Mon Sep 17 00:00:00 2001 From: Josef Reidinger Date: Tue, 18 Jul 2023 14:04:38 +0200 Subject: [PATCH 06/11] remove unused use --- rust/agama-cli/src/questions.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rust/agama-cli/src/questions.rs b/rust/agama-cli/src/questions.rs index 02535f8e0f..dab3c54f69 100644 --- a/rust/agama-cli/src/questions.rs +++ b/rust/agama-cli/src/questions.rs @@ -2,7 +2,7 @@ use agama_lib::connection; use agama_lib::proxies::Questions1Proxy; use anyhow::{Context, Ok}; use clap::{Args, Subcommand, ValueEnum}; -use log; + #[derive(Subcommand, Debug)] pub enum QuestionsCommands { From 7a9c6144608592d75da0f6e6acb7653662b2168c Mon Sep 17 00:00:00 2001 From: Josef Reidinger Date: Tue, 18 Jul 2023 14:07:09 +0200 Subject: [PATCH 07/11] update cli guidelines --- doc/cli_guidelines.md | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/doc/cli_guidelines.md b/doc/cli_guidelines.md index ee6e1aa324..82d33ca757 100644 --- a/doc/cli_guidelines.md +++ b/doc/cli_guidelines.md @@ -192,6 +192,10 @@ There are pending questions. Please, answer questions first: agama questions. ## Non Interactive Mode -Commands should offer a `--non-interactive` option to make scripting possible. The non interactive mode should allow answering questions automatically. +There are questions subcommand that support `mode` subcommand to set non-interactive mode. Another option is to use `answers` subcommand to pass file with +predefined answers. -TBD: Non interactive mode will be defined later in the next iteration. +~~~ +agama questions mode non-interactive +agama questions answers /tmp/answers.json +~~~ From c0ec508cf61f2618afacd114bad137615fdd449a Mon Sep 17 00:00:00 2001 From: Josef Reidinger Date: Tue, 18 Jul 2023 14:30:43 +0200 Subject: [PATCH 08/11] Apply suggestions from code review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Imobach González Sosa --- doc/cli_guidelines.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/cli_guidelines.md b/doc/cli_guidelines.md index 82d33ca757..32136c50b1 100644 --- a/doc/cli_guidelines.md +++ b/doc/cli_guidelines.md @@ -192,7 +192,7 @@ There are pending questions. Please, answer questions first: agama questions. ## Non Interactive Mode -There are questions subcommand that support `mode` subcommand to set non-interactive mode. Another option is to use `answers` subcommand to pass file with +There is a `questions` subcommand that implements a `mode` subcommand to set the non-interactive mode. Another option is to use `answers` subcommand to pass a file with predefined answers. ~~~ From 1d74a2b694db7e8871600005fae999367c1da0f5 Mon Sep 17 00:00:00 2001 From: Josef Reidinger Date: Tue, 18 Jul 2023 14:48:12 +0200 Subject: [PATCH 09/11] apply suggestion from jilopez for doc --- doc/cli_guidelines.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/doc/cli_guidelines.md b/doc/cli_guidelines.md index 32136c50b1..4e244455c0 100644 --- a/doc/cli_guidelines.md +++ b/doc/cli_guidelines.md @@ -192,8 +192,9 @@ There are pending questions. Please, answer questions first: agama questions. ## Non Interactive Mode -There is a `questions` subcommand that implements a `mode` subcommand to set the non-interactive mode. Another option is to use `answers` subcommand to pass a file with -predefined answers. +There is a `questions` subcommand that support `mode` and `answers` subcommands. +The `mode` subcommand allows to set `interactive` and `non-interactive` modes. +The `answers` allows to pass a file with predefined answers. ~~~ agama questions mode non-interactive From f36e29bf77af3fbd22258e965958cb2e987762db Mon Sep 17 00:00:00 2001 From: Josef Reidinger Date: Tue, 18 Jul 2023 15:01:35 +0200 Subject: [PATCH 10/11] fix formatting --- rust/agama-cli/src/questions.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/rust/agama-cli/src/questions.rs b/rust/agama-cli/src/questions.rs index dab3c54f69..d0a22c1c00 100644 --- a/rust/agama-cli/src/questions.rs +++ b/rust/agama-cli/src/questions.rs @@ -3,7 +3,6 @@ use agama_lib::proxies::Questions1Proxy; use anyhow::{Context, Ok}; use clap::{Args, Subcommand, ValueEnum}; - #[derive(Subcommand, Debug)] pub enum QuestionsCommands { /// Set mode for answering questions. From b4315b122286da2492a75f92fb8f13411a1e75e8 Mon Sep 17 00:00:00 2001 From: Josef Reidinger Date: Tue, 18 Jul 2023 15:34:03 +0200 Subject: [PATCH 11/11] changes --- rust/package/agama-cli.changes | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/rust/package/agama-cli.changes b/rust/package/agama-cli.changes index 1d5445bb30..33c0cae800 100644 --- a/rust/package/agama-cli.changes +++ b/rust/package/agama-cli.changes @@ -1,3 +1,9 @@ +------------------------------------------------------------------- +Tue Jul 18 13:32:04 UTC 2023 - Josef Reidinger + +- Add to CLI "questions" subcommand with mode option to set + interactive and non-interactive mode (gh#openSUSE/agama#668) + ------------------------------------------------------------------- Mon Jul 17 09:16:38 UTC 2023 - Josef Reidinger