spin_trigger/cli/
summary.rs

1use spin_core::async_trait;
2use spin_factor_key_value::KeyValueFactor;
3use spin_factor_sqlite::SqliteFactor;
4use spin_factors::RuntimeFactors;
5use spin_factors_executor::ExecutorHooks;
6
7/// An [`ExecutorHooks`] that prints information about the default KV store.
8pub struct KeyValueDefaultStoreSummaryHook;
9
10#[async_trait]
11impl<F: RuntimeFactors, U> ExecutorHooks<F, U> for KeyValueDefaultStoreSummaryHook {
12    async fn configure_app(
13        &self,
14        configured_app: &spin_factors::ConfiguredApp<F>,
15    ) -> anyhow::Result<()> {
16        let Ok(kv_app_state) = configured_app.app_state::<KeyValueFactor>() else {
17            return Ok(());
18        };
19        if !kv_app_state.store_is_used("default") {
20            // We don't talk about unused default stores
21            return Ok(());
22        }
23        if let Some(default_store_summary) = kv_app_state.store_summary("default") {
24            println!("Storing default key-value data to {default_store_summary}.");
25        }
26        Ok(())
27    }
28}
29
30/// An [`ExecutorHooks`] that prints information about the default KV store.
31pub struct SqliteDefaultStoreSummaryHook;
32
33#[async_trait]
34impl<F: RuntimeFactors, U> ExecutorHooks<F, U> for SqliteDefaultStoreSummaryHook {
35    async fn configure_app(
36        &self,
37        configured_app: &spin_factors::ConfiguredApp<F>,
38    ) -> anyhow::Result<()> {
39        let Ok(sqlite_app_state) = configured_app.app_state::<SqliteFactor>() else {
40            return Ok(());
41        };
42        if !sqlite_app_state.database_is_used("default") {
43            // We don't talk about unused default databases
44            return Ok(());
45        }
46        if let Some(default_database_summary) = sqlite_app_state
47            .get_connection("default")
48            .await
49            .and_then(Result::ok)
50            .and_then(|conn| conn.summary())
51        {
52            println!("Storing default SQLite data to {default_database_summary}.");
53        }
54        Ok(())
55    }
56}