Skip to content

Commit

Permalink
Move to json (#1667)
Browse files Browse the repository at this point in the history
Unify formats to use JSON with only exception beeing products files as
it is intended to be PM oriented and rest is user oriented. Also we need
translations and multiline content from yaml in products.
  • Loading branch information
jreidinger authored Oct 14, 2024
2 parents e634c8a + 0329a50 commit 0c3c8b5
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 41 deletions.
2 changes: 1 addition & 1 deletion doc/dbus/org.opensuse.Agama1.Questions.doc.xml
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ when the question is answered and the answer is successfully read.
</method>
<!--
AddAnswerFile:
@path: Local fs path to answers file in yaml format.
@path: Local fs path to answers file in JSON format.
Adds file with list of predefined answers that will be used to
automatically answer matching questions.
Expand Down
2 changes: 1 addition & 1 deletion doc/questions.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ storage.* -> :skip. Also it is needed for matching of answers with type of gener
Partition identification for luks password. Why: Getting data only from question text is hard and very unreadable with regexp.
So questions need to specify some easy to match data in addition to the pure text.
E.g. checksum and repository url for automatic repository approval.
4. Question API has method to set answers ( TODO: lets see if it is better to get data structure or path to yaml file ). In such case if question
4. Question API has method to set answers as path to JSON file. In such case if question
with known answer is asked, it get immediate response.
Why: To allow user define machine answers in advance.
5. Questions API have property that defines if for questions without answer default one is used or if ask user.
Expand Down
20 changes: 0 additions & 20 deletions rust/Cargo.lock

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

2 changes: 1 addition & 1 deletion rust/agama-cli/src/questions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ pub enum QuestionsCommands {
/// Please check Agama documentation for more details and examples:
/// https://github.com/openSUSE/agama/blob/master/doc/questions.md
Answers {
/// Path to a file containing the answers in YAML format.
/// Path to a file containing the answers in JSON format.
path: String,
},
/// Prints the list of questions that are waiting for an answer in JSON format
Expand Down
1 change: 0 additions & 1 deletion rust/agama-server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ zbus = { version = "3", default-features = false, features = ["tokio"] }
uuid = { version = "1.10.0", features = ["v4"] }
thiserror = "1.0.64"
serde = { version = "1.0.210", features = ["derive"] }
serde_yaml = "0.9.34"
cidr = { version = "0.2.3", features = ["serde"] }
tokio = { version = "1.40.0", features = ["macros", "rt-multi-thread"] }
tokio-stream = "0.1.16"
Expand Down
2 changes: 1 addition & 1 deletion rust/agama-server/src/questions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pub enum QuestionsError {
#[error("Could not read the answers file: {0}")]
IO(std::io::Error),
#[error("Could not deserialize the answers file: {0}")]
Deserialize(serde_yaml::Error),
Deserialize(serde_json::Error),
}

#[derive(Clone, Debug)]
Expand Down
31 changes: 19 additions & 12 deletions rust/agama-server/src/questions/answers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use serde::{Deserialize, Serialize};

use super::QuestionsError;

/// Data structure for single yaml answer. For variables specification see
/// Data structure for single JSON answer. For variables specification see
/// corresponding [agama_lib::questions::GenericQuestion] fields.
/// The *matcher* part is: `class`, `text`, `data`.
/// The *answer* part is: `answer`, `password`.
Expand Down Expand Up @@ -83,7 +83,7 @@ pub struct Answers {
impl Answers {
pub fn new_from_file(path: &str) -> Result<Self, QuestionsError> {
let f = std::fs::File::open(path).map_err(QuestionsError::IO)?;
let result: Self = serde_yaml::from_reader(f).map_err(QuestionsError::Deserialize)?;
let result: Self = serde_json::from_reader(f).map_err(QuestionsError::Deserialize)?;

Ok(result)
}
Expand Down Expand Up @@ -298,18 +298,25 @@ mod tests {
}

#[test]
fn test_loading_yaml() {
fn test_loading_json() {
let file = r#"
answers:
- class: "without_data"
answer: "OK"
- class: "with_data"
data:
testk: testv
testk2: testv2
answer: "Cancel"
{
"answers": [
{
"class": "without_data",
"answer": "OK"
},
{
"class": "with_data",
"data": {
"testk": "testv",
"testk2": "testv2"
},
"answer": "Cancel"
}]
}
"#;
let result: Answers = serde_yaml::from_str(file).expect("failed to load yaml string");
let result: Answers = serde_json::from_str(file).expect("failed to load JSON string");
assert_eq!(result.answers.len(), 2);
}
}
7 changes: 7 additions & 0 deletions rust/package/agama.changes
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
-------------------------------------------------------------------
Mon Oct 14 13:53:10 UTC 2024 - Josef Reidinger <[email protected]>

- CLI: change format for questions answers file from YAML to JSON
to be consistent with other commands
(gh#agama-project/agama#1667).

-------------------------------------------------------------------
Fri Sep 27 13:09:10 UTC 2024 - Jorik Cronenberg <[email protected]>

Expand Down
8 changes: 4 additions & 4 deletions service/lib/agama/autoyast/report_patching.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ def show(message, details: "", headline: "", timeout: 0, focus: nil,
data: {}
}
data = { generic: question }.to_json
answer_yaml = Yast::Execute.locally!("agama", "questions", "ask",
answer_json = Yast::Execute.locally!("agama", "questions", "ask",
stdin: data, stdout: :capture)
answer = JSON.parse!(answer_yaml)
answer = JSON.parse!(answer_json)
answer["generic"]["answer"].to_sym
end

Expand Down Expand Up @@ -101,9 +101,9 @@ def run
"data" => {}
}
data = { generic: question, withPassword: {} }.to_json
answer_yaml = Yast::Execute.locally!("agama", "questions", "ask", stdin: data,
answer_json = Yast::Execute.locally!("agama", "questions", "ask", stdin: data,
stdout: :capture)
answer = JSON.parse!(answer_yaml)
answer = JSON.parse!(answer_json)
result = answer["generic"]["answer"].to_sym
return nil if result == :cancel

Expand Down

0 comments on commit 0c3c8b5

Please sign in to comment.