diff --git a/README.md b/README.md index 2c48774..fcf2a25 100644 --- a/README.md +++ b/README.md @@ -154,13 +154,14 @@ Identity: `SHOPWARE_SHOP_ID`, `SHOPWARE_DEPLOY_ENV`, optional Does not overwrite the whole `.env`. Does not invent MYSQL passwords or `APP_URL`. Does not generate `APP_SECRET` (`shopware-cli project create` already writes it). `--shop-id` is required unless `SHOPWARE_SHOP_ID` is -already set. `--env` defaults to `live`. `--vps` comments out -`COMPOSE_PROJECT_NAME`. `.env` may still be missing; missing keys are +already set. `--env` defaults to `live`. Uncommented `COMPOSE_PROJECT_NAME` +lines are always commented out (Compose SoT is `SHOPWARE_SHOP_ID` + +`SHOPWARE_DEPLOY_ENV`). `.env` may still be missing; missing keys are merged from `.env.example`. ```bash -fyrst-cli shopware env init --shop-id acme --vps --dry-run -fyrst-cli shopware env init --shop-id acme --env live --vps --image ghcr.io/example/acme +fyrst-cli shopware env init --shop-id acme --dry-run +fyrst-cli shopware env init --shop-id acme --env live --image ghcr.io/example/acme ``` ### db import diff --git a/docs/command-matrix.md b/docs/command-matrix.md index 493a804..734667b 100644 --- a/docs/command-matrix.md +++ b/docs/command-matrix.md @@ -29,7 +29,7 @@ those scripts. Use the CLI column. | Historical overlay (removed) | CLI | Nested verbs | Flags | Status | | --- | --- | --- | --- | --- | -| `deploy/init-env.sh` | `fyrst-cli shopware env init` | `init` | `--shop-id`, `--env`, `--image`, `--vps`, `--dry-run` | **implemented** | +| `deploy/init-env.sh` | `fyrst-cli shopware env init` | `init` | `--shop-id`, `--env`, `--image`, `--dry-run` | **implemented** | | `deploy/vps-release.sh` | `fyrst-cli shopware deploy release` | `release` | `--dry-run`, `--skip-pull` | **implemented** | | `deploy/vps-rollback.sh` | `fyrst-cli shopware deploy rollback` | `rollback` | `--dry-run`, `--skip-pull` | **implemented** | | `restore_db_*` (sync-runtime) | `fyrst-cli shopware db import` | `import` | `--file`, `--dry-run`, `--allow-live` | **implemented** | @@ -90,7 +90,7 @@ Rewrite: `APP_URL` only. CI `VPS_*` secrets stay secrets — they are not shop ## Exact env init CLI ```text -fyrst-cli shopware env init [--shop-id SLUG] [--env live|staging|playground|dev] [--image REPO] [--vps] [--dry-run] +fyrst-cli shopware env init [--shop-id SLUG] [--env live|staging|playground|dev] [--image REPO] [--dry-run] ``` ## Exact deploy release CLI @@ -179,8 +179,9 @@ shell expansion**), same parser as import (`src/shopware/envfile.rs`). 3. `--env` is `live` | `staging` | `playground` | `dev`. Default `live` when unset/empty; keep an existing non-empty value. 4. `--image` sets `IMAGE` (no whitespace). Unset leaves `IMAGE` as-is. -5. `--vps` comments out uncommented `COMPOSE_PROJECT_NAME=…` lines (create - footgun on a VPS). Does not leave an empty `COMPOSE_PROJECT_NAME=`. +5. Always comments out uncommented `COMPOSE_PROJECT_NAME=…` lines (create + writes `COMPOSE_PROJECT_NAME=sw-…`; Compose SoT is `SHOPWARE_SHOP_ID` + + `SHOPWARE_DEPLOY_ENV`). Does not leave an empty `COMPOSE_PROJECT_NAME=`. 6. `--dry-run` prints the summary and does not write `.env`. 7. After a real write: `chmod 600 .env`. diff --git a/src/cli.rs b/src/cli.rs index 46973cc..dcfc5af 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -94,8 +94,8 @@ pub enum Command { deploy (VPS compose), db (SQL import), sync (between environments / workdir), \ backup (off-host disaster recovery).\n\n\ `env init` is implemented: finish shop-root `.env` after create + Flex (merge missing \ -keys from `.env.example`, shop id / deploy env, optional IMAGE, `--vps` \ -comments COMPOSE_PROJECT_NAME). Does not generate APP_SECRET. `--dry-run` prints the \ +keys from `.env.example`, shop id / deploy env, optional IMAGE; always comments out \ +COMPOSE_PROJECT_NAME). Does not generate APP_SECRET. `--dry-run` prints the \ plan and does not write. Passwords are never printed.\n\n\ `db import` is implemented: MySQL/MariaDB client import (Compose `mysql` exec, else a \ one-shot client image for DATABASE_URL). `sync apply` uses that same import module, \ @@ -182,13 +182,16 @@ impl DeployEnv { #[derive(Debug, Args)] #[command( after_help = "Does not overwrite the whole .env. Does not invent MYSQL passwords or APP_URL. \ -Does not generate APP_SECRET (shopware-cli project create writes that).\n\n\ +Does not generate APP_SECRET (shopware-cli project create writes that). \ +Always comments out uncommented COMPOSE_PROJECT_NAME=… lines (create writes \ +COMPOSE_PROJECT_NAME=sw-…; Compose SoT is SHOPWARE_SHOP_ID + SHOPWARE_DEPLOY_ENV). \ +Does not leave an empty COMPOSE_PROJECT_NAME=.\n\n\ Environment:\n \ COMPOSE_DIR Shop checkout (default: walk from cwd for .env / .env.example + deploy/)\n\n\ Examples:\n \ fyrst-cli shopware env init --shop-id acme\n \ - fyrst-cli shopware env init --shop-id acme --env live --vps --image ghcr.io/example/acme\n \ - fyrst-cli shopware env init --shop-id acme --vps --dry-run\n" + fyrst-cli shopware env init --shop-id acme --env live --image ghcr.io/example/acme\n \ + fyrst-cli shopware env init --shop-id acme --dry-run\n" )] pub struct InitEnvArgs { /// Shop slug (required unless SHOPWARE_SHOP_ID is already non-empty in `.env`) @@ -203,10 +206,6 @@ pub struct InitEnvArgs { #[arg(long = "image", value_name = "REPO")] pub image: Option, - /// Comment out COMPOSE_PROJECT_NAME=… lines (create footgun on a VPS) - #[arg(long)] - pub vps: bool, - /// Print the summary; do not write .env #[arg(long)] pub dry_run: bool, @@ -510,7 +509,6 @@ mod tests { "staging", "--image", "ghcr.io/example/acme", - "--vps", "--dry-run", ]) .unwrap(); @@ -521,13 +519,31 @@ mod tests { assert_eq!(op.shop_id.as_deref(), Some("acme")); assert_eq!(op.env, Some(DeployEnv::Staging)); assert_eq!(op.image.as_deref(), Some("ghcr.io/example/acme")); - assert!(op.vps); assert!(op.dry_run); } other => panic!("unexpected parse: {other:?}"), } } + #[test] + fn env_init_rejects_vps() { + let err = Cli::try_parse_from([ + "fyrst-cli", + "shopware", + "env", + "init", + "--shop-id", + "acme", + "--vps", + ]) + .unwrap_err(); + let text = err.to_string(); + assert!( + text.contains("unexpected argument") || text.contains("unexpected"), + "{text}" + ); + } + #[test] fn env_init_rejects_generate_app_secret() { let err = Cli::try_parse_from([ diff --git a/src/shopware/envfile.rs b/src/shopware/envfile.rs index 4d011b5..bd5a500 100644 --- a/src/shopware/envfile.rs +++ b/src/shopware/envfile.rs @@ -47,8 +47,8 @@ pub fn has_key(contents: &str, key: &str) -> bool { pub const MERGE_FROM_EXAMPLE_HEADER: &str = "# --- missing keys merged from .env.example by deploy/init-env.sh ---"; -pub const VPS_COMMENT_SUFFIX: &str = - " # commented by deploy/init-env.sh --vps (restore for local project dev)"; +pub const COMPOSE_PROJECT_NAME_COMMENT_SUFFIX: &str = + " # commented by deploy/init-env.sh (restore for local project dev)"; /// Replace every uncommented `KEY=` line, or append `KEY=value`. /// Preserves an `export` prefix. Does not quote `value` (overlay `env_set_key`). @@ -83,7 +83,7 @@ pub fn set_key(contents: &str, key: &str, value: &str) -> String { out } -/// Comment uncommented `COMPOSE_PROJECT_NAME=` lines (overlay `--vps`). +/// Comment uncommented `COMPOSE_PROJECT_NAME=` lines (create footgun). /// Returns the rewritten file and how many lines were commented. pub fn comment_compose_project_name(contents: &str) -> (String, usize) { let mut out = String::new(); @@ -93,7 +93,7 @@ pub fn comment_compose_project_name(contents: &str) -> (String, usize) { if uncommented_assignment(line, "COMPOSE_PROJECT_NAME").is_some() { out.push_str("# "); out.push_str(line); - out.push_str(VPS_COMMENT_SUFFIX); + out.push_str(COMPOSE_PROJECT_NAME_COMMENT_SUFFIX); out.push('\n'); n += 1; } else { @@ -331,12 +331,13 @@ MYSQL_PASSWORD=s3cret assert!(!out .lines() .any(|l| l.trim_start().starts_with("COMPOSE_PROJECT_NAME="))); - assert!(out.contains( - "# COMPOSE_PROJECT_NAME=sw-shop-acme # commented by deploy/init-env.sh --vps" - )); - assert!(out.contains( - "# export COMPOSE_PROJECT_NAME=other # commented by deploy/init-env.sh --vps" - )); + assert!( + out.contains("# COMPOSE_PROJECT_NAME=sw-shop-acme # commented by deploy/init-env.sh") + ); + assert!( + out.contains("# export COMPOSE_PROJECT_NAME=other # commented by deploy/init-env.sh") + ); + assert!(!out.contains("--vps")); assert!(out.contains("# COMPOSE_PROJECT_NAME=keep-commented")); assert!(out.contains("MYSQL_PASSWORD=s3cret")); assert!(!out.contains("COMPOSE_PROJECT_NAME=\n")); diff --git a/src/shopware/init_env.rs b/src/shopware/init_env.rs index 6b5358d..bf42e2a 100644 --- a/src/shopware/init_env.rs +++ b/src/shopware/init_env.rs @@ -1,10 +1,12 @@ //! `fyrst-cli shopware env init` — finish shop-root `.env` after create + Flex. //! -//! Sets fyrst identity (shop id, deploy env, optional IMAGE, `--vps`) and -//! merges missing keys from `.env.example`. Does not overwrite the whole file, -//! does not invent MYSQL passwords or `APP_URL`, and does not generate or -//! rewrite `APP_SECRET` (`shopware-cli project create` writes that). Never -//! prints secrets. This is not a dump command. +//! Sets fyrst identity (shop id, deploy env, optional IMAGE) and merges +//! missing keys from `.env.example`. Always comments out uncommented +//! `COMPOSE_PROJECT_NAME` lines (create writes `COMPOSE_PROJECT_NAME=sw-…`; +//! Compose SoT is `SHOPWARE_SHOP_ID` + `SHOPWARE_DEPLOY_ENV`). Does not +//! overwrite the whole file, does not invent MYSQL passwords or `APP_URL`, +//! and does not generate or rewrite `APP_SECRET` (`shopware-cli project +//! create` writes that). Never prints secrets. This is not a dump command. use super::env::resolve_compose_dir_init; use super::envfile::{ @@ -29,8 +31,7 @@ pub(crate) struct Plan { pub deploy_env: String, pub deploy_env_changed: bool, pub image_set: Option, - pub vps: bool, - pub vps_commented: usize, + pub compose_project_name_commented: usize, } impl std::fmt::Debug for Plan { @@ -46,8 +47,10 @@ impl std::fmt::Debug for Plan { .field("deploy_env", &self.deploy_env) .field("deploy_env_changed", &self.deploy_env_changed) .field("image_set", &self.image_set) - .field("vps", &self.vps) - .field("vps_commented", &self.vps_commented) + .field( + "compose_project_name_commented", + &self.compose_project_name_commented, + ) .finish_non_exhaustive() } } @@ -162,12 +165,7 @@ pub(crate) fn plan( contents = set_key(&contents, "IMAGE", image); } - let mut vps_commented = 0; - if args.vps { - let (next, n) = comment_compose_project_name(&contents); - contents = next; - vps_commented = n; - } + let (contents, compose_project_name_commented) = comment_compose_project_name(&contents); let shop_id_changed = existing_shop_id != shop_id; let deploy_env_changed = existing_deploy_env != deploy_env; @@ -183,8 +181,7 @@ pub(crate) fn plan( deploy_env, deploy_env_changed, image_set, - vps: args.vps, - vps_commented, + compose_project_name_commented, }) } @@ -279,17 +276,15 @@ pub(crate) fn summary_text(plan: &Plan) -> String { let _ = writeln!(s, " set IMAGE={image}"); changes += 1; } - if plan.vps { - if plan.vps_commented > 0 { - let _ = writeln!( - s, - " commented {} COMPOSE_PROJECT_NAME=… line(s) (--vps)", - plan.vps_commented - ); - changes += 1; - } else { - let _ = writeln!(s, " --vps: no uncommented COMPOSE_PROJECT_NAME=… lines"); - } + if plan.compose_project_name_commented > 0 { + let _ = writeln!( + s, + " commented {} COMPOSE_PROJECT_NAME=… line(s)", + plan.compose_project_name_commented + ); + changes += 1; + } else { + let _ = writeln!(s, " no uncommented COMPOSE_PROJECT_NAME=… lines"); } if changes == 0 { let _ = writeln!(s, " no changes (already up to date)"); @@ -369,7 +364,6 @@ mod tests { shop_id: Some("acme".into()), env: None, image: None, - vps: false, dry_run: true, } } @@ -390,14 +384,13 @@ COMPOSE_PROJECT_NAME=sw-shop-acme " )); let mut a = args(); - a.vps = true; a.image = Some("ghcr.io/example/acme".into()); let p = plan(&process(shop.path()), shop.path(), &a).unwrap(); assert!(p.dry_run); assert_eq!(p.shop_id, "acme"); assert_eq!(p.deploy_env, "live"); assert_eq!(p.image_set.as_deref(), Some("ghcr.io/example/acme")); - assert_eq!(p.vps_commented, 1); + assert_eq!(p.compose_project_name_commented, 1); assert_eq!(last_value(p.contents(), "APP_SECRET"), EXISTING_SECRET); assert!(!has_key(p.contents(), "COMPOSE_PROJECT_NAME")); let text = summary_text(&p); @@ -406,6 +399,7 @@ COMPOSE_PROJECT_NAME=sw-shop-acme assert!(text.contains("set SHOPWARE_DEPLOY_ENV=live")); assert!(text.contains("set IMAGE=ghcr.io/example/acme")); assert!(text.contains("commented 1 COMPOSE_PROJECT_NAME")); + assert!(!text.contains("--vps")); assert!(text.contains("APP_SECRET")); assert!(!text.contains("generate-app-secret")); assert!(!text.contains(MYSQL_PASS)); @@ -419,7 +413,7 @@ COMPOSE_PROJECT_NAME=sw-shop-acme } #[test] - fn write_path_sets_keys_merges_example_and_comments_vps() { + fn write_path_sets_keys_merges_example_and_comments_compose_project_name() { let shop = TempShop::new("write"); shop.write_env(&format!( "\ @@ -439,7 +433,6 @@ NEW_FROM_EXAMPLE=1 ); let mut a = args(); a.dry_run = false; - a.vps = true; a.env = Some(DeployEnv::Staging); let p = plan(&process(shop.path()), shop.path(), &a).unwrap(); assert!(p.merged_keys.contains(&"NEW_FROM_EXAMPLE".into())); @@ -450,13 +443,62 @@ NEW_FROM_EXAMPLE=1 assert!(p.contents().contains(MERGE_FROM_EXAMPLE_HEADER)); assert!(p.contents().contains("NEW_FROM_EXAMPLE=1")); assert!(!has_key(p.contents(), "COMPOSE_PROJECT_NAME")); - assert!(p.contents().contains( - "# COMPOSE_PROJECT_NAME=sw-shop-acme # commented by deploy/init-env.sh --vps" - )); + assert!(p + .contents() + .contains("# COMPOSE_PROJECT_NAME=sw-shop-acme # commented by deploy/init-env.sh")); + assert!(!p.contents().contains("--vps")); let text = summary_text(&p); assert!(!text.contains(MYSQL_PASS)); + assert!(!text.contains("--vps")); assert!(text.contains("merge missing keys from .env.example: NEW_FROM_EXAMPLE")); assert!(text.contains("set SHOPWARE_DEPLOY_ENV=staging")); + assert!(text.contains("commented 1 COMPOSE_PROJECT_NAME")); + } + + #[test] + fn reports_none_when_compose_project_name_already_commented() { + let shop = TempShop::new("already-commented"); + shop.write_env( + "\ +SHOPWARE_SHOP_ID=acme +SHOPWARE_DEPLOY_ENV=live +# COMPOSE_PROJECT_NAME=sw-shop-acme +", + ); + let mut a = args(); + a.shop_id = None; + let p = plan(&process(shop.path()), shop.path(), &a).unwrap(); + assert_eq!(p.compose_project_name_commented, 0); + assert!(p.contents().contains("# COMPOSE_PROJECT_NAME=sw-shop-acme")); + let text = summary_text(&p); + assert!(text.contains("no uncommented COMPOSE_PROJECT_NAME")); + assert!(!text.contains("commented 0")); + assert!(!text.contains("--vps")); + assert!(text.contains("no changes (already up to date)")); + } + + #[test] + fn copy_from_example_comments_compose_project_name() { + let shop = TempShop::new("copy-cpn"); + shop.write_example( + "\ +SHOPWARE_SHOP_ID= +COMPOSE_PROJECT_NAME=sw-shop-acme +MYSQL_PASSWORD=example-secret +", + ); + let p = plan(&process(shop.path()), shop.path(), &args()).unwrap(); + assert!(p.copied); + assert_eq!(p.compose_project_name_commented, 1); + assert!(!has_key(p.contents(), "COMPOSE_PROJECT_NAME")); + assert!(p + .contents() + .contains("# COMPOSE_PROJECT_NAME=sw-shop-acme # commented by deploy/init-env.sh")); + let text = summary_text(&p); + assert!(text.contains("copy .env.example → .env")); + assert!(text.contains("commented 1 COMPOSE_PROJECT_NAME")); + assert!(!text.contains("--vps")); + assert!(!text.contains("example-secret")); } #[test] @@ -577,7 +619,10 @@ MYSQL_PASSWORD=example-secret let p = plan(&process(shop.path()), shop.path(), &a).unwrap(); assert_eq!(p.deploy_env, "playground"); assert!(!p.deploy_env_changed); - assert!(summary_text(&p).contains("no changes (already up to date)")); + let text = summary_text(&p); + assert!(text.contains("no uncommented COMPOSE_PROJECT_NAME")); + assert!(!text.contains("--vps")); + assert!(text.contains("no changes (already up to date)")); } #[test] diff --git a/tests/cli.rs b/tests/cli.rs index 3c1355d..a8b3854 100644 --- a/tests/cli.rs +++ b/tests/cli.rs @@ -81,6 +81,7 @@ fn shopware_help_prints_command_tree() { "backup restore", "sync sync", "backup backup", + "--vps", ] { assert!( !help.contains(gone), diff --git a/tests/init_env.rs b/tests/init_env.rs index 6a3d956..48e4283 100644 --- a/tests/init_env.rs +++ b/tests/init_env.rs @@ -108,12 +108,16 @@ fn init_env_help_lists_flags() { "--shop-id", "--env", "--image", - "--vps", "--dry-run", "COMPOSE_DIR", + "COMPOSE_PROJECT_NAME", ] { assert!(help.contains(needle), "missing {needle} in:\n{help}"); } + assert!( + !help.contains("--vps"), + "env init --help must not list --vps:\n{help}" + ); assert!( !help.contains("generate-app-secret"), "env init --help must not list --generate-app-secret:\n{help}" @@ -146,7 +150,6 @@ COMPOSE_PROJECT_NAME=sw-shop-acme &[ "--shop-id", "acme", - "--vps", "--image", "ghcr.io/example/acme", "--dry-run", @@ -165,6 +168,7 @@ COMPOSE_PROJECT_NAME=sw-shop-acme assert!(log.contains("set SHOPWARE_DEPLOY_ENV=live"), "{log}"); assert!(log.contains("set IMAGE=ghcr.io/example/acme"), "{log}"); assert!(log.contains("commented 1 COMPOSE_PROJECT_NAME"), "{log}"); + assert!(!log.contains("--vps"), "{log}"); assert!(!log.contains("generate-app-secret"), "{log}"); assert!(!log.contains("set APP_SECRET"), "{log}"); assert!( @@ -178,7 +182,7 @@ COMPOSE_PROJECT_NAME=sw-shop-acme } #[test] -fn write_merges_example_comments_vps_and_chmod() { +fn write_merges_example_comments_compose_project_name_and_chmod() { let shop = TempShop::new("write"); fs::write( shop.path().join(".env"), @@ -202,10 +206,7 @@ NEW_FROM_EXAMPLE=1 ", ) .unwrap(); - let out = init_env( - shop.path(), - &["--shop-id", "acme", "--env", "staging", "--vps"], - ); + let out = init_env(shop.path(), &["--shop-id", "acme", "--env", "staging"]); assert_eq!( out.status.code(), Some(0), @@ -223,6 +224,7 @@ NEW_FROM_EXAMPLE=1 assert!(log.contains("set SHOPWARE_SHOP_ID=acme"), "{log}"); assert!(log.contains("set SHOPWARE_DEPLOY_ENV=staging"), "{log}"); assert!(log.contains("commented 1 COMPOSE_PROJECT_NAME"), "{log}"); + assert!(!log.contains("--vps"), "{log}"); assert_no_secrets(&out, &["from-example"]); let env = fs::read_to_string(shop.path().join(".env")).unwrap(); @@ -231,9 +233,8 @@ NEW_FROM_EXAMPLE=1 assert!(env.contains(&format!("MYSQL_PASSWORD={MYSQL_PASS}"))); assert!(!env.contains("MYSQL_PASSWORD=from-example")); assert!(env.contains("NEW_FROM_EXAMPLE=1")); - assert!( - env.contains("# COMPOSE_PROJECT_NAME=sw-shop-acme # commented by deploy/init-env.sh --vps") - ); + assert!(env.contains("# COMPOSE_PROJECT_NAME=sw-shop-acme # commented by deploy/init-env.sh")); + assert!(!env.contains("--vps")); assert!(!env .lines() .any(|l| l.trim_start().starts_with("COMPOSE_PROJECT_NAME="))); @@ -307,6 +308,44 @@ MYSQL_PASSWORD={MYSQL_PASS} ); } +#[test] +fn vps_flag_is_rejected() { + let shop = TempShop::new("gone-vps"); + fs::write(shop.path().join(".env"), "SHOPWARE_SHOP_ID=acme\n").unwrap(); + let out = init_env(shop.path(), &["--vps"]); + assert_ne!(out.status.code(), Some(0), "stderr={}", stderr(&out)); + let text = combined(&out); + assert!( + text.contains("unexpected argument") || text.contains("unexpected"), + "{text}" + ); +} + +#[test] +fn already_commented_compose_project_name_reports_none() { + let shop = TempShop::new("already-cpn"); + fs::write( + shop.path().join(".env"), + "\ +SHOPWARE_SHOP_ID=acme +SHOPWARE_DEPLOY_ENV=live +# COMPOSE_PROJECT_NAME=sw-shop-acme +", + ) + .unwrap(); + let out = init_env(shop.path(), &[]); + assert_eq!(out.status.code(), Some(0), "stderr={}", stderr(&out)); + let log = stdout(&out); + assert!(log.contains("no uncommented COMPOSE_PROJECT_NAME"), "{log}"); + assert!(!log.contains("commented 1 COMPOSE_PROJECT_NAME"), "{log}"); + assert!(!log.contains("--vps"), "{log}"); + let env = fs::read_to_string(shop.path().join(".env")).unwrap(); + assert!(env.contains("# COMPOSE_PROJECT_NAME=sw-shop-acme")); + assert!(!env + .lines() + .any(|l| l.trim_start().starts_with("COMPOSE_PROJECT_NAME="))); +} + #[test] fn generate_app_secret_flag_is_rejected() { let shop = TempShop::new("gone-flag");