bosshogg 2026.5.3

BossHogg — the agent-first PostHog CLI. Feature flags, HogQL queries, insights, dashboards, cohorts, persons, events, experiments, and more — from the terminal or from a Claude Code / Cursor / other coding-agent loop. Ships with a Claude Code skill (~200 idle tokens) that teaches models how to use it.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
// src/commands/endpoint.rs
//! `bosshogg endpoint` — list / get / create / update / delete / run /
//!   materialize-preview / materialize-status / openapi.
//!
//! Endpoints (HogQL saved queries) are environment-scoped. Addressed by name string.

use clap::{Args, Subcommand};
use serde::{Deserialize, Serialize};
use serde_json::{Value, json};
use std::path::PathBuf;

use crate::commands::context::CommandContext;
use crate::commands::util::{env_id_required, read_text_file};
use crate::error::{BosshoggError, Result};
use crate::output;

// ── Typed struct ────────────────────────────────────────────────────────────

#[derive(Deserialize, Serialize, Debug, Clone)]
pub struct Endpoint {
    pub name: String,
    #[serde(default)]
    pub description: Option<String>,
    pub query: serde_json::Value,
    #[serde(default)]
    pub created_at: Option<String>,
    #[serde(default)]
    pub updated_at: Option<String>,
    #[serde(default)]
    pub created_by: Option<serde_json::Value>,
    #[serde(default)]
    pub is_materialized: Option<bool>,
    #[serde(default)]
    pub last_materialized_at: Option<String>,
}

// ── Clap tree ────────────────────────────────────────────────────────────────

#[derive(Args, Debug)]
pub struct EndpointArgs {
    #[command(subcommand)]
    pub command: EndpointCommand,
}

#[derive(Subcommand, Debug)]
pub enum EndpointCommand {
    /// List all saved endpoints.
    List,
    /// Get a saved endpoint by name.
    Get { name: String },
    /// Create a new saved endpoint.
    Create {
        /// Endpoint name (used as the URL slug).
        #[arg(long)]
        name: String,
        /// Path to a SQL file containing the HogQL query.
        #[arg(long)]
        query_file: PathBuf,
        #[arg(long)]
        description: Option<String>,
    },
    /// Update an existing saved endpoint.
    Update {
        name: String,
        /// Path to a SQL file with the updated HogQL query.
        #[arg(long)]
        query_file: Option<PathBuf>,
        #[arg(long)]
        description: Option<String>,
    },
    /// Delete a saved endpoint.
    Delete { name: String },
    /// Execute the saved query and return results.
    Run { name: String },
    /// Preview what materialization would do for this endpoint.
    #[command(name = "materialize-preview")]
    MaterializePreview { name: String },
    /// Get the current materialization status of an endpoint.
    #[command(name = "materialize-status")]
    MaterializeStatus { name: String },
    /// Get the OpenAPI spec for an endpoint.
    Openapi { name: String },
}

// ── Dispatch ─────────────────────────────────────────────────────────────────

pub async fn execute(args: EndpointArgs, cx: &CommandContext) -> Result<()> {
    match args.command {
        EndpointCommand::List => list_endpoints(cx).await,
        EndpointCommand::Get { name } => get_endpoint(cx, &name).await,
        EndpointCommand::Create {
            name,
            query_file,
            description,
        } => create_endpoint(cx, name, query_file, description).await,
        EndpointCommand::Update {
            name,
            query_file,
            description,
        } => update_endpoint(cx, name, query_file, description).await,
        EndpointCommand::Delete { name } => delete_endpoint(cx, &name).await,
        EndpointCommand::Run { name } => run_endpoint(cx, &name).await,
        EndpointCommand::MaterializePreview { name } => materialize_preview(cx, &name).await,
        EndpointCommand::MaterializeStatus { name } => materialize_status(cx, &name).await,
        EndpointCommand::Openapi { name } => openapi_endpoint(cx, &name).await,
    }
}

// ── list ──────────────────────────────────────────────────────────────────────

#[derive(Serialize)]
struct ListOutput {
    count: usize,
    results: Vec<Endpoint>,
}

async fn list_endpoints(cx: &CommandContext) -> Result<()> {
    let client = &cx.client;
    let env_id = env_id_required(client)?;
    let path = format!("/api/environments/{env_id}/endpoints/");
    let results: Vec<Endpoint> = client.get_paginated(&path, None).await?;

    if cx.json_mode {
        output::print_json(&ListOutput {
            count: results.len(),
            results,
        });
    } else {
        let headers = &["NAME", "MATERIALIZED", "CREATED_AT", "DESCRIPTION"];
        let rows: Vec<Vec<String>> = results
            .iter()
            .map(|e| {
                vec![
                    e.name.clone(),
                    e.is_materialized
                        .map(|v| if v { "yes" } else { "no" })
                        .unwrap_or("-")
                        .to_string(),
                    e.created_at.clone().unwrap_or_default(),
                    e.description.clone().unwrap_or_default(),
                ]
            })
            .collect();
        output::table::print(headers, &rows);
    }
    Ok(())
}

// ── get ───────────────────────────────────────────────────────────────────────

async fn get_endpoint(cx: &CommandContext, name: &str) -> Result<()> {
    let client = &cx.client;
    let env_id = env_id_required(client)?;
    let ep: Endpoint = client
        .get(&format!("/api/environments/{env_id}/endpoints/{name}/"))
        .await?;
    print_endpoint(&ep, cx.json_mode);
    Ok(())
}

// ── create ────────────────────────────────────────────────────────────────────

async fn create_endpoint(
    cx: &CommandContext,
    name: String,
    query_file: PathBuf,
    description: Option<String>,
) -> Result<()> {
    let client = &cx.client;
    let env_id = env_id_required(client)?;
    let sql = read_text_file(&query_file).await?;

    let mut body = json!({
        "name": name,
        "query": { "kind": "HogQLQuery", "query": sql }
    });

    if let Some(d) = description {
        body["description"] = json!(d);
    }

    let created: Endpoint = client
        .post(&format!("/api/environments/{env_id}/endpoints/"), &body)
        .await?;

    if cx.json_mode {
        #[derive(Serialize)]
        struct Out {
            ok: bool,
            action: &'static str,
            name: String,
        }
        output::print_json(&Out {
            ok: true,
            action: "create",
            name: created.name,
        });
    } else {
        println!("Created endpoint '{}'", created.name);
    }
    Ok(())
}

// ── update ────────────────────────────────────────────────────────────────────

async fn update_endpoint(
    cx: &CommandContext,
    name: String,
    query_file: Option<PathBuf>,
    description: Option<String>,
) -> Result<()> {
    let client = &cx.client;
    let env_id = env_id_required(client)?;

    let mut body = serde_json::Map::new();
    if let Some(p) = query_file {
        let sql = read_text_file(&p).await?;
        body.insert(
            "query".into(),
            json!({ "kind": "HogQLQuery", "query": sql }),
        );
    }
    if let Some(d) = description {
        body.insert("description".into(), Value::String(d));
    }

    if body.is_empty() {
        return Err(BosshoggError::BadRequest(
            "no update flags provided (try --query-file, --description)".into(),
        ));
    }

    cx.confirm(&format!("update endpoint `{name}`; continue?"))?;

    let updated: Endpoint = client
        .patch(
            &format!("/api/environments/{env_id}/endpoints/{name}/"),
            &Value::Object(body),
        )
        .await?;

    if cx.json_mode {
        output::print_json(&updated);
    } else {
        println!("Updated endpoint '{}'", updated.name);
    }
    Ok(())
}

// ── delete ────────────────────────────────────────────────────────────────────

async fn delete_endpoint(cx: &CommandContext, name: &str) -> Result<()> {
    let client = &cx.client;
    let env_id = env_id_required(client)?;

    cx.confirm(&format!("delete endpoint `{name}`; continue?"))?;

    client
        .delete(&format!("/api/environments/{env_id}/endpoints/{name}/"))
        .await?;

    if cx.json_mode {
        #[derive(Serialize)]
        struct Out {
            ok: bool,
            action: &'static str,
            name: String,
        }
        output::print_json(&Out {
            ok: true,
            action: "delete",
            name: name.to_string(),
        });
    } else {
        println!("Deleted endpoint '{name}'");
    }
    Ok(())
}

// ── run ───────────────────────────────────────────────────────────────────────

async fn run_endpoint(cx: &CommandContext, name: &str) -> Result<()> {
    let client = &cx.client;
    let env_id = env_id_required(client)?;
    let v: Value = client
        .get(&format!("/api/environments/{env_id}/endpoints/{name}/run/"))
        .await?;
    if cx.json_mode {
        output::print_json(&v);
    } else {
        println!("{}", serde_json::to_string_pretty(&v).unwrap_or_default());
    }
    Ok(())
}

// ── materialize-preview ───────────────────────────────────────────────────────

async fn materialize_preview(cx: &CommandContext, name: &str) -> Result<()> {
    let client = &cx.client;
    let env_id = env_id_required(client)?;
    let body = json!({});
    let v: Value = client
        .post(
            &format!("/api/environments/{env_id}/endpoints/{name}/materialization_preview/"),
            &body,
        )
        .await?;
    if cx.json_mode {
        output::print_json(&v);
    } else {
        println!("{}", serde_json::to_string_pretty(&v).unwrap_or_default());
    }
    Ok(())
}

// ── materialize-status ────────────────────────────────────────────────────────

async fn materialize_status(cx: &CommandContext, name: &str) -> Result<()> {
    let client = &cx.client;
    let env_id = env_id_required(client)?;
    let v: Value = client
        .get(&format!(
            "/api/environments/{env_id}/endpoints/{name}/materialization_status/"
        ))
        .await?;
    if cx.json_mode {
        output::print_json(&v);
    } else {
        println!("{}", serde_json::to_string_pretty(&v).unwrap_or_default());
    }
    Ok(())
}

// ── openapi ───────────────────────────────────────────────────────────────────

async fn openapi_endpoint(cx: &CommandContext, name: &str) -> Result<()> {
    let client = &cx.client;
    let env_id = env_id_required(client)?;
    let v: Value = client
        .get(&format!(
            "/api/environments/{env_id}/endpoints/{name}/openapi.json/"
        ))
        .await?;
    if cx.json_mode {
        output::print_json(&v);
    } else {
        println!("{}", serde_json::to_string_pretty(&v).unwrap_or_default());
    }
    Ok(())
}

// ── print helper ──────────────────────────────────────────────────────────────

fn print_endpoint(ep: &Endpoint, json_mode: bool) {
    if json_mode {
        output::print_json(ep);
    } else {
        println!("Name:          {}", ep.name);
        if let Some(d) = ep.description.as_deref() {
            println!("Description:   {d}");
        }
        println!(
            "Materialized:  {}",
            ep.is_materialized
                .map(|v| if v { "yes" } else { "no" })
                .unwrap_or("-")
        );
        if let Some(ca) = ep.created_at.as_deref() {
            println!("Created:       {ca}");
        }
        if let Some(ua) = ep.updated_at.as_deref() {
            println!("Updated:       {ua}");
        }
        println!(
            "Query:         {}",
            serde_json::to_string(&ep.query).unwrap_or_default()
        );
    }
}

// ── unit tests ────────────────────────────────────────────────────────────────

#[cfg(test)]
mod tests {
    use super::*;

    fn endpoint_json() -> &'static str {
        r#"{
            "name": "daily-signups",
            "description": "Daily signups query",
            "query": {"kind": "HogQLQuery", "query": "SELECT count() FROM events"},
            "created_at": "2026-01-01T00:00:00Z",
            "updated_at": "2026-04-01T00:00:00Z",
            "is_materialized": false
        }"#
    }

    #[test]
    fn endpoint_roundtrip_minimal() {
        let raw = r#"{
            "name": "my-endpoint",
            "query": {"kind": "HogQLQuery", "query": "SELECT 1"}
        }"#;
        let e: Endpoint = serde_json::from_str(raw).unwrap();
        assert_eq!(e.name, "my-endpoint");
        assert!(e.description.is_none());
        assert!(e.is_materialized.is_none());
    }

    #[test]
    fn endpoint_roundtrip_full() {
        let e: Endpoint = serde_json::from_str(endpoint_json()).unwrap();
        assert_eq!(e.name, "daily-signups");
        assert_eq!(e.is_materialized, Some(false));
        assert_eq!(e.description, Some("Daily signups query".into()));
    }

    #[test]
    fn endpoint_query_is_fluid_value() {
        let raw = r#"{
            "name": "flexible",
            "query": {"kind": "HogQLQuery", "query": "SELECT event FROM events", "extra_field": 42}
        }"#;
        let e: Endpoint = serde_json::from_str(raw).unwrap();
        assert_eq!(e.query["extra_field"], serde_json::json!(42));
    }

    #[test]
    fn endpoint_serialize_roundtrip() {
        let e: Endpoint = serde_json::from_str(endpoint_json()).unwrap();
        let s = serde_json::to_string(&e).unwrap();
        let e2: Endpoint = serde_json::from_str(&s).unwrap();
        assert_eq!(e.name, e2.name);
        assert_eq!(e.is_materialized, e2.is_materialized);
    }

    #[test]
    fn endpoint_optional_timestamps_default() {
        let raw = r#"{"name": "bare", "query": {}}"#;
        let e: Endpoint = serde_json::from_str(raw).unwrap();
        assert!(e.created_at.is_none());
        assert!(e.updated_at.is_none());
        assert!(e.last_materialized_at.is_none());
    }
}