BlueGit

ict rusty-teams

Files

rusty-teams / crates / cli / src / main.rs

main.rs

main.rs 2.5 KiB Download Raw
1mod cache_commands;
2
3use std::path::PathBuf;
4use std::process::ExitCode;
5use std::time::Instant;
6
7use clap::{Parser, Subcommand};
8use graph::Graph;
9use session::{DEFAULT_ENDPOINT, Session};
10
11type BoxedResult<T> = Result<T, Box<dyn std::error::Error>>;
12
13#[derive(Parser)]
14#[command(
15 name = "teams-probe",
16 about = "Read-only end-to-end check of the Teams session, Graph and cache layers"
17)]
18struct Arguments {
19 #[arg(long, default_value = DEFAULT_ENDPOINT)]
20 endpoint: String,
21 #[command(subcommand)]
22 command: Command,
23}
24
25#[derive(Subcommand)]
26enum Command {
27 Me,
28 Chats {
29 #[arg(long, default_value_t = 10)]
30 limit: usize,
31 },
32 /// Refresh chats, teams and channels into the cache, then read them back from it.
33 Sync {
34 #[arg(long)]
35 database: Option<PathBuf>,
36 #[arg(long, default_value_t = 200)]
37 chat_limit: usize,
38 },
39 /// Open a chat or channel by title substring and print counts and timings only.
40 Open {
41 title: String,
42 #[arg(long)]
43 database: Option<PathBuf>,
44 },
45}
46
47async fn run(arguments: Arguments) -> BoxedResult<()> {
48 let started = Instant::now();
49 let graph = Graph::new(Session::connect(&arguments.endpoint).await?);
50 match arguments.command {
51 Command::Me => {
52 let me = graph.me().await?;
53 println!("{} ({})", me.display_name.as_deref().unwrap_or("?"), me.id);
54 }
55 Command::Chats { limit } => {
56 let me = graph.me().await?;
57 let chats = teams_core::native::NativeChats::new(graph.session())
58 .first_page(limit)
59 .await?
60 .items;
61 for chat in chats {
62 let last = chat
63 .last_message_time()
64 .map(|time| time.format("%Y-%m-%d %H:%M UTC").to_string())
65 .unwrap_or_else(|| "-".to_owned());
66 println!("{last} {}", chat.title(&me.id));
67 }
68 }
69 Command::Sync {
70 database,
71 chat_limit,
72 } => cache_commands::sync(graph, database, chat_limit).await?,
73 Command::Open { title, database } => cache_commands::open(graph, database, &title).await?,
74 }
75 eprintln!("done in {} ms", started.elapsed().as_millis());
76 Ok(())
77}
78
79#[tokio::main]
80async fn main() -> ExitCode {
81 match run(Arguments::parse()).await {
82 Ok(()) => ExitCode::SUCCESS,
83 Err(error) => {
84 eprintln!("error: {error}");
85 ExitCode::FAILURE
86 }
87 }
88}