From d95a7b6b80a300f64c4f29f3cf748e243ebadeb7 Mon Sep 17 00:00:00 2001 From: Matt Miller Date: Mon, 18 Mar 2024 14:28:48 -0400 Subject: [PATCH] consolidating the instance config --- wpdev_core/src/config.rs | 62 +++++++++++++++++++++---------- wpdev_core/src/docker/instance.rs | 35 ++++++----------- wpdev_core/src/lib.rs | 5 ++- 3 files changed, 58 insertions(+), 44 deletions(-) diff --git a/wpdev_core/src/config.rs b/wpdev_core/src/config.rs index 0b22df4..d57caad 100644 --- a/wpdev_core/src/config.rs +++ b/wpdev_core/src/config.rs @@ -18,29 +18,56 @@ use crate::AppConfig; pub async fn read_or_create_config() -> Result { info!("Reading or creating config file"); - let config_dir = - dirs::config_dir().ok_or_else(|| anyhow::anyhow!("Failed to find config directory"))?; - let config_dir = config_dir.join("wpdev"); + let config_dir = get_config_dir().await?; fs::create_dir_all(&config_dir) .await .context("Failed to create config directory")?; let config_path = config_dir.join("config.toml"); + let default_config_dir = config_dir.join("instances"); match fs::read_to_string(&config_path).await { Ok(contents) => { info!("Reading config file from {:?}", config_path); - let config: AppConfig = toml::from_str(&contents) + let mut config: AppConfig = toml::from_str(&contents) .with_context(|| format!("Failed to parse config file at {:?}", config_path))?; + info!("Checking if custom root is set"); + if config.custom_root.is_none() { + info!("Custom root not found in config, setting to default value"); + config.custom_root = Some(default_config_dir); + } + info!("Config file read successfully"); Ok(config) } Err(_) => { info!("Creating new config file at {:?}", config_path); - Ok(AppConfig::default()) + let config = AppConfig { + custom_root: Some(config_dir.join("instances")), + ..AppConfig::default() + }; + info!("Writing default config to {:?}", config_path); + Ok(config) } } } +pub(crate) async fn get_config_dir() -> Result { + info!("Getting root directory"); + let config_dir = dirs::config_dir().context("Failed to find home directory")?; + let config_dir = config_dir.join("wpdev"); + Ok(config_dir) +} + +pub(crate) async fn get_instance_dir() -> Result { + info!("Getting instance directory"); + let config = read_or_create_config().await?; + let instance_dir = config + .custom_root + .ok_or_else(|| AnyhowError::msg("Custom root not found"))?; + info!("Instance directory: {:?}", instance_dir); + Ok(instance_dir) +} + pub async fn image_exists(image_name: &str) -> Result { info!("Checking if image {} exists locally", image_name); let docker = Docker::connect_with_defaults()?; @@ -272,14 +299,16 @@ server {{ } pub(crate) async fn generate_wpcli_config( - config: &crate::AppConfig, + config: &PathBuf, instance_label: &str, home_dir: &PathBuf, ) -> Result<(), AnyhowError> { info!("Generating wp-cli config"); let instance_dir = home_dir.join(format!( "{}/{}-{}/", - &config.custom_root, + &config + .to_str() + .ok_or_else(|| AnyhowError::msg("Config directory not found"))?, crate::NETWORK_NAME, instance_label )); @@ -330,13 +359,9 @@ define('WP_DEBUG', false); pub async fn read_instance_data_from_toml(instance_label: &str) -> Result { info!("Reading instance data from toml"); - let config = read_or_create_config() - .await - .context("Failed to read config")?; - let home_dir = dirs::home_dir().context("Failed to find home directory")?; - let instance_dir = home_dir - .join(&config.custom_root) - .join(format!("{}/instance.toml", instance_label)); + let instance_config_dir = get_instance_dir().await?; + let instance_dir = instance_config_dir.join(format!("{}/instance.toml", instance_label)); + info!("Reading instance data from {:?}", instance_dir); if !instance_dir.exists() { error!("Instance file not found at {:?}", instance_dir); @@ -363,14 +388,13 @@ pub(crate) async fn parse_instance_data( env_vars: &EnvVars, nginx_port: &u32, adminer_port: &u32, - config: &crate::AppConfig, - home_dir: &PathBuf, instance_label: &str, ) -> Result { info!("Parsing instance data"); - let instance_dir = home_dir.join(format!( - "{}/{}-{}/instance.toml", - &config.custom_root, + let instance_config_dir = get_instance_dir().await?; + let config = read_or_create_config().await?; + let instance_dir = instance_config_dir.join(format!( + "{}-{}/instance.toml", crate::NETWORK_NAME, instance_label )); diff --git a/wpdev_core/src/docker/instance.rs b/wpdev_core/src/docker/instance.rs index 0754ad1..97d4f62 100644 --- a/wpdev_core/src/docker/instance.rs +++ b/wpdev_core/src/docker/instance.rs @@ -9,7 +9,7 @@ use std::collections::HashMap; use std::path::PathBuf; use tokio::fs; -use crate::config::{self, read_or_create_config}; +use crate::config::{self}; use crate::docker::config::{ configure_adminer_container, configure_mysql_container, configure_nginx_container, configure_wordpress_container, @@ -103,7 +103,7 @@ impl Instance { instance_label: &str, user_env_vars: ContainerEnvVars, ) -> Result { - let config = config::read_or_create_config().await?; + let instance_dir = config::get_instance_dir().await?; let home_dir = dirs::home_dir().ok_or_else(|| AnyhowError::msg("Home directory not found"))?; @@ -125,9 +125,8 @@ impl Instance { labels.insert("nginx_port".to_string(), nginx_port_str); labels.insert("adminer_port".to_string(), adminer_port_str); - let instance_path = home_dir.join(PathBuf::from(format!( - "{}/{}-{}", - &config.custom_root, + let instance_path = instance_dir.join(PathBuf::from(format!( + "{}-{}", crate::NETWORK_NAME, instance_label ))); @@ -151,15 +150,9 @@ impl Instance { ) .await?; - let wordpress_data = config::parse_instance_data( - &env_vars, - &nginx_port, - &adminer_port, - &config, - &home_dir, - &instance_label, - ) - .await?; + let wordpress_data = + config::parse_instance_data(&env_vars, &nginx_port, &adminer_port, &instance_label) + .await?; let mut instance = Instance { uuid: format!("{}-{}", crate::NETWORK_NAME, instance_label.to_string()), @@ -172,7 +165,7 @@ impl Instance { wordpress_data: Some(wordpress_data), }; - config::generate_wpcli_config(&config, instance_label, &home_dir).await?; + config::generate_wpcli_config(&instance_dir, instance_label, &home_dir).await?; let containers = vec![ (mysql_options, "mysql"), @@ -529,14 +522,10 @@ impl Instance { async fn purge_instances(instance: InstanceSelection) -> Result<()> { info!("Starting to purge instances"); - let config = read_or_create_config() - .await - .context("Failed to read config")?; - let home_dir = dirs::home_dir().context("Failed to find home directory")?; - let config_dir = home_dir.join(&config.custom_root); + let instance_dir = config::get_instance_dir().await?; let docker = Docker::connect_with_defaults().context("Failed to connect to Docker")?; - if !config_dir.exists() { + if !instance_dir.exists() { info!("Instance directory not found"); return Ok(()); } @@ -544,7 +533,7 @@ async fn purge_instances(instance: InstanceSelection) -> Result<()> { match instance { InstanceSelection::All => { info!("Pruning all instances"); - let p = &config_dir; + let p = &instance_dir; let path = p.to_str().context("Instance directory not found")?; let networks = docker .list_networks::(None) @@ -578,7 +567,7 @@ async fn purge_instances(instance: InstanceSelection) -> Result<()> { } InstanceSelection::One(instance_uuid) => { info!("Removing instance: {}", instance_uuid); - let p = &config_dir; + let p = &instance_dir; let path = p.to_str().context("Instance directory not found")?; let instance_path = format!("{}/{}", path, instance_uuid); if !PathBuf::from(&instance_path).exists() { diff --git a/wpdev_core/src/lib.rs b/wpdev_core/src/lib.rs index cf3456c..e8e0878 100644 --- a/wpdev_core/src/lib.rs +++ b/wpdev_core/src/lib.rs @@ -1,5 +1,6 @@ use serde::{Deserialize, Serialize}; use std::net::{IpAddr, Ipv4Addr}; +use std::path::PathBuf; pub mod config; pub mod docker; @@ -15,7 +16,7 @@ pub const WORDPRESS_CLI_IMAGE: &str = "wordpress:cli"; #[derive(Serialize, Deserialize)] #[serde(default)] pub struct AppConfig { - pub custom_root: String, + pub custom_root: Option, pub docker_images: Vec, pub log_level: String, pub enable_frontend: bool, @@ -32,7 +33,7 @@ pub struct AppConfig { impl Default for AppConfig { fn default() -> Self { AppConfig { - custom_root: String::from(".config/wpdev/instances"), + custom_root: None, docker_images: vec![ WORDPRESS_IMAGE.to_string(), NGINX_IMAGE.to_string(),