| 1 | use { |
| 2 | crate::{cli::BillingCommand, context::Ctx, ui}, |
| 3 | anyhow::Result, |
| 4 | }; |
| 5 | |
| 6 | pub async fn run(ctx: &Ctx, command: BillingCommand) -> Result<()> { |
| 7 | ctx.require_token()?; |
| 8 | |
| 9 | match command { |
| 10 | BillingCommand::Show => show(ctx).await, |
| 11 | BillingCommand::Checkout { plan } => checkout(ctx, plan).await, |
| 12 | BillingCommand::Portal => portal(ctx).await, |
| 13 | } |
| 14 | } |
| 15 | |
| 16 | async fn show(ctx: &Ctx) -> Result<()> { |
| 17 | let billing = ctx.client.billing().await?; |
| 18 | ui::emit(ctx.json, &billing, ui::render::billing) |
| 19 | } |
| 20 | |
| 21 | async fn checkout(ctx: &Ctx, plan: String) -> Result<()> { |
| 22 | let url = ctx.client.billing_checkout(&plan).await?; |
| 23 | open_and_report(ctx, &url) |
| 24 | } |
| 25 | |
| 26 | async fn portal(ctx: &Ctx) -> Result<()> { |
| 27 | let url = ctx.client.billing_portal().await?; |
| 28 | open_and_report(ctx, &url) |
| 29 | } |
| 30 | |
| 31 | fn open_and_report(ctx: &Ctx, url: &str) -> Result<()> { |
| 32 | if ctx.json { |
| 33 | println!("{}", serde_json::json!({ "url": url })); |
| 34 | return Ok(()); |
| 35 | } |
| 36 | open::that(url)?; |
| 37 | ui::note(format!("Opened {url}")); |
| 38 | Ok(()) |
| 39 | } |