| 1 | //! Dogfood/proof harness: run the cloud facts client once against a real |
| 2 | //! endpoint with an isolated `CODEWHALE_HOME` and print the resulting status. |
| 3 | //! |
| 4 | //! ```sh |
| 5 | //! CODEWHALE_HOME=$(mktemp -d) CODEWHALE_CLOUD_FACTS=1 \ |
| 6 | //! CODEWHALE_CLOUD_FACTS_URL=http://localhost:3000/api/facts/v1/{channel} \ |
| 7 | //! cargo run -p codewhale-cloud-facts --example fetch_live |
| 8 | //! ``` |
| 9 | //! |
| 10 | //! The flag stays off by default; this example only honours the env override. |
| 11 | |
| 12 | use codewhale_cloud_facts::{Settings, maybe_load_persisted_cache, refresh, status}; |
| 13 | use codewhale_config::catalog::now_unix; |
| 14 | use codewhale_config::cloud_facts::overlay; |
| 15 | |
| 16 | #[tokio::main(flavor = "current_thread")] |
| 17 | async fn main() { |
| 18 | let settings = Settings::default().resolve(); |
| 19 | println!( |
| 20 | "enabled={} channel={} url={}", |
| 21 | settings.enabled, |
| 22 | settings.channel, |
| 23 | settings.url() |
| 24 | ); |
| 25 | codewhale_cloud_facts::configure(&settings); |
| 26 | let seeded = maybe_load_persisted_cache(&settings); |
| 27 | println!("seeded_from_disk={seeded:?}"); |
| 28 | println!("before: {}", status().label(now_unix())); |
| 29 | match refresh(&settings, true).await { |
| 30 | Ok(outcome) => println!("refresh: {outcome:?}"), |
| 31 | Err(err) => println!("refresh error: {err}"), |
| 32 | } |
| 33 | let st = status(); |
| 34 | println!("after: {}", st.label(now_unix())); |
| 35 | println!( |
| 36 | "status_json={}", |
| 37 | serde_json::to_string(&st).unwrap_or_default() |
| 38 | ); |
| 39 | if let Some(facts) = overlay::overlay() { |
| 40 | println!( |
| 41 | "overlay: channel={} v{} key={} sha256={} patches={} defaults={} announcements={} dropped={}", |
| 42 | facts.channel, |
| 43 | facts.facts_version, |
| 44 | facts.key_id, |
| 45 | facts.sha256, |
| 46 | facts.models.len(), |
| 47 | facts.provider_defaults.len(), |
| 48 | facts.announcements.len(), |
| 49 | facts.dropped.len() |
| 50 | ); |
| 51 | if let Some(release) = &facts.release { |
| 52 | println!( |
| 53 | "release: latest={} yanked={:?}", |
| 54 | release.latest, release.yanked |
| 55 | ); |
| 56 | } |
| 57 | for a in &facts.announcements { |
| 58 | println!("announcement[{}] {:?}: {}", a.id, a.level, a.text); |
| 59 | } |
| 60 | } else { |
| 61 | println!("overlay: none (bundled facts in use)"); |
| 62 | } |
| 63 | } |
| 64 |