asimov-http-module 0.1.0

ASIMOV module for HTTP/HTTPS protocol support.
Documentation
// This is free and unencumbered software released into the public domain.

#[cfg(not(feature = "std"))]
compile_error!("asimov-http-fetcher requires the 'std' feature");

use asimov_module::SysexitsError::{self, *};
use clap::Parser;
use clientele::StandardOptions;
use std::error::Error;

/// asimov-http-fetcher
#[derive(Debug, Parser)]
#[command(arg_required_else_help = true)]
struct Options {
    #[clap(flatten)]
    flags: StandardOptions,

    /// The `http:` or `https:` URL to fetch
    url: String,
}

fn main() -> Result<SysexitsError, Box<dyn Error>> {
    // Load environment variables from `.env`:
    asimov_module::dotenv().ok();

    // Expand wildcards and @argfiles:
    let args = asimov_module::args_os()?;

    // Parse command-line options:
    let options = Options::parse_from(args);

    // Handle the `--version` flag:
    if options.flags.version {
        println!("{} {}", env!("CARGO_PKG_NAME"), env!("CARGO_PKG_VERSION"));
        return Ok(EX_OK);
    }

    // Handle the `--license` flag:
    if options.flags.license {
        print!("{}", include_str!("../../UNLICENSE"));
        return Ok(EX_OK);
    }

    // Configure logging & tracing:
    #[cfg(feature = "tracing")]
    asimov_module::tracing_subscriber::fmt()
        .with_writer(std::io::stderr)
        .with_max_level(&options.flags)
        .with_level(options.flags.debug || options.flags.verbose > 0)
        .with_target(options.flags.debug)
        .with_file(false)
        .without_time()
        .init();

    let mut output = std::io::stdout().lock();
    let mut input = asimov_http_module::open(&options.url)?;
    std::io::copy(&mut input, &mut output)?;

    Ok(EX_OK)
}