| 1 | use { |
| 2 | crate::{ |
| 3 | cli::SessionCommand, |
| 4 | context::Ctx, |
| 5 | ui::{self, prompt}, |
| 6 | }, |
| 7 | anyhow::Result, |
| 8 | }; |
| 9 | |
| 10 | pub async fn run(ctx: &Ctx, command: SessionCommand) -> Result<()> { |
| 11 | ctx.require_token()?; |
| 12 | |
| 13 | match command { |
| 14 | SessionCommand::List => list(ctx).await, |
| 15 | SessionCommand::View { id } => view(ctx, id).await, |
| 16 | SessionCommand::New { title, model } => new(ctx, title, model).await, |
| 17 | SessionCommand::Update { id, title, model } => update(ctx, id, title, model).await, |
| 18 | SessionCommand::Delete { id, yes } => delete(ctx, id, yes).await, |
| 19 | } |
| 20 | } |
| 21 | |
| 22 | async fn list(ctx: &Ctx) -> Result<()> { |
| 23 | let sessions = ctx.client.sessions().await?; |
| 24 | ui::emit(ctx.json, &sessions, |sessions| { |
| 25 | ui::render::sessions(sessions) |
| 26 | }) |
| 27 | } |
| 28 | |
| 29 | async fn view(ctx: &Ctx, id: i64) -> Result<()> { |
| 30 | let session = ctx.client.session(id).await?; |
| 31 | ui::emit(ctx.json, &session, ui::render::session_detail) |
| 32 | } |
| 33 | |
| 34 | async fn new(ctx: &Ctx, title: Option<String>, model: Option<String>) -> Result<()> { |
| 35 | let session = ctx |
| 36 | .client |
| 37 | .create_session(title.as_deref(), model.as_deref()) |
| 38 | .await?; |
| 39 | ui::emit(ctx.json, &session, |session| { |
| 40 | ui::success(format!("Started session #{}.", session.id)); |
| 41 | }) |
| 42 | } |
| 43 | |
| 44 | async fn update(ctx: &Ctx, id: i64, title: Option<String>, model: Option<String>) -> Result<()> { |
| 45 | let session = ctx |
| 46 | .client |
| 47 | .update_session(id, title.as_deref(), model.as_deref()) |
| 48 | .await?; |
| 49 | ui::emit(ctx.json, &session, |session| { |
| 50 | ui::success(format!("Updated session #{}.", session.id)); |
| 51 | }) |
| 52 | } |
| 53 | |
| 54 | async fn delete(ctx: &Ctx, id: i64, yes: bool) -> Result<()> { |
| 55 | if !yes && !prompt::confirm(&format!("Delete session #{id}?"), false)? { |
| 56 | ui::note("Cancelled."); |
| 57 | return Ok(()); |
| 58 | } |
| 59 | |
| 60 | ctx.client.delete_session(id).await?; |
| 61 | ui::success(format!("Deleted session #{id}.")); |
| 62 | Ok(()) |
| 63 | } |