posthog_cli/commands/
mod.rs

1pub mod login;
2pub mod query;
3pub mod sourcemap;
4
5use clap::{Parser, Subcommand};
6use query::OutputMode;
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    /// Authenticate with PostHog, storing a personal API token locally
25    Login,
26
27    /// Run a SQL query against any data you have in posthog. This is mostly for fun, and subject to change
28    Query {
29        /// The query to run
30        query: String,
31        #[arg(short, long, default_value = "print", value_enum)]
32        output: OutputMode,
33    },
34
35    #[command(about = "Upload a directory of bundled chunks to PostHog")]
36    Sourcemap {
37        #[command(subcommand)]
38        cmd: SourcemapCommand,
39    },
40}
41
42#[derive(Subcommand)]
43pub enum SourcemapCommand {
44    /// Inject each bundled chunk with a posthog chunk ID
45    Inject {
46        /// The directory containing the bundled chunks
47        #[arg(short, long)]
48        directory: PathBuf,
49
50        /// Where to write the injected chunks. If not provided, the original files will be overwritten
51        #[arg(short, long)]
52        output: Option<PathBuf>,
53    },
54    /// Upload the bundled chunks to PostHog
55    Upload {
56        /// The directory containing the bundled chunks
57        #[arg(short, long)]
58        directory: PathBuf,
59
60        /// The build ID to associate with the uploaded chunks
61        #[arg(short, long)]
62        build: Option<String>,
63    },
64}
65
66impl Cli {
67    pub fn run() -> Result<(), CapturedError> {
68        let command = Cli::parse();
69
70        match &command.command {
71            Commands::Login => {
72                login::login()?;
73            }
74            Commands::Sourcemap { cmd } => match cmd {
75                SourcemapCommand::Inject { directory, output } => {
76                    sourcemap::inject::inject(directory, output)?;
77                }
78                SourcemapCommand::Upload { directory, build } => {
79                    sourcemap::upload::upload(&command.host, directory, build)?;
80                }
81            },
82            Commands::Query { query, output } => query::run_query(&command.host, query, *output)?,
83        }
84
85        Ok(())
86    }
87}