commit df3f2fe4e55abd4e18222ed53405bc0c80d0f5a1
parent fa8a254141d87b99a1ce4eed597b3365ee93c4a5
Author: Stefan Koch <programming@stefan-koch.name>
Date: Fri, 20 Sep 2019 19:23:31 +0200
use percentage symbol to mark variables
Diffstat:
3 files changed, 22 insertions(+), 26 deletions(-)
diff --git a/.cinderella.toml b/.cinderella.toml
@@ -6,9 +6,9 @@ commands = [
[build-release]
commands = [
"cargo build --release",
- "cp target/release/cinderella /opt/cinderella/.cinderella-{{ branch }}",
+ "cp target/release/cinderella /opt/cinderella/.cinderella-%BRANCH",
# use -f on deletion to avoid errors if file does not exist
- "rm -f /opt/cinderella/cinderella-{{ branch }}",
- "mv /opt/cinderella/.cinderella-{{ branch }} /opt/cinderella/cinderella-{{ branch }}",
+ "rm -f /opt/cinderella/cinderella-%BRANCH",
+ "mv /opt/cinderella/.cinderella-%BRANCH /opt/cinderella/cinderella-%BRANCH",
]
-when = "branch == \"master\""
+when = "\"%BRANCH\" == \"master\""
diff --git a/README.md b/README.md
@@ -52,11 +52,12 @@ followed by `build-release`.
### Variables
-You can use variables in your build commands. These variables will get
-substituted with actual values from the build context. Currently supported
-variables are:
+You can use variables in the configuration file. All variables are denoted
+with a percentage symbol (`%`) and will be replaced before the commands
+are being sent to the shell.
+Currently supported variables are:
-- `{{ branch }}`: The name of the branch that is built
+- `%BRANCH`: The name of the branch that is built
### Conditions
@@ -68,11 +69,8 @@ to run a pipeline only for specific branches:
commands = [
"cargo build --release",
]
-when = "branch == \"master\""
+when = "\"%BRANCH\" == \"master\""
```
The condition will be executed with the Rust library
-[evalexpr](https://docs.rs/evalexpr/5.0.5/evalexpr/index.html). Unlike in
-the commands, variables in the `when` conditions are used without
-braces. Be aware that this syntax will change in the future and the syntax
-for variables in commands and conditions will be the same.
+[evalexpr](https://docs.rs/evalexpr/5.0.5/evalexpr/index.html).
diff --git a/src/execution.rs b/src/execution.rs
@@ -2,7 +2,7 @@ use std::collections::HashMap;
use std::process::Command;
use std::io::Write;
-use evalexpr::{self, Context, HashMapContext, Value};
+use evalexpr;
use crate::pipeline;
@@ -58,14 +58,12 @@ fn split_command<'a>(command: &'a str) -> Vec<&'a str> {
}
fn execute_test(test: &str, variables: &HashMap<String, String>) -> bool {
- let mut context = HashMapContext::new();
+ // not possible to use evalexpr Context, because evalexpr only handles
+ // standard variable names without special characters (percentage
+ // symbol cannot be used)
+ let test = replace_variables(test, variables);
- for (key, value) in variables {
- context.set_value(key.to_string(), Value::String(value.clone()))
- .unwrap();
- }
-
- match evalexpr::eval_boolean_with_context(test, &context) {
+ match evalexpr::eval_boolean(&test) {
Ok(true) => true,
_ => false,
}
@@ -77,8 +75,8 @@ fn replace_variables(command: &str, variables: &HashMap<String, String>)
let mut res = String::from(command);
for (original, replacement) in variables {
- // replace "{{ varname }}" with replacement value
- let varname = format!("{{{{ {} }}}}", original);
+ // replace "%VARNAME" with replacement value
+ let varname = format!("%{}", original.to_uppercase());
res = res.replace(&varname, replacement);
}
@@ -117,7 +115,7 @@ mod tests {
fn test_pipeline_with_variables() {
let pipeline = Pipeline {
name: String::from("my-test"),
- commands: vec!["echo '{{ myvar }}'".to_string()],
+ commands: vec!["echo '%MYVAR'".to_string()],
when: None,
};
let mut variables = HashMap::new();
@@ -133,7 +131,7 @@ mod tests {
let pipeline = Pipeline {
name: String::from("my-test"),
commands: vec!["echo 'Building non-master'".to_string()],
- when: Some(String::from("branch != \"master\"")),
+ when: Some(String::from("\"%BRANCH\" != \"master\"")),
};
let mut variables = HashMap::new();
variables.insert(String::from("branch"), String::from("master"));
@@ -149,7 +147,7 @@ mod tests {
let pipeline = Pipeline {
name: String::from("my-test"),
commands: vec!["echo 'Building master'".to_string()],
- when: Some(String::from("branch == \"master\"")),
+ when: Some(String::from("\"%BRANCH\" == \"master\"")),
};
let mut variables = HashMap::new();
variables.insert(String::from("branch"), String::from("master"));