Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move to json #1667

Merged
merged 3 commits into from
Oct 14, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}
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
Loading