| 1 | //! The g1t command line. |
| 2 | |
| 3 | mod auth; |
| 4 | mod chunk; |
| 5 | mod cli; |
| 6 | mod client; |
| 7 | mod commands; |
| 8 | mod config; |
| 9 | mod context; |
| 10 | mod credentials; |
| 11 | #[cfg(test)] |
| 12 | mod docs; |
| 13 | mod error; |
| 14 | mod git; |
| 15 | mod image; |
| 16 | mod jq; |
| 17 | mod mapping; |
| 18 | mod ops; |
| 19 | mod ops_table; |
| 20 | mod output; |
| 21 | mod prompt; |
| 22 | mod push; |
| 23 | mod reference; |
| 24 | mod registry; |
| 25 | mod repo; |
| 26 | |
| 27 | use clap::{CommandFactory, FromArgMatches}; |
| 28 | use std::process::ExitCode; |
| 29 | |
| 30 | fn main() -> ExitCode { |
| 31 | let args: Vec<String> = std::env::args().collect(); |
| 32 | let code = match run(args) { |
| 33 | Ok(()) => 0, |
| 34 | Err(error) => { |
| 35 | if !error.silent && !error.message.is_empty() { |
| 36 | eprintln!("g1t: {}", error.message.trim_end()); |
| 37 | } |
| 38 | error.kind.code() |
| 39 | } |
| 40 | }; |
| 41 | ExitCode::from(code) |
| 42 | } |
| 43 | |
| 44 | fn run(args: Vec<String>) -> error::Result<()> { |
| 45 | let args = match commands::alias::expand(args)? { |
| 46 | commands::alias::Expanded::Args(args) => args, |
| 47 | commands::alias::Expanded::Ran(code) => { |
| 48 | return if code == 0 { |
| 49 | Ok(()) |
| 50 | } else { |
| 51 | Err(error::Error::exit(error::Kind::Failed)) |
| 52 | }; |
| 53 | } |
| 54 | }; |
| 55 | let matches = match cli::Cli::command().try_get_matches_from(&args) { |
| 56 | Ok(m) => m, |
| 57 | Err(e) => return clap_error(e), |
| 58 | }; |
| 59 | let parsed = match cli::Cli::from_arg_matches(&matches) { |
| 60 | Ok(p) => p, |
| 61 | Err(e) => return clap_error(e), |
| 62 | }; |
| 63 | commands::dispatch(parsed) |
| 64 | } |
| 65 | |
| 66 | /// Help and the version go to stdout and succeed; a mistake is one line on |
| 67 | /// stderr and exit code 2. |
| 68 | fn clap_error(e: clap::Error) -> error::Result<()> { |
| 69 | use clap::error::ErrorKind; |
| 70 | match e.kind() { |
| 71 | ErrorKind::DisplayHelp | ErrorKind::DisplayVersion => { |
| 72 | let _ = e.print(); |
| 73 | Ok(()) |
| 74 | } |
| 75 | ErrorKind::DisplayHelpOnMissingArgumentOrSubcommand => { |
| 76 | let _ = e.print(); |
| 77 | Err(error::Error::exit(error::Kind::Usage)) |
| 78 | } |
| 79 | _ => { |
| 80 | let text = e.render().to_string(); |
| 81 | let line = text |
| 82 | .lines() |
| 83 | .find(|l| !l.trim().is_empty()) |
| 84 | .unwrap_or("") |
| 85 | .trim_start_matches("error: ") |
| 86 | .trim(); |
| 87 | let tip = text |
| 88 | .lines() |
| 89 | .find(|l| l.trim_start().starts_with("tip:")) |
| 90 | .map(|l| format!(" ({})", l.trim().trim_start_matches("tip: "))) |
| 91 | .unwrap_or_default(); |
| 92 | Err(error::Error::usage(format!("{line}{tip} See --help."))) |
| 93 | } |
| 94 | } |
| 95 | } |