2.5 KiB · text History 3208487
//! `vgl`: a command-line client for a Vigil error tracker.
//!
//! Speaks Vigil's Sentry-compatible `/api/0` API with a bearer token, the
//! same surface sentry-cli and Terraform-style tools use, so whatever the
//! CLI can do, any Sentry client can do too.

mod api;
mod cli;
mod commands;
mod config;
mod output;

use anyhow::Result;
use clap::Parser;

use cli::{Cli, Command};
use commands::Ctx;

fn main() {
    restore_sigpipe();

    if let Err(e) = run() {
        // `{:#}` puts the cause chain on one line: "couldn't reach
        // https://vigil…: connection refused" says more than either half.
        eprintln!("{}: {:#}", output::paint("error", output::Tone::Bad), e);
        std::process::exit(1);
    }
}

/// Die quietly when `head` or a pager closes the pipe, rather than panic.
#[cfg(unix)]
fn restore_sigpipe() {
    const SIGPIPE: i32 = 13;
    const SIG_DFL: usize = 0;

    unsafe extern "C" {
        fn signal(signum: i32, handler: usize) -> usize;
    }

    unsafe {
        signal(SIGPIPE, SIG_DFL);
    }
}

#[cfg(not(unix))]
fn restore_sigpipe() {}

fn run() -> Result<()> {
    let args = Cli::parse();
    let flags = config::Flags {
        url: args.url.clone(),
        token: args.token.clone(),
        org: args.org.clone(),
        project: args.project.clone(),
    };

    // The two commands that can't require credentials to exist already.
    match &args.command {
        Command::Login(login) => return commands::auth::login(login, &flags, args.json),
        Command::Logout => return commands::auth::logout(args.json),
        _ => {}
    }

    let cfg = config::Config::load()?;
    let settings = config::resolve(&flags, config::process_env, &cfg)?;
    let ctx = Ctx::new(settings, args.json)?;

    match &args.command {
        Command::Login(_) | Command::Logout => unreachable!("handled above"),
        Command::Whoami => commands::auth::whoami(&ctx),
        Command::Orgs => commands::org::orgs(&ctx),
        Command::Teams => commands::org::teams(&ctx),
        Command::Projects(p) => commands::project::run(&ctx, p.command.as_ref()),
        Command::Keys(k) => commands::key::run(&ctx, k.command.as_ref()),
        Command::Dsn => commands::key::dsn(&ctx),
        Command::Issues(i) => commands::issue::run(&ctx, i.command.as_ref()),
        Command::Releases(r) => commands::release::releases(&ctx, r.command.as_ref()),
        Command::Monitors(_) => commands::release::monitors(&ctx),
        Command::Uptime(_) => commands::release::uptime(&ctx),
    }
}