Add prefix flag to write_properties command #77

This commit is contained in:
sagie gur ari 2020-01-30 21:24:45 +00:00
parent 642727aae9
commit e097ae7e92
6 changed files with 64 additions and 6 deletions

View file

@ -2,6 +2,7 @@
### v0.1.9
* Add prefix flag to write_properties command #77
* New split command #76
* New appendfile command.
* New watchdog command.

View file

@ -1085,14 +1085,15 @@ read_properties
<a name="std__collections__WriteProperties"></a>
## std::collections::WriteProperties
```sh
text = write_properties [names]
text = write_properties [--prefix prefix] [names]
```
Creates a properties string from the provided list of variable names (not values).
#### Parameters
A list of variable names.
* Optional prefix which will be added to all written properties.
* A list of variable names.
#### Return Value
@ -1110,6 +1111,12 @@ a.b.c = set 3
# b=2
# a.b.c=3
text = write_properties a b a.b.c
# text will be equal to:
# P.a=1
# P.b=2
# P.a.b.c=3
text = write_properties --prefix P a b a.b.c
```

View file

@ -1,12 +1,13 @@
```sh
text = write_properties [names]
text = write_properties [--prefix prefix] [names]
```
Creates a properties string from the provided list of variable names (not values).
#### Parameters
A list of variable names.
* Optional prefix which will be added to all written properties.
* A list of variable names.
#### Return Value
@ -24,4 +25,10 @@ a.b.c = set 3
# b=2
# a.b.c=3
text = write_properties a b a.b.c
# text will be equal to:
# P.a=1
# P.b=2
# P.a.b.c=3
text = write_properties --prefix P a b a.b.c
```

View file

@ -49,11 +49,23 @@ impl Command for CommandImpl {
if arguments.len() < 1 {
CommandResult::Error("Missing properties names.".to_string())
} else {
let (start_index, prefix) = if arguments.len() > 2 && arguments[0] == "--prefix" {
(2, arguments[1].as_str())
} else {
(0, "")
};
let mut data = HashMap::new();
for argument in &arguments {
for argument in &arguments[start_index..] {
match variables.get(argument) {
Some(value) => {
data.insert(argument.to_string(), value.to_string());
let mut key = argument.to_string();
if !prefix.is_empty() {
key.insert(0, '.');
key.insert_str(0, prefix);
}
data.insert(key, value.to_string());
}
None => (),
}

View file

@ -24,3 +24,16 @@ fn run_valid() {
CommandValidation::Contains("out".to_string(), "b=2".to_string()),
);
}
#[test]
fn run_with_prefix() {
test::run_script_and_validate(
vec![create(""), Box::new(SetCommand {})],
r#"
a = test_set 1
b = test_set 2
out = write_properties --prefix A a b
"#,
CommandValidation::Contains("out".to_string(), "A.b=2".to_string()),
);
}

View file

@ -20,3 +20,21 @@ function test_write_properties
assert_eq ${b} 2
assert_eq ${a.b.c} 3
end
function test_write_properties_with_prefix
count = read_properties a=1\nb=2\na.b.c=3
assert_eq ${count} 3
assert_eq ${a} 1
assert_eq ${b} 2
assert_eq ${a.b.c} 3
text = write_properties --prefix A a b a.b.c
count = read_properties ${text}
assert_eq ${count} 3
assert_eq ${A.a} 1
assert_eq ${A.b} 2
assert_eq ${A.a.b.c} 3
end