posthog_cli/commands/
mod.rs

1pub mod login;
2pub mod query;
3pub mod sourcemap;
4
5use clap::{Parser, Subcommand};
6use query::QueryCommand;
7use std::path::PathBuf;
8
9use crate::error::CapturedError;
10
11#[derive(Parser)]
12#[command(version, about, long_about = None)]
13pub struct Cli {
14    /// The PostHog host to connect to
15    #[arg(long, default_value = "https://us.posthog.com")]
16    host: String,
17
18    #[command(subcommand)]
19    command: Commands,
20}
21
22#[derive(Subcommand)]
23pub enum Commands {
24    /// Interactively authenticate with PostHog, storing a personal API token locally. You can also use the
25    /// environment variables `POSTHOG_CLI_TOKEN` and `POSTHOG_CLI_ENV_ID`
26    Login,
27
28    /// Run a SQL query against any data you have in posthog. This is mostly for fun, and subject to change
29    Query {
30        #[command(subcommand)]
31        cmd: QueryCommand,
32    },
33
34    #[command(about = "Upload a directory of bundled chunks to PostHog")]
35    Sourcemap {
36        #[command(subcommand)]
37        cmd: SourcemapCommand,
38    },
39}
40
41#[derive(Subcommand)]
42pub enum SourcemapCommand {
43    /// Inject each bundled chunk with a posthog chunk ID
44    Inject {
45        /// The directory containing the bundled chunks
46        #[arg(short, long)]
47        directory: PathBuf,
48    },
49    /// Upload the bundled chunks to PostHog
50    Upload {
51        /// The directory containing the bundled chunks
52        #[arg(short, long)]
53        directory: PathBuf,
54
55        /// The build ID to associate with the uploaded chunks
56        #[arg(short, long)]
57        build: Option<String>,
58    },
59}
60
61impl Cli {
62    pub fn run() -> Result<(), CapturedError> {
63        let command = Cli::parse();
64
65        match &command.command {
66            Commands::Login => {
67                login::login()?;
68            }
69            Commands::Sourcemap { cmd } => match cmd {
70                SourcemapCommand::Inject { directory } => {
71                    sourcemap::inject::inject(directory)?;
72                }
73                SourcemapCommand::Upload { directory, build } => {
74                    sourcemap::upload::upload(&command.host, directory, build)?;
75                }
76            },
77            Commands::Query { cmd } => query::query_command(&command.host, cmd)?,
78        }
79
80        Ok(())
81    }
82}