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)]
16 host: Option<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 project name associated with the uploaded chunks. Required to have the uploaded chunks associated with
56 /// a specific release, auto-discovered from git information on disk if not provided.
57 #[arg(long)]
58 project: Option<String>,
59
60 /// The version of the project - this can be a version number, semantic version, or a git commit hash. Required
61 /// to have the uploaded chunks associated with a specific release. Auto-discovered from git information on
62 /// disk if not provided.
63 #[arg(long)]
64 version: Option<String>,
65 },
66}
67
68impl Cli {
69 pub fn run() -> Result<(), CapturedError> {
70 let command = Cli::parse();
71
72 match &command.command {
73 Commands::Login => {
74 login::login()?;
75 }
76 Commands::Sourcemap { cmd } => match cmd {
77 SourcemapCommand::Inject { directory } => {
78 sourcemap::inject::inject(directory)?;
79 }
80 SourcemapCommand::Upload {
81 directory,
82 project,
83 version,
84 } => {
85 sourcemap::upload::upload(
86 command.host,
87 directory,
88 project.clone(),
89 version.clone(),
90 )?;
91 }
92 },
93 Commands::Query { cmd } => query::query_command(command.host, cmd)?,
94 }
95
96 Ok(())
97 }
98}