use std::process::Output;
use thiserror::Error;
pub type Result<T> = std::result::Result<T, AzureError>;
#[derive(Debug, Error)]
pub enum AzureError {
#[error("Azure CLI is not installed or not accessible. Please install Azure CLI and ensure it's in your PATH.")]
CliNotFound,
#[error("Not authenticated with Azure. Please run 'az login' first.")]
Authentication,
#[error("Failed to execute Azure CLI command '{command}': {error}")]
CliExecution { command: String, error: String },
#[error("Azure CLI error in command '{command}': {stderr}")]
CliError { command: String, stderr: String },
#[error("Failed to parse JSON response: {0}")]
JsonParse(#[from] serde_json::Error),
#[error("IO error: {0}")]
Io(#[from] std::io::Error),
#[error("{0}")]
Custom(String),
}
impl AzureError {
pub fn from_command_output(command: &str, output: Output) -> Self {
let stderr = String::from_utf8_lossy(&output.stderr);
if !stderr.is_empty() {
AzureError::CliError {
command: command.to_string(),
stderr: stderr.to_string(),
}
} else {
AzureError::CliExecution {
command: command.to_string(),
error: format!("Command failed with exit code: {}",
output.status.code().unwrap_or(-1)),
}
}
}
}