diff --git a/src/cli.rs b/src/cli.rs index 8182ee9..c50885e 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -461,9 +461,11 @@ Run `floo notifications list` to see the available categories." /// Inspect custom domains declared in floo.app.toml. #[command( subcommand, - after_help = "Declare [domains.\"\"] in floo.app.toml and release to prod. -Publish the DNS records printed by the deploy or `floo domains show `, -then run `floo domains watch `. Remove the block to retire the domain." + after_help = "Declare [domains.\"\"] in floo.app.toml. A domain belongs to prod +by default; `environment = \"dev\"` attaches it to dev instead, and that +environment's deploy applies it. Publish the DNS records printed by the deploy +or `floo domains show `, then run `floo domains watch `. Remove the +block to retire the domain." )] Domains(DomainsCommands), diff --git a/src/commands/domains.rs b/src/commands/domains.rs index fa3c952..ea1dd8d 100644 --- a/src/commands/domains.rs +++ b/src/commands/domains.rs @@ -24,7 +24,7 @@ pub fn list(app: Option<&str>) { if !output::is_json_mode() { output::info( &format!( - "No custom domains on {app_name}. Declare [domains.\"\"] in floo.app.toml and release to prod." + "No custom domains on {app_name}. Declare [domains.\"\"] in floo.app.toml and deploy its environment (prod unless the block sets `environment = \"dev\"`)." ), None, ); @@ -174,7 +174,7 @@ pub fn watch(hostname: &str, app: Option<&str>, timeout_secs: u64) { output::error( &format!("Domain {hostname} is {}.", domain_status(Some("removed"))), &ErrorCode::DomainNotFound, - Some("Re-declare the domain in floo.app.toml and release to prod to restore."), + Some("Re-declare the domain in floo.app.toml and deploy its environment to restore."), ); process::exit(1); } diff --git a/src/project_config/app_config.rs b/src/project_config/app_config.rs index 2ac8e93..4c2f650 100644 --- a/src/project_config/app_config.rs +++ b/src/project_config/app_config.rs @@ -166,13 +166,25 @@ pub struct ManagedServiceBlock { const VALID_MANAGED_SERVICE_TYPES: &[&str] = &["postgres", "redis", "storage"]; -/// A custom domain declared as `[domains.""]` on the prod release. -/// `service` selects its backend; omission uses the app default. Removing the -/// block retires the domain while retaining its certificate for seven days. +/// A custom domain declared as `[domains.""]`. It belongs to prod by +/// default; `environment = "dev"` attaches it to dev instead, and that +/// environment's deploy applies it. `service` selects its backend; omission uses +/// the app default. Removing the block retires the domain while retaining its +/// certificate for seven days. #[derive(Debug, Serialize, Default, Clone)] pub struct DomainBlock { #[serde(skip_serializing_if = "Option::is_none")] pub service: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub environment: Option, +} + +/// The environment a custom domain attaches to; unset means prod. +#[derive(Debug, Serialize, Clone, Copy, PartialEq, Eq)] +#[serde(rename_all = "lowercase")] +pub enum DomainEnvironment { + Dev, + Prod, } impl<'de> Deserialize<'de> for DomainBlock { @@ -181,17 +193,31 @@ impl<'de> Deserialize<'de> for DomainBlock { #[serde(deny_unknown_fields)] struct Fields { service: Option, + environment: Option, } // Keep serde's key-specific rejection and attach migration guidance at // this boundary so both app and service config parsers explain removal. let fields = Fields::deserialize(deserializer).map_err(|err| { serde::de::Error::custom(format!( - "{err}; domain blocks accept only `service`; remove the block to remove the domain" + "{err}; domain blocks accept only `service` and `environment`; \ + remove the block to remove the domain" )) })?; + let environment = match fields.environment.as_deref() { + None => None, + Some("dev") => Some(DomainEnvironment::Dev), + Some("prod") => Some(DomainEnvironment::Prod), + Some(other) => { + return Err(serde::de::Error::custom(format!( + "domain `environment` must be \"dev\" or \"prod\", got \"{other}\"; \ + omit it to attach the domain to prod" + ))) + } + }; Ok(Self { service: fields.service, + environment, }) } } @@ -2079,17 +2105,65 @@ name = "my-app" service = "web" [domains."api.example.com"] + +[domains."dev.example.com"] +environment = "dev" + +[domains."www.example.com"] +environment = "prod" "#, ) .unwrap(); let config = load_app_config(dir.path()).unwrap().unwrap(); - assert_eq!(config.domains.len(), 2); + assert_eq!(config.domains.len(), 4); assert_eq!( config.domains["app.example.com"].service.as_deref(), Some("web") ); - assert!(config.domains.contains_key("api.example.com")); + assert_eq!(config.domains["app.example.com"].environment, None); + assert_eq!(config.domains["api.example.com"].environment, None); + assert_eq!( + config.domains["dev.example.com"].environment, + Some(DomainEnvironment::Dev) + ); + assert_eq!( + config.domains["www.example.com"].environment, + Some(DomainEnvironment::Prod) + ); + + // An omitted environment stays omitted, so existing files round-trip unchanged. + let serialized = toml::to_string(&config.domains["app.example.com"]).unwrap(); + assert_eq!(serialized.trim(), r#"service = "web""#); + let serialized = toml::to_string(&config.domains["dev.example.com"]).unwrap(); + assert_eq!(serialized.trim(), r#"environment = "dev""#); + } + + #[test] + fn test_load_app_config_rejects_unknown_domain_environment() { + let _guard = crate::output::GLOBAL_MODE_LOCK.lock().unwrap(); + crate::output::set_json_mode(false); + crate::output::set_dry_run_mode(false); + let dir = TempDir::new().unwrap(); + fs::write( + dir.path().join(super::super::APP_CONFIG_FILE), + r#"[app] +name = "my-app" +[domains."app.example.com"] +environment = "preview" +"#, + ) + .unwrap(); + + let err = load_app_config(dir.path()).unwrap_err(); + assert_eq!(err.code, ErrorCode::InvalidProjectConfig); + assert!( + err.message + .contains(r#"domain `environment` must be "dev" or "prod", got "preview""#), + "got: {}", + err.message + ); + assert!(!err.suggestion.unwrap().contains("floo update")); } #[test] diff --git a/src/project_config/mod.rs b/src/project_config/mod.rs index e1e9590..afd526b 100644 --- a/src/project_config/mod.rs +++ b/src/project_config/mod.rs @@ -38,7 +38,7 @@ pub(super) fn toml_parse_error(file_label: &str, err: toml::de::Error) -> crate: None => field.to_string(), }; let correction = if raw.contains("remove the block to remove the domain") { - "Domain blocks accept only `service`; remove the block to remove the domain." + "Domain blocks accept only `service` and `environment`; remove the block to remove the domain." } else { "Remove or correct it." }; diff --git a/src/project_config/service_config.rs b/src/project_config/service_config.rs index 92940c6..054a15d 100644 --- a/src/project_config/service_config.rs +++ b/src/project_config/service_config.rs @@ -968,6 +968,30 @@ domain = "app.example.com" assert!(err.suggestion.unwrap().contains("`service.domain`")); } + #[test] + fn test_load_service_config_accepts_dev_domain() { + let dir = TempDir::new().unwrap(); + fs::write( + dir.path().join(super::super::SERVICE_CONFIG_FILE), + r#"[app] +name = "my-app" +[service] +name = "web" +type = "web" +port = 3000 +[domains."dev.example.com"] +environment = "dev" +"#, + ) + .unwrap(); + + let config = load_service_config(dir.path()).unwrap().unwrap(); + assert_eq!( + config.domains["dev.example.com"].environment, + Some(super::super::app_config::DomainEnvironment::Dev) + ); + } + #[test] fn test_load_service_config_with_resources() { let dir = TempDir::new().unwrap(); diff --git a/tests/fixtures/manifests/valid/server_supported_keys.toml b/tests/fixtures/manifests/valid/server_supported_keys.toml index 880ef6f..c72315e 100644 --- a/tests/fixtures/manifests/valid/server_supported_keys.toml +++ b/tests/fixtures/manifests/valid/server_supported_keys.toml @@ -49,6 +49,7 @@ size = "large" [managed.db.users] [domains."app.example.com"] service = "web" +environment = "dev" [edge] rules = [{ action = "allow", cidr = "10.0.0.0/8" }] default_action = "deny"