main.rs
1 use backstage_client::{entities::Entity, BackstageClient}; 2 use log::{error, info}; 3 use std::env; 4 5 #[tokio::main] 6 async fn main() -> Result<(), Box<dyn std::error::Error>> { 7 env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("INFO")).init(); 8 9 // Get configuration from environment variables 10 let base_url = 11 env::var("BACKSTAGE_URL").unwrap_or_else(|_| "https://demo.backstage.io".to_string()); 12 let token = 13 env::var("BACKSTAGE_TOKEN").expect("BACKSTAGE_TOKEN environment variable must be set"); 14 15 let client = BackstageClient::new(&base_url, &token)?; 16 17 // Example: Fetch all entities 18 info!("Fetching all entities from Backstage..."); 19 match client.fetch_entities::<Entity>(None).await { 20 Ok(entities) => { 21 info!("Successfully fetched {} entities", entities.len()); 22 for (i, entity) in entities.iter().take(5).enumerate() { 23 info!("Entity {}: {} ({})", i + 1, entity.name(), entity.kind()); 24 } 25 if entities.len() > 5 { 26 info!("... and {} more entities", entities.len() - 5); 27 } 28 } 29 Err(e) => { 30 error!("Error fetching entities: {}", e); 31 return Err(e.into()); 32 } 33 } 34 35 // Example: Fetch only components 36 info!("Fetching component entities..."); 37 let mut filters = std::collections::HashMap::new(); 38 filters.insert("kind".to_string(), "Component".to_string()); 39 40 match client.fetch_entities::<Entity>(Some(filters)).await { 41 Ok(components) => { 42 info!("Successfully fetched {} components", components.len()); 43 for component in components.iter().take(3) { 44 info!( 45 "Component: {} - {}", 46 component.name(), 47 component.description().unwrap_or("No description") 48 ); 49 } 50 } 51 Err(e) => { 52 error!("Error fetching components: {}", e); 53 return Err(e.into()); 54 } 55 } 56 57 Ok(()) 58 }