|
| 1 | +use std::io::{stdout, Write}; |
| 2 | + |
1 | 3 | use crate::{
|
2 |
| - args::ProvisionArgs, |
| 4 | + args::{ProvisionArgs, ProvisionOutputFormat}, |
3 | 5 | config::Config,
|
4 | 6 | eval::{DeclaredState, Evaluator},
|
5 |
| - plugins::{load_plugins, Plugin, ProvisionInfo, ProvisionStateStatus}, |
| 7 | + plugins::{ |
| 8 | + load_plugins, Plugin, ProvisionInfo, ProvisionStateOutput, |
| 9 | + ProvisionStateStatus, |
| 10 | + }, |
6 | 11 | resolve::{resolve, ResolveOptions},
|
7 | 12 | root::{ensure_not_root, sudo_prompt},
|
8 | 13 | sort::sort_execution_sets,
|
9 | 14 | vars::get_global_vars,
|
10 | 15 | };
|
11 | 16 | use console::style;
|
| 17 | +use itertools::Itertools; |
12 | 18 | use jsonschema::Validator;
|
13 | 19 | use textwrap::indent;
|
14 | 20 |
|
| 21 | +trait Formatter { |
| 22 | + // TODO: refactor to accept an iterator? (may allow more advanced formatting...) |
| 23 | + fn write_result( |
| 24 | + &self, |
| 25 | + writer: &mut dyn Write, |
| 26 | + // TODO: generic context object? |
| 27 | + args: &ProvisionArgs, |
| 28 | + res: &Result<ProvisionStateOutput, serde_json::Error>, |
| 29 | + raw: &str, |
| 30 | + ) -> Result<(), Box<dyn std::error::Error>>; |
| 31 | +} |
| 32 | + |
| 33 | +struct RawFormatter {} |
| 34 | +impl Formatter for RawFormatter { |
| 35 | + fn write_result( |
| 36 | + &self, |
| 37 | + writer: &mut dyn Write, |
| 38 | + _args: &ProvisionArgs, |
| 39 | + _res: &Result<ProvisionStateOutput, serde_json::Error>, |
| 40 | + raw: &str, |
| 41 | + ) -> Result<(), Box<dyn std::error::Error>> { |
| 42 | + writeln!(writer, "{}", raw)?; |
| 43 | + Ok(()) |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +struct PrettyFormatter {} |
| 48 | +impl Formatter for PrettyFormatter { |
| 49 | + fn write_result( |
| 50 | + &self, |
| 51 | + writer: &mut dyn Write, |
| 52 | + args: &ProvisionArgs, |
| 53 | + res: &Result<ProvisionStateOutput, serde_json::Error>, |
| 54 | + raw: &str, |
| 55 | + ) -> Result<(), Box<dyn std::error::Error>> { |
| 56 | + match res { |
| 57 | + Ok(o) => { |
| 58 | + // TODO: consider plugin context: "[x!] {plugin}: {description}" |
| 59 | + match (&o.status, o.changed) { |
| 60 | + (ProvisionStateStatus::Success, false) => { |
| 61 | + if args.show_unchanged { |
| 62 | + writeln!( |
| 63 | + writer, |
| 64 | + "{}", |
| 65 | + style(format!("x {}", o.description)).green() |
| 66 | + )?; |
| 67 | + }; |
| 68 | + } |
| 69 | + (ProvisionStateStatus::Success, true) => { |
| 70 | + writeln!( |
| 71 | + writer, |
| 72 | + "{}", |
| 73 | + style(format!("- {}", o.description)).color256(208) |
| 74 | + )?; |
| 75 | + } |
| 76 | + (ProvisionStateStatus::Failed, _) => { |
| 77 | + writeln!( |
| 78 | + writer, |
| 79 | + "{}", |
| 80 | + style(format!("! {}", o.description)).red() |
| 81 | + )?; |
| 82 | + // TODO: can we get the terminal tab size? |
| 83 | + writeln!( |
| 84 | + writer, |
| 85 | + "{}", |
| 86 | + indent(o.output.as_str(), " ") |
| 87 | + )?; |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + Err(e) => { |
| 92 | + // provisioning a single result failed, likely parsing error from extraneous output |
| 93 | + // TODO: need plugin context so we can print the plugin that produced the error |
| 94 | + writeln!( |
| 95 | + writer, |
| 96 | + "{}", |
| 97 | + style(format!("!! {} received: {}", e, raw)) |
| 98 | + .red() |
| 99 | + .underlined() |
| 100 | + )?; |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + Ok(()) |
| 105 | + } |
| 106 | +} |
| 107 | + |
15 | 108 | // TODO: wrap most errors in our own, more user friendly error
|
16 | 109 | pub async fn provision(
|
17 | 110 | args: ProvisionArgs,
|
@@ -52,80 +145,53 @@ pub async fn provision(
|
52 | 145 | // validate
|
53 | 146 | validate(&execution_sets)?;
|
54 | 147 |
|
| 148 | + let formatter: Box<dyn Formatter> = match args.output { |
| 149 | + ProvisionOutputFormat::Pretty => Box::new(PrettyFormatter {}), |
| 150 | + ProvisionOutputFormat::Raw => Box::new(RawFormatter {}), |
| 151 | + }; |
| 152 | + |
55 | 153 | // provision
|
56 | 154 | let provision_info = ProvisionInfo {
|
57 | 155 | sources: config.sources,
|
58 | 156 | vars: resolved.vars,
|
59 | 157 | };
|
| 158 | + let mut lock = stdout().lock(); |
| 159 | + let writer = lock.by_ref(); |
60 | 160 | let provision_results = execution_sets
|
61 | 161 | .iter()
|
62 |
| - .map(|(p, v)| match p.provision(&provision_info, v) { |
63 |
| - Ok(i) => { |
64 |
| - Ok(i.map(|r| match r { |
65 |
| - Ok(o) => { |
66 |
| - // provisioning a single result |
67 |
| - // TODO: format: "[x!] {plugin}: {description}" |
68 |
| - // TODO: include "output" indented for failed results |
69 |
| - match (o.status, o.changed) { |
70 |
| - (ProvisionStateStatus::Success, false) => { |
71 |
| - if args.show_unchanged { |
72 |
| - println!( |
73 |
| - "{}", |
74 |
| - style(format!("x {}", o.description)) |
75 |
| - .green() |
76 |
| - ); |
77 |
| - }; |
78 |
| - |
79 |
| - Ok(()) |
80 |
| - } |
81 |
| - (ProvisionStateStatus::Success, true) => { |
82 |
| - println!( |
83 |
| - "{}", |
84 |
| - style(format!("- {}", o.description)) |
85 |
| - .color256(208) |
86 |
| - ); |
87 |
| - Ok(()) |
88 |
| - } |
89 |
| - (ProvisionStateStatus::Failed, _) => { |
90 |
| - println!( |
91 |
| - "{}", |
92 |
| - style(format!("! {}", o.description)).red() |
93 |
| - ); |
94 |
| - // TODO: can we get the terminal tab size? |
95 |
| - println!( |
96 |
| - "{}", |
97 |
| - indent(o.output.as_str(), " ") |
98 |
| - ); |
99 |
| - Err("provisioning failed".to_string()) // TODO: idk about this message... |
100 |
| - } |
101 |
| - } |
102 |
| - } |
103 |
| - Err(e) => { |
104 |
| - // provisioning a single result failed, ie. maybe just output parsing error |
105 |
| - // TODO: decide format... |
106 |
| - println!("{}", e); |
107 |
| - Err(e.to_string()) |
108 |
| - } |
109 |
| - }) |
110 |
| - .collect::<Vec<Result<_, _>>>()) |
111 |
| - } |
112 |
| - Err(e) => { |
113 |
| - // provisioning as a whole failed for this plugin |
114 |
| - // TODO: decide format... |
115 |
| - println!("{}", e); |
116 |
| - Err(e) |
117 |
| - // Ok() |
118 |
| - // vec![Err(e)] |
| 162 | + .map(|(p, v)| { |
| 163 | + match p.provision(&provision_info, v) { |
| 164 | + Ok(i) => Ok(i |
| 165 | + .flatten() |
| 166 | + .map(|line| { |
| 167 | + let result = |
| 168 | + serde_json::from_str::<ProvisionStateOutput>(&line); |
| 169 | + formatter |
| 170 | + .write_result(writer, &args, &result, &line)?; |
| 171 | + |
| 172 | + result.map_err(|e| e.into()) |
| 173 | + }) |
| 174 | + .collect::<Vec<Result<_, _>>>()), |
| 175 | + Err(e) => { |
| 176 | + // provisioning as a whole failed for this plugin |
| 177 | + // TODO: decide format... |
| 178 | + writeln!( |
| 179 | + writer, |
| 180 | + "plugin failed provisioning {}: {}", |
| 181 | + p.definition.name, e |
| 182 | + )?; |
| 183 | + Err(e) |
| 184 | + } |
119 | 185 | }
|
120 | 186 | })
|
121 |
| - .collect::<Result<Vec<_>, _>>(); |
| 187 | + .flatten_ok() |
| 188 | + .collect::<Result<Vec<Result<_, Box<dyn std::error::Error>>>, _>>(); |
122 | 189 |
|
123 | 190 | // TODO: list unmatched states
|
124 | 191 |
|
125 | 192 | // TODO: ugh...
|
126 |
| - if provision_results |
127 |
| - .iter() |
128 |
| - .any(|pr| pr.iter().any(|r| r.iter().any(Result::is_err))) |
| 193 | + if provision_results.is_err() |
| 194 | + || provision_results.unwrap().iter().any(Result::is_err) |
129 | 195 | {
|
130 | 196 | return Err("provisioning failed...")?;
|
131 | 197 | }
|
|
0 commit comments