Onset: version control where nobody waits and nothing gets lost. The on command, its core and onset-server.
| 5 |
| 6 | /// A finished command's answer. |
| 7 | #[derive(Default)] |
| 8 | pub struct Report { |
| 9 | /// Label and value pairs, printed as aligned columns. |
| 10 | lines: Vec<(String, String)>, |
| 11 | /// Lines printed as they are (diffs, logs), before the labelled lines. |
| 12 | raw: Vec<String>, |
| 13 | /// The JSON form of the same facts. |
| 14 | pub json: Map<String, Value>, |
| 15 | pub next: Vec<String>, |
| 16 | /// Exit status for answers that aren't failures but need attention |
| 17 | /// (sync leaving conflicts, land waiting for someone). |
| 18 | pub exit: i32, |
| 19 | } |
| 20 |
| 21 | impl Report { |
| 22 | pub fn new() -> Self { |
| 23 | Report::default() |
| 24 | } |
| 25 |
| 26 | pub fn line(&mut self, label: &str, value: impl Into<String>) -> &mut Self { |
| 27 | self.lines.push((label.to_string(), value.into())); |
| 28 | self |
| 29 | } |
| 30 |
| 31 | /// A continuation line under the previous label. |
| 32 | pub fn more(&mut self, value: impl Into<String>) -> &mut Self { |
| 33 | self.lines.push((String::new(), value.into())); |
| 34 | self |
| 35 | } |
| 36 |
| 37 | pub fn raw(&mut self, text: impl Into<String>) -> &mut Self { |
| 38 | self.raw.push(text.into()); |
| 39 | self |
| 40 | } |
| 41 |
| 42 | pub fn set(&mut self, key: &str, value: impl Into<Value>) -> &mut Self { |
| 43 | self.json.insert(key.to_string(), value.into()); |
| 44 | self |
| 45 | } |
| 46 |
| 47 | pub fn next(&mut self, step: impl Into<String>) -> &mut Self { |
| 48 | self.next.push(step.into()); |
| 49 | self |
| 50 | } |
| 51 |
| 52 | pub fn print(&self, as_json: bool) { |
| 53 | if as_json { |
| 54 | let mut object = Map::new(); |
| 55 | object.insert("ok".into(), Value::Bool(self.exit == 0 || self.exit == 3 || self.exit == 5)); |
| 56 | for (k, v) in &self.json { |
| 57 | object.insert(k.clone(), v.clone()); |
| 58 | } |
| 59 | object.insert("next".into(), json!(self.next)); |
| 60 | println!("{}", Value::Object(object)); |
| 61 | return; |
| 62 | } |
| 63 | for text in &self.raw { |
| 64 | print!("{text}"); |
| 65 | if !text.ends_with('\n') { |
| 66 | println!(); |
| 67 | } |
| 68 | } |
| 69 | print_lines(&self.lines); |
| 70 | if let Some(text) = next_text(&self.next) { |
| 71 | println!("next: {text}"); |
| 72 | } |
| 73 | } |
| 74 | } |
| 75 |
| 76 | fn print_lines(lines: &[(String, String)]) { |
| 77 | let width = lines.iter().map(|(l, _)| l.chars().count()).max().unwrap_or(0).max(8) + 2; |
| 78 | for (label, value) in lines { |
| 79 | let mut first = true; |
| 80 | for part in value.split('\n') { |
| 81 | if first { |
| 82 | println!("{label:width$}{part}"); |
| 83 | first = false; |
| 84 | } else { |
| 85 | println!("{:width$}{part}", ""); |
| 86 | } |
| 87 | } |
| 88 | } |
| 89 | } |
| 90 |
| 91 | fn next_text(next: &[String]) -> Option<String> { |
| 92 | match next.len() { |
| 93 | 0 => None, |
| 94 | 1 => Some(next[0].clone()), |
| 95 | _ => Some(next.join(", then ")), |
| 96 | } |
| 97 | } |
| 98 |
| 99 | /// A command that didn't do what was asked. Every failure says whether |
| 100 | /// anything changed and what to do next. |
| 101 | #[derive(Debug)] |
| 102 | pub struct Fail { |
| 103 | pub code: &'static str, |
| 104 | pub exit: i32, |
| 105 | pub message: String, |
| 106 | /// Indented detail lines under the message (files, reasons). |
| 107 | pub details: Vec<String>, |
| 108 | /// What did or didn't change ("main didn't change; your change is saved"). |
| 109 | pub state: Option<String>, |
| 110 | pub next: Vec<String>, |
| 111 | pub json: Map<String, Value>, |
| 112 | } |
| 113 |
| 114 | impl Fail { |
| 115 | pub fn new(code: &'static str, message: impl Into<String>) -> Self { |
| 116 | let exit = match code { |
| 117 | "bad_usage" | "no_description" => 2, |
| 118 | "conflict" => 3, |
| 119 | "checks_failed" => 4, |
| 120 | "waiting" => 5, |
| 121 | "offline" => 6, |
| 122 | "not_allowed" | "not_logged_in" => 7, |
| 123 | "secret" => 8, |
| 124 | _ => 1, |
| 125 | }; |
| 126 | Fail { |
| 127 | code, |
| 128 | exit, |
| 129 | message: message.into(), |
| 130 | details: Vec::new(), |
| 131 | state: None, |
| 132 | next: Vec::new(), |
| 133 | json: Map::new(), |
| 134 | } |
| 135 | } |
| 136 |
| 137 | pub fn detail(mut self, line: impl Into<String>) -> Self { |
| 138 | self.details.push(line.into()); |
| 139 | self |
| 140 | } |
| 141 |
| 142 | pub fn state(mut self, text: impl Into<String>) -> Self { |
| 143 | self.state = Some(text.into()); |
| 144 | self |
| 145 | } |
| 146 |
| 147 | pub fn next(mut self, step: impl Into<String>) -> Self { |
| 148 | self.next.push(step.into()); |
| 149 | self |
| 150 | } |
| 151 |
| 152 | pub fn set(mut self, key: &str, value: impl Into<Value>) -> Self { |
| 153 | self.json.insert(key.to_string(), value.into()); |
| 154 | self |
| 155 | } |
| 156 |
| 157 | pub fn print(&self, as_json: bool) { |
| 158 | if as_json { |
| 159 | let mut object = Map::new(); |
| 160 | object.insert("ok".into(), Value::Bool(false)); |
| 161 | object.insert("error".into(), json!({ "code": self.code, "message": self.message })); |
| 162 | for (k, v) in &self.json { |
| 163 | object.insert(k.clone(), v.clone()); |
| 164 | } |
| 165 | if let Some(state) = &self.state { |
| 166 | object.insert("state".into(), json!(state)); |
| 167 | } |
| 168 | object.insert("next".into(), json!(self.next)); |
| 169 | println!("{}", Value::Object(object)); |
| 170 | return; |
| 171 | } |
| 172 | eprintln!("error: {}", self.message); |
| 173 | for line in &self.details { |
| 174 | eprintln!(" {line}"); |
| 175 | } |
| 176 | if let Some(state) = &self.state { |
| 177 | eprintln!("{state}"); |
| 178 | } |
| 179 | if let Some(text) = next_text(&self.next) { |
| 180 | eprintln!("next: {text}"); |
| 181 | } |
| 182 | } |
| 183 | } |
| 184 |
| 185 | impl From<anyhow::Error> for Fail { |
| 186 | fn from(error: anyhow::Error) -> Self { |
| 187 | if let Some(fail) = error.downcast_ref::<FailBox>() { |
| 188 | return fail.0.clone_fail(); |
| 189 | } |
| 190 | let message = format!("{error:#}"); |
| 191 | if is_network(&message) { |
| 192 | return Fail::new("offline", format!("can't reach the host: {message}")) |
| 193 | .state("nothing on the host changed; your work is saved here") |
| 194 | .next("run it again when you're online"); |
| 195 | } |
| 196 | Fail::new("failed", message) |
| 197 | } |
| 198 | } |
| 199 |
| 200 | impl From<onset_core::Error> for Fail { |
| 201 | fn from(error: onset_core::Error) -> Self { |
| 202 | Fail::from(anyhow::Error::from(error)) |
| 203 | } |
| 204 | } |
| 205 |
| 206 | impl From<rusqlite::Error> for Fail { |
| 207 | fn from(error: rusqlite::Error) -> Self { |
| 208 | Fail::from(anyhow::Error::from(error)) |
| 209 | } |
| 210 | } |
| 211 |
| 212 | impl From<std::io::Error> for Fail { |
| 213 | fn from(error: std::io::Error) -> Self { |
| 214 | Fail::from(anyhow::Error::from(error)) |
| 215 | } |
| 216 | } |
| 217 |
| 218 | /// Carries a [`Fail`] through `anyhow` (for helpers that return anyhow errors). |
| 219 | #[derive(Debug)] |
| 220 | pub struct FailBox(pub Fail); |
| 221 |
| 222 | impl std::fmt::Display for FailBox { |
| 223 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 224 | f.write_str(&self.0.message) |
| 225 | } |
| 226 | } |
| 227 |
| 228 | impl std::error::Error for FailBox {} |
| 229 |
| 230 | impl Fail { |
| 231 | fn clone_fail(&self) -> Fail { |
| 232 | Fail { |
| 233 | code: self.code, |
| 234 | exit: self.exit, |
| 235 | message: self.message.clone(), |
| 236 | details: self.details.clone(), |
| 237 | state: self.state.clone(), |
| 238 | next: self.next.clone(), |
| 239 | json: self.json.clone(), |
| 240 | } |
| 241 | } |
| 242 |
| 243 | pub fn into_anyhow(self) -> anyhow::Error { |
| 244 | anyhow::Error::new(FailBox(self)) |
| 245 | } |
| 246 | } |
| 247 |
| 248 | pub fn is_network(message: &str) -> bool { |
| 249 | let m = message.to_ascii_lowercase(); |
| 250 | [ |
| 251 | "failed to resolve address", |
| 252 | "could not resolve", |
| 253 | "failed to connect", |
| 254 | "connection refused", |
| 255 | "network is unreachable", |
| 256 | "timed out", |
| 257 | "dns error", |
| 258 | "connection reset", |
| 259 | "failed to send request", |
| 260 | "io: connection", |
| 261 | ] |
| 262 | .iter() |
| 263 | .any(|needle| m.contains(needle)) |
| 264 | } |
| 265 |
| 266 | pub type Out = Result<Report, Fail>; |
| 267 |