toasty_cli/migration/
snapshot.rs

1use super::SnapshotFile;
2use crate::Config;
3use anyhow::Result;
4use clap::Parser;
5use console::style;
6use toasty::Db;
7
8/// Prints the current schema as a TOML snapshot to stdout.
9///
10/// Reads the schema registered on the [`Db`] and formats it as a
11/// [`SnapshotFile`]. Table headers, key-value pairs, and whitespace are
12/// syntax-highlighted for terminal display.
13#[derive(Parser, Debug)]
14pub struct SnapshotCommand {
15    // Future options can be added here
16}
17
18impl SnapshotCommand {
19    pub(crate) fn run(self, db: &Db, _config: &Config) -> Result<()> {
20        println!();
21        println!(
22            "  {}",
23            style("Current Schema Snapshot").cyan().bold().underlined()
24        );
25        println!();
26
27        let snapshot_file = SnapshotFile::new(toasty::schema::db::Schema::clone(&db.schema().db));
28
29        // Print the snapshot with nice formatting
30        let snapshot_str = snapshot_file.to_string();
31        for line in snapshot_str.lines() {
32            if line.starts_with('[') {
33                println!("  {}", style(line).yellow().bold());
34            } else if line.contains('=') {
35                let parts: Vec<&str> = line.splitn(2, '=').collect();
36                if parts.len() == 2 {
37                    println!(
38                        "  {}{} {}",
39                        style(parts[0]).cyan(),
40                        style("=").dim(),
41                        style(parts[1]).green()
42                    );
43                } else {
44                    println!("  {}", style(line).dim());
45                }
46            } else if line.trim().is_empty() {
47                println!();
48            } else {
49                println!("  {}", style(line).dim());
50            }
51        }
52
53        println!();
54        Ok(())
55    }
56}