akas 2.4.18

AKAS: API Key Authorization Server
use crate::utils::validate_inputs;
use clap::{Parser, Subcommand};

#[derive(Debug, Parser)]
#[command(version, about = "A HTTP API key-Based Authorization Server", long_about = None)]
pub struct Cli {
    #[command(subcommand)]
    pub command: Commands,
}

#[derive(Debug, Subcommand)]
pub enum Commands {
    /// Start the server.
    Serve {
        /// Admin key for /load and /status URI
        #[arg(long, default_value = "", env = "AKAS_ADMIN_KEY")]
        admin_key: String,

        /// No admin key flag
        #[arg(long, conflicts_with = "admin_key")]
        no_admin_key: bool,

        /// Bind local adress only
        #[arg(long, default_value = "false", env = "AKAS_LOCAL")]
        local: bool,

        /// Enable Prometheus metrics endpoint
        #[arg(long, default_value = "false", env = "AKAS_ENABLE_METRICS")]
        enable_metrics: bool,

        /// Port of the server
        #[arg(short, long, default_value_t = 5001, env = "AKAS_PORT")]
        port: u16,

        /// Log level <error|warn|info|debug|trace>
        #[arg(long, default_value = "info", env = "AKAS_LOG_LEVEL")]
        log_level: String,

        /// Length of the x-forwarded-for, x-original header fields
        #[arg(long, default_value_t = 100, env = "AKAS_ORIGINAL_LENGTH")]
        original_length: u16,

        /// Length of the metadata header field
        #[arg(long, default_value_t = 0, env = "AKAS_METADATA_LENGTH")]
        metadata_length: u16,

        /// Length of the key [optional]
        #[arg(long, default_value_t = 0, env = "AKAS_KEY_LENGTH")]
        key_length: u16,

        /// Prefix of the key [optional]
        #[arg(long, default_value = "", env = "AKAS_KEY_PREFIX")]
        key_prefix: String,
    },
    /// Load keys from file (Not yet implemented).
    Load {
        /// Host of the server
        #[arg(long, default_value = "http://localhost:5001", env = "AKAS_HOST")]
        host: String,

        /// File path of the keys list
        file: String,

        /// Format of the key in the file <plain|sha256>
        #[arg(long, default_value = "sha256", env = "AKAS_FORMAT")]
        format: String,
    },
}

/// A command to load the keys file from the command line.
///
/// # Arguments
///
/// * `host` - The URL of the server.
/// * `filename` - The path to the file.
/// * `format` - The format of the file ("plain" or "sha256").
///
/// # Returns
///
/// * `Ok(String)` - A string containing the loaded and formatted client data on success.
/// * `Err(std::io::Error)` - An error value indicating the reason for failure, such as file not found
///   or invalid format.
///
pub fn load_client(host: &str, filename: &str, format: &str) -> Result<String, std::io::Error> {
    validate_inputs(host, filename, format)?;

    let msg = format!(
        "Request {}/load to load {} in format {}",
        host, filename, format
    );
    Ok(msg)
}