gitbucket-cli 0.8.1

A command-line tool for GitBucket, inspired by GitHub CLI
use crate::config::auth::{AuthConfig, ResolvedCredential};
use crate::error::{GbError, Result};

/// Resolve GitBucket host or URL from CLI arg, env var, or config.
pub fn resolve_hostname(
    cli_hostname: &Option<String>,
    cli_profile: &Option<String>,
) -> Result<String> {
    let config = AuthConfig::load()?;
    hostname_from_config(&config, cli_hostname, cli_profile)
}

pub(super) fn hostname_from_config(
    config: &AuthConfig,
    cli_hostname: &Option<String>,
    cli_profile: &Option<String>,
) -> Result<String> {
    let hostname = config.resolve_hostname(cli_hostname.as_deref(), cli_profile.as_deref())?;
    let has_active_profile = config
        .active_profile_name(cli_profile.as_deref())?
        .is_some();
    hostname.ok_or_else(|| {
        if has_active_profile {
            GbError::Auth(
                "No GitBucket host or URL configured for the selected profile. Pass --hostname or set the profile default host.".into(),
            )
        } else {
            GbError::Auth("No GitBucket host or URL configured. Run `gb auth login` first.".into())
        }
    })
}

/// Validate a selected profile even for commands that do not otherwise need config.
pub fn validate_selected_profile(cli_profile: &Option<String>) -> Result<()> {
    let config = AuthConfig::load()?;
    config.active_profile_name(cli_profile.as_deref())?;
    Ok(())
}

pub fn resolve_credential(
    hostname: &str,
    cli_profile: &Option<String>,
) -> Result<ResolvedCredential> {
    let config = AuthConfig::load()?;
    config.credential_for_profile(hostname, cli_profile.as_deref())
}