Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 5 additions & 4 deletions docs/command-matrix.md
Original file line number Diff line number Diff line change
Expand Up @@ -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** |
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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`.

Expand Down
38 changes: 27 additions & 11 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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, \
Expand Down Expand Up @@ -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`)
Expand All @@ -203,10 +206,6 @@ pub struct InitEnvArgs {
#[arg(long = "image", value_name = "REPO")]
pub image: Option<String>,

/// 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,
Expand Down Expand Up @@ -510,7 +509,6 @@ mod tests {
"staging",
"--image",
"ghcr.io/example/acme",
"--vps",
"--dry-run",
])
.unwrap();
Expand All @@ -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([
Expand Down
21 changes: 11 additions & 10 deletions src/shopware/envfile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`).
Expand Down Expand Up @@ -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();
Expand All @@ -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 {
Expand Down Expand Up @@ -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"));
Expand Down
119 changes: 82 additions & 37 deletions src/shopware/init_env.rs
Original file line number Diff line number Diff line change
@@ -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::{
Expand All @@ -29,8 +31,7 @@ pub(crate) struct Plan {
pub deploy_env: String,
pub deploy_env_changed: bool,
pub image_set: Option<String>,
pub vps: bool,
pub vps_commented: usize,
pub compose_project_name_commented: usize,
}

impl std::fmt::Debug for Plan {
Expand All @@ -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()
}
}
Expand Down Expand Up @@ -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;
Expand All @@ -183,8 +181,7 @@ pub(crate) fn plan(
deploy_env,
deploy_env_changed,
image_set,
vps: args.vps,
vps_commented,
compose_project_name_commented,
})
}

Expand Down Expand Up @@ -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)");
Expand Down Expand Up @@ -369,7 +364,6 @@ mod tests {
shop_id: Some("acme".into()),
env: None,
image: None,
vps: false,
dry_run: true,
}
}
Expand All @@ -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);
Expand All @@ -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));
Expand All @@ -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!(
"\
Expand All @@ -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()));
Expand All @@ -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]
Expand Down Expand Up @@ -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]
Expand Down
Loading
Loading