Arbitrary header fields in commands
Adopt the parser to be able to parse header fields.
This commit is contained in:
parent
2c00441b34
commit
8099bf773f
2 changed files with 39 additions and 21 deletions
38
src/main.rs
38
src/main.rs
|
@ -67,21 +67,29 @@ fn replace_parameter(input: &str, headers: &HeaderMap, data: &serde_json::Value)
|
|||
let parse: IResult<&str, Vec<&str>> = many0(alt((
|
||||
map_res(
|
||||
delimited(tag("{{"), take_until("}}"), tag("}}")),
|
||||
|param: &str| match param.trim() {
|
||||
"event" => {
|
||||
if let Some(event) = headers.get_one("X-Gitea-Event") {
|
||||
Ok(event)
|
||||
} else {
|
||||
bail!("Could not extract event parameter from header");
|
||||
|param: &str| {
|
||||
let expr = param.trim().split(' ').collect::<Vec<&str>>();
|
||||
|
||||
match expr.get(0) {
|
||||
Some(&"header") => {
|
||||
if let Some(field) = expr.get(1) {
|
||||
match headers.get_one(field) {
|
||||
Some(value) => Ok(value),
|
||||
_ => bail!("Could not extract event parameter from header"),
|
||||
}
|
||||
} else {
|
||||
bail!("Missing parameter for `header` expression");
|
||||
}
|
||||
}
|
||||
}
|
||||
pointer => match data.pointer(pointer) {
|
||||
Some(value) => match value.as_str() {
|
||||
Some(value) => Ok(value),
|
||||
_ => bail!("Could not convert value `{}` to string", value),
|
||||
Some(pointer) => match data.pointer(pointer) {
|
||||
Some(value) => match value.as_str() {
|
||||
Some(value) => Ok(value),
|
||||
_ => bail!("Could not convert value `{}` to string", value),
|
||||
},
|
||||
_ => bail!("Could not convert field `{}` to string", param.trim()),
|
||||
},
|
||||
_ => bail!("Could not convert field `{}` to string", param.trim()),
|
||||
},
|
||||
None => bail!("Missing expression in `{}`", input),
|
||||
}
|
||||
},
|
||||
),
|
||||
take_until("{{"),
|
||||
|
@ -297,7 +305,7 @@ fn get_config() -> Result<File> {
|
|||
return Ok(config);
|
||||
}
|
||||
|
||||
bail!("No configuration files found.");
|
||||
bail!("No configuration file found.");
|
||||
}
|
||||
|
||||
fn main() -> Result<()> {
|
||||
|
@ -471,7 +479,7 @@ mod tests {
|
|||
|
||||
assert_eq!(
|
||||
replace_parameter(
|
||||
" {{ event }} command",
|
||||
" {{ header X-Gitea-Event }} command",
|
||||
&map,
|
||||
&json!({ "field1": { "foo": "bar" } })
|
||||
)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue