aboutsummaryrefslogtreecommitdiffstats
path: root/src/main.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/main.rs')
-rw-r--r--src/main.rs65
1 files changed, 50 insertions, 15 deletions
diff --git a/src/main.rs b/src/main.rs
index 2df4c78..ca7d0df 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -147,13 +147,13 @@ fn command() -> Command {
Arg::new("json")
.long("json")
.action(ArgAction::SetTrue)
- .help("Print JSON (--plan, --apply, --stage, --status, --materialize, --migrate)"),
+ .help("Print JSON (--plan, --apply, --stage, --status, --materialize, --migrate, --pack --dry-run)"),
)
.arg(
Arg::new("dry-run")
.long("dry-run")
.action(ArgAction::SetTrue)
- .help("With --migrate: say what it would do, changing nothing"),
+ .help("With --migrate, --pack: say what it would do, or what keeps it from it, changing nothing"),
)
.arg(
Arg::new("out")
@@ -229,11 +229,12 @@ fn run() -> Result<ExitCode> {
if out.is_some() && command != "pack" {
bail!("--out is for --pack only");
}
- if matches.get_flag("dry-run") && command != "migrate" {
- bail!("--dry-run is for --migrate only");
+ let dry_run = matches.get_flag("dry-run");
+ if dry_run && !matches!(command, "migrate" | "pack") {
+ bail!("--dry-run is for --migrate and --pack only");
}
- if json && command == "pack" {
- bail!("--json is not for --pack");
+ if json && command == "pack" && !dry_run {
+ bail!("--json is for --pack only with --dry-run");
}
// A setting given as an argument that the command doesn't use is an error; from a
// configuration file, which holds them for every command, it isn't.
@@ -245,9 +246,26 @@ fn run() -> Result<ExitCode> {
}
}
if command == "pack" {
- let out = out.context("--pack needs --out DIR")?;
let schema = settings.schema.as_deref().map(Schema::load).transpose()?;
- for warning in enstate::pack::pack(path.as_deref().unwrap(), &out, schema.as_ref())? {
+ let path = path.as_deref().unwrap();
+ if dry_run {
+ if out.is_some() {
+ bail!("--out is not for a dry run");
+ }
+ let report = enstate::pack::dry_run(path, schema.as_ref())?;
+ if json {
+ println!("{}", serde_json::to_string_pretty(&report)?);
+ } else {
+ print_report(&report)?;
+ }
+ return Ok(if report.problems.is_empty() {
+ ExitCode::SUCCESS
+ } else {
+ ExitCode::from(1)
+ });
+ }
+ let out = out.context("--pack needs --out DIR, or --dry-run")?;
+ for warning in enstate::pack::pack(path, &out, schema.as_ref())? {
eprintln!("enstate: warning: {warning}");
}
return Ok(ExitCode::SUCCESS);
@@ -321,13 +339,8 @@ fn run() -> Result<ExitCode> {
Ok(ExitCode::SUCCESS)
}
"migrate" => {
- let migration = enstate::migrate::migrate(
- &schema,
- &schema_bytes,
- &store,
- matches.get_flag("dry-run"),
- &systemctl,
- )?;
+ let migration =
+ enstate::migrate::migrate(&schema, &schema_bytes, &store, dry_run, &systemctl)?;
if json {
println!("{}", serde_json::to_string_pretty(&migration)?);
} else {
@@ -662,3 +675,25 @@ fn uses(command: &str, setting: &str) -> bool {
_ => true,
}
}
+
+fn print_report(report: &enstate::pack::Report) -> Result<()> {
+ let style = Style::new();
+ let mut out = std::io::stdout().lock();
+ for warning in &report.warnings {
+ writeln!(out, "{} {warning}", style.paint("33", "warning:"))?;
+ }
+ for problem in &report.problems {
+ writeln!(out, "{problem}")?;
+ }
+ let verdict = if report.problems.is_empty() {
+ style.paint("32", "nothing missing")
+ } else {
+ style.paint("31", &format!("{} problems", report.problems.len()))
+ };
+ writeln!(
+ out,
+ "channel {}: {} items, {verdict}",
+ report.channel, report.items
+ )?;
+ Ok(())
+}