|
1 | 1 | extern crate actions;
|
2 | 2 | extern crate libc;
|
| 3 | +extern crate serde; |
| 4 | +extern crate serde_derive; |
3 | 5 | extern crate serde_json;
|
4 | 6 |
|
5 | 7 | use actions::main as actionMain;
|
| 8 | +use serde_derive::Deserialize; |
| 9 | + |
6 | 10 | use serde_json::{Error, Value};
|
7 | 11 | use std::collections::HashMap;
|
8 | 12 | use std::env;
|
9 | 13 | use std::fs::File;
|
10 |
| -use std::io::{self, stderr, stdout, Write}; |
| 14 | +use std::io::{stderr, stdin, stdout, Write}; |
11 | 15 | use std::os::unix::io::FromRawFd;
|
12 | 16 |
|
| 17 | +#[derive(Debug, Clone, PartialEq, Deserialize)] |
| 18 | +struct Input { |
| 19 | + value: HashMap<String, Value>, |
| 20 | + #[serde(flatten)] |
| 21 | + environment: HashMap<String, Value>, |
| 22 | +} |
| 23 | + |
13 | 24 | fn main() {
|
14 | 25 | let mut fd3 = unsafe { File::from_raw_fd(3) };
|
15 | 26 | loop {
|
16 | 27 | let mut buffer = String::new();
|
17 |
| - io::stdin().read_line(&mut buffer).unwrap(); |
18 |
| - let parsed_input: Result<HashMap<String, Value>, Error> = serde_json::from_str(&buffer); |
19 |
| - let mut payload: HashMap<String, Value> = HashMap::new(); |
| 28 | + stdin().read_line(&mut buffer).unwrap(); |
| 29 | + let parsed_input: Result<Input, Error> = serde_json::from_str(&buffer); |
20 | 30 | match parsed_input {
|
21 |
| - Ok(n) => { |
22 |
| - for (key, val) in n { |
23 |
| - if key == "value" { |
24 |
| - let mut unparsed_payload: Result<HashMap<String, Value>, Error> = |
25 |
| - serde_json::from_value(val); |
26 |
| - match unparsed_payload { |
27 |
| - Ok(value) => payload = value, |
28 |
| - Err(err) => { |
29 |
| - eprintln!("Error parsing value json: {}", err); |
30 |
| - continue; |
31 |
| - } |
32 |
| - } |
33 |
| - } else { |
34 |
| - env::set_var(format!("__OW_{}", key.to_uppercase()), val.to_string()); |
| 31 | + Ok(input) => { |
| 32 | + for (key, val) in input.environment { |
| 33 | + env::set_var(format!("__OW_{}", key.to_uppercase()), val.to_string()); |
| 34 | + } |
| 35 | + match serde_json::to_string(&actionMain(input.value)) { |
| 36 | + Ok(result) => { |
| 37 | + writeln!(&mut fd3, "{}", result).expect("Error writing on fd3"); |
| 38 | + } |
| 39 | + Err(err) => { |
| 40 | + eprintln!("Error formatting result value json: {}", err); |
35 | 41 | }
|
36 | 42 | }
|
37 | 43 | }
|
38 |
| - Err(e) => { |
39 |
| - eprintln!("Error: {}", e); |
40 |
| - continue; |
41 |
| - } |
42 |
| - } |
43 |
| - |
44 |
| - match serde_json::to_string(&actionMain(payload)) { |
45 |
| - Ok(result) => { |
46 |
| - writeln!(&mut fd3, "{}", result).expect("Error writing on fd3"); |
47 |
| - } |
48 | 44 | Err(err) => {
|
49 |
| - eprintln!("Error formatting result value json: {}", err); |
| 45 | + eprintln!("Error parsing input: {}", err); |
50 | 46 | }
|
51 | 47 | }
|
52 | 48 | stdout().flush().expect("Error flushing stdout");
|
|
0 commit comments