-
Notifications
You must be signed in to change notification settings - Fork 163
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
Switch config to YAML #239
Merged
Merged
Changes from 4 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
5278543
Switch config to YAML
artem-zinnatullin aa3b062
More tests
artem-zinnatullin 35874e8
Remove remoteMachine.user for now
artem-zinnatullin ea9cfaa
Add test for quotes
artem-zinnatullin f6f167b
Change yaml structure
artem-zinnatullin ec76da7
Fix test failures
artem-zinnatullin 3536c4f
Use u8
artem-zinnatullin 6ca5077
Merge branch '3.x' of github.com:buildfoundation/mainframer into az/y…
artem-zinnatullin a761727
Extract compression parsing
artem-zinnatullin c1393c9
Remove Eq
artem-zinnatullin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,3 +9,4 @@ version = "3.0.0-dev" | |
authors = ["Artem Zinnatullin <[email protected]>", "Artur Dryomov <[email protected]>", "Mainframer Developers and Contributors"] | ||
|
||
[dependencies] | ||
yaml-rust = "0.4.2" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,161 +1,16 @@ | ||
use std::fs::File; | ||
use std::io::prelude::*; | ||
use std::path::Path; | ||
|
||
#[derive(Debug, PartialEq, Eq)] | ||
pub struct Config { | ||
pub remote_machine_name: String, | ||
pub local_compression_level: u8, | ||
pub remote_compression_level: u8, | ||
} | ||
|
||
impl Config { | ||
pub fn from_file(file_path: &Path) -> Result<Config, String> { | ||
let mut content = String::new(); | ||
|
||
let mut file = match File::open(file_path) { | ||
Err(_) => return Err(format!("Could not open config file '{}'.", file_path.to_string_lossy())), | ||
Ok(value) => value, | ||
}; | ||
|
||
file.read_to_string(&mut content) | ||
.unwrap_or_else(|_| panic!("Could not read config file '{}'.", file_path.to_string_lossy())); | ||
|
||
match parse_config_from_str(&content) { | ||
Err(message) => Err(format!("Error during parsing config file '{}'\n{}", file_path.to_string_lossy(), message)), | ||
Ok(config) => Ok(config) | ||
} | ||
} | ||
} | ||
|
||
// Parses config content as https://en.wikipedia.org/wiki/.properties | ||
fn find_value(config_content: &str, key: &str) -> Option<String> { | ||
match config_content.find(&format!("{}=", key)) { | ||
None => None, | ||
Some(start_index) => { | ||
let content_starting_with_key = &config_content[start_index..config_content.len()]; | ||
let value_start_index = match content_starting_with_key.find('=') { | ||
None => return None, | ||
Some(index) => index | ||
}; | ||
|
||
let line_end_index = match content_starting_with_key.find('\n') { | ||
None => content_starting_with_key.len(), | ||
Some(index) => index | ||
}; | ||
|
||
let value = (&content_starting_with_key[value_start_index + 1..line_end_index]).trim(); | ||
|
||
match value.len() { | ||
0 => None, | ||
_ => Some(String::from(value)) | ||
} | ||
} | ||
} | ||
pub remote_machine: RemoteMachine, | ||
pub compression: Compression, | ||
} | ||
|
||
fn parse_config_from_str(config_content: &str) -> Result<Config, String> { | ||
Ok(Config { | ||
remote_machine_name: match find_value(&config_content, &"remote_machine") { | ||
None => return Err("please specify 'remote_machine'.".to_string()), | ||
Some(value) => value | ||
}, | ||
local_compression_level: match find_value(&config_content, &"local_compression_level") { | ||
None => 1, | ||
Some(value) => match value.parse() { | ||
Err(_) => return Err(format!("'local_compression_level' must be a positive number, found '{}'.", value)), | ||
Ok(value) => value | ||
} | ||
}, | ||
remote_compression_level: match find_value(&config_content, &"remote_compression_level") { | ||
None => 1, | ||
Some(value) => match value.parse() { | ||
Err(_) => return Err(format!("'remote_compression_level' must be a positive number, found '{}'.", value)), | ||
Ok(value) => value | ||
} | ||
}, | ||
}) | ||
#[derive(Debug, Eq, PartialEq)] | ||
pub struct RemoteMachine { | ||
pub host: String, | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn find_value_no_new_line_at_the_end() { | ||
let content = "key=value"; | ||
assert_eq!(find_value(content, "key"), Some(String::from("value"))); | ||
} | ||
|
||
#[test] | ||
fn find_value_new_line_at_the_end() { | ||
let content = "key=value\n"; | ||
assert_eq!(find_value(content, "key"), Some(String::from("value"))); | ||
} | ||
|
||
#[test] | ||
fn find_value_trims_value() { | ||
let content = "key=value "; | ||
assert_eq!(find_value(content, "key"), Some(String::from("value"))); | ||
} | ||
|
||
#[test] | ||
fn find_value_no_value_after_equals_sign() { | ||
let content = "key="; | ||
assert_eq!(find_value(content, "key"), None); | ||
} | ||
|
||
#[test] | ||
fn find_value_whitespace_instead_of_value() { | ||
let content = "key= "; | ||
assert_eq!(find_value(content, "key"), None); | ||
} | ||
|
||
#[test] | ||
fn parse_config_from_str_all_fields() { | ||
let content = "remote_machine=test@machine\nlocal_compression_level=2\nremote_compression_level=3"; | ||
assert_eq!(parse_config_from_str(content), Ok(Config { | ||
remote_machine_name: String::from("test@machine"), | ||
local_compression_level: 2, | ||
remote_compression_level: 3, | ||
})); | ||
} | ||
|
||
#[test] | ||
fn parse_config_from_str_all_fields_unordered() { | ||
let content = "remote_compression_level=3\nremote_machine=test@machine\nlocal_compression_level=2"; | ||
assert_eq!(parse_config_from_str(content), Ok(Config { | ||
remote_machine_name: String::from("test@machine"), | ||
local_compression_level: 2, | ||
remote_compression_level: 3, | ||
})); | ||
} | ||
|
||
#[test] | ||
fn parse_config_from_str_only_remote_machine_name() { | ||
let content = "remote_machine=test@machine"; | ||
assert_eq!(parse_config_from_str(content), Ok(Config { | ||
remote_machine_name: String::from("test@machine"), | ||
local_compression_level: 1, // Default value. | ||
remote_compression_level: 1, // Default value. | ||
})); | ||
} | ||
|
||
#[test] | ||
fn parse_config_from_str_no_remote_machine_name() { | ||
let content = "local_compression_level=2\nremote_compression_level=3"; | ||
assert_eq!(parse_config_from_str(content), Err(String::from("please specify 'remote_machine'."))); | ||
} | ||
|
||
#[test] | ||
fn parse_config_from_str_local_compression_level_not_a_number() { | ||
let content = "remote_machine=test@machine\nlocal_compression_level=yooo"; | ||
assert_eq!(parse_config_from_str(content), Err(String::from("'local_compression_level' must be a positive number, found 'yooo'."))); | ||
} | ||
|
||
#[test] | ||
fn parse_config_from_str_remote_compression_level_not_a_number() { | ||
let content = "remote_machine=test@machine\nremote_compression_level=wut"; | ||
assert_eq!(parse_config_from_str(content), Err(String::from("'remote_compression_level' must be a positive number, found 'wut'."))); | ||
} | ||
#[derive(Debug, Eq, PartialEq)] | ||
pub struct Compression { | ||
pub local: i64, | ||
pub remote: i64, | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn’t it be
i8
though?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yea, it's a slip of YAML impl details, I guess I should use proper type here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not done yet, I'll track it separately, value is validated anyway and realistically i8 is going to still be i32 at least on actual hardware
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#243