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