use std::path::PathBuf;
use serde_json::Value;
use sprawl_guard_lib::config::{Config, LoadOptions, load_resolved_config};
use crate::error::{CliError, Result};
#[derive(Clone, Debug, Eq, PartialEq)]
pub(super) struct ConfigCommandExecution {
config: Config,
}
impl ConfigCommandExecution {
fn new(config: Config) -> Self {
Self { config }
}
}
pub(super) fn execute_config_resolved(
root: Option<PathBuf>,
config_path: Option<PathBuf>,
) -> Result<ConfigCommandExecution> {
execute_config_command(root, config_path)
}
pub(super) fn execute_config_export(
root: Option<PathBuf>,
config_path: Option<PathBuf>,
) -> Result<ConfigCommandExecution> {
execute_config_command(root, config_path)
}
pub(super) fn render_config_stdout(execution: &ConfigCommandExecution) -> Result<String> {
toml::to_string_pretty(&execution.config).map_err(|source| CliError::RenderConfig { source })
}
pub(super) fn config_payload(execution: &ConfigCommandExecution) -> Result<Value> {
serde_json::to_value(&execution.config).map_err(|source| CliError::RenderJson { source })
}
fn execute_config_command(
root: Option<PathBuf>,
config_path: Option<PathBuf>,
) -> Result<ConfigCommandExecution> {
let root = root.unwrap_or(
std::env::current_dir().map_err(|source| CliError::CurrentDirectory { source })?,
);
let loaded = load_resolved_config(&LoadOptions {
root,
config: config_path,
})?;
Ok(ConfigCommandExecution::new(loaded.config))
}