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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
// src/commands/hog_function.rs
//! `bosshogg hog-function` — list / get / create / update / delete / enable /
//! disable / invoke / logs / metrics / enable-backfills.
//!
//! Hog functions are environment-scoped.
//! `hog_functions` IS in SOFT_DELETE_RESOURCES — `client.delete()` rewrites to
//! PATCH {"deleted": true}.

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_json_file, read_text_file};
use crate::error::{BosshoggError, Result};
use crate::output;

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

#[derive(Deserialize, Serialize, Debug, Clone)]
pub struct HogFunction {
    pub id: String,
    pub name: String,
    #[serde(default)]
    pub description: Option<String>,
    #[serde(rename = "type", default)]
    pub fn_type: Option<String>,
    #[serde(default)]
    pub template_id: Option<String>,
    #[serde(default)]
    pub enabled: Option<bool>,
    #[serde(default)]
    pub deleted: Option<bool>,
    #[serde(default)]
    pub hog: Option<String>,
    #[serde(default)]
    pub inputs: Option<Value>,
    #[serde(default)]
    pub inputs_schema: Option<Value>,
    #[serde(default)]
    pub filters: Option<Value>,
    #[serde(default)]
    pub mappings: Option<Value>,
    #[serde(default)]
    pub masking: Option<Value>,
    #[serde(default)]
    pub created_at: Option<String>,
    #[serde(default)]
    pub updated_at: Option<String>,
    #[serde(default)]
    pub created_by: Option<Value>,
}

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

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

#[derive(Subcommand, Debug)]
pub enum HogFunctionCommand {
    /// List hog functions with optional filters.
    List {
        /// Filter by function type (destination, transformation, etc.).
        #[arg(long = "type")]
        fn_type: Option<String>,
        /// Show only enabled functions.
        #[arg(long, conflicts_with = "disabled")]
        enabled: bool,
        /// Show only disabled functions.
        #[arg(long, conflicts_with = "enabled")]
        disabled: bool,
        /// Search by name.
        #[arg(long)]
        search: Option<String>,
        /// Cap results at N rows (default: fetch all pages).
        #[arg(long)]
        limit: Option<usize>,
    },
    /// Get a single hog function by UUID.
    Get { id: String },
    /// Create a new hog function from a template.
    Create {
        #[arg(long)]
        name: String,
        /// Template ID to base the function on.
        #[arg(long)]
        template_id: String,
        /// Path to a JSON file containing the inputs object.
        #[arg(long)]
        inputs_file: Option<PathBuf>,
    },
    /// Update a hog function's fields.
    Update {
        id: String,
        #[arg(long)]
        name: Option<String>,
        /// Path to a JSON file containing the updated inputs object.
        #[arg(long)]
        inputs_file: Option<PathBuf>,
        /// Path to a .hog file containing the updated Hog source.
        #[arg(long)]
        hog_file: Option<PathBuf>,
    },
    /// Soft-delete a hog function.
    Delete { id: String },
    /// Enable a hog function.
    Enable { id: String },
    /// Disable a hog function.
    Disable { id: String },
    /// Invoke a hog function with a sample event (POST /invocations/).
    Invoke {
        id: String,
        /// Path to a JSON file containing the sample event.
        #[arg(long)]
        event_file: PathBuf,
    },
    /// Fetch execution logs for a hog function.
    Logs {
        id: String,
        /// Filter logs after this timestamp (ISO 8601).
        #[arg(long)]
        after: Option<String>,
        /// Filter logs before this timestamp (ISO 8601).
        #[arg(long)]
        before: Option<String>,
    },
    /// Fetch metrics for a hog function.
    Metrics { id: String },
    /// Enable backfills for a hog function.
    #[command(name = "enable-backfills")]
    EnableBackfills { id: String },
}

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

pub async fn execute(args: HogFunctionArgs, cx: &CommandContext) -> Result<()> {
    match args.command {
        HogFunctionCommand::List {
            fn_type,
            enabled,
            disabled,
            search,
            limit,
        } => list_hog_functions(cx, fn_type, enabled, disabled, search, limit).await,
        HogFunctionCommand::Get { id } => get_hog_function(cx, id).await,
        HogFunctionCommand::Create {
            name,
            template_id,
            inputs_file,
        } => create_hog_function(cx, name, template_id, inputs_file).await,
        HogFunctionCommand::Update {
            id,
            name,
            inputs_file,
            hog_file,
        } => update_hog_function(cx, id, name, inputs_file, hog_file).await,
        HogFunctionCommand::Delete { id } => delete_hog_function(cx, id).await,
        HogFunctionCommand::Enable { id } => enable_hog_function(cx, id).await,
        HogFunctionCommand::Disable { id } => disable_hog_function(cx, id).await,
        HogFunctionCommand::Invoke { id, event_file } => {
            invoke_hog_function(cx, id, event_file).await
        }
        HogFunctionCommand::Logs { id, after, before } => {
            logs_hog_function(cx, id, after, before).await
        }
        HogFunctionCommand::Metrics { id } => metrics_hog_function(cx, id).await,
        HogFunctionCommand::EnableBackfills { id } => enable_backfills(cx, id).await,
    }
}

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

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

async fn list_hog_functions(
    cx: &CommandContext,
    fn_type: Option<String>,
    enabled: bool,
    disabled: bool,
    search: Option<String>,
    limit: Option<usize>,
) -> Result<()> {
    let client = &cx.client;
    let env_id = env_id_required(client)?;

    let mut qs: Vec<(String, String)> = Vec::new();
    if let Some(t) = fn_type {
        qs.push(("type".into(), t));
    }
    if enabled {
        qs.push(("enabled".into(), "true".into()));
    }
    if disabled {
        qs.push(("enabled".into(), "false".into()));
    }
    if let Some(s) = search {
        qs.push(("search".into(), s));
    }

    let query = if qs.is_empty() {
        String::new()
    } else {
        let joined = qs
            .iter()
            .map(|(k, v)| format!("{}={}", k, urlencoding::encode(v)))
            .collect::<Vec<_>>()
            .join("&");
        format!("?{joined}")
    };

    let path = format!("/api/environments/{env_id}/hog_functions/{query}");
    let results: Vec<HogFunction> = client.get_paginated(&path, limit).await?;

    if cx.json_mode {
        output::print_json(&ListOutput {
            count: results.len(),
            results,
        });
    } else {
        let headers = &["ID", "NAME", "TYPE", "ENABLED"];
        let rows: Vec<Vec<String>> = results
            .iter()
            .map(|f| {
                vec![
                    f.id.clone(),
                    f.name.clone(),
                    f.fn_type.clone().unwrap_or_else(|| "-".into()),
                    f.enabled
                        .map(|e| e.to_string())
                        .unwrap_or_else(|| "-".into()),
                ]
            })
            .collect();
        output::table::print(headers, &rows);
    }
    Ok(())
}

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

async fn get_hog_function(cx: &CommandContext, id: String) -> Result<()> {
    let client = &cx.client;
    let env_id = env_id_required(client)?;
    let f: HogFunction = client
        .get(&format!("/api/environments/{env_id}/hog_functions/{id}/"))
        .await?;
    if cx.json_mode {
        output::print_json(&f);
    } else {
        println!("ID:          {}", f.id);
        println!("Name:        {}", f.name);
        if let Some(t) = f.fn_type.as_deref() {
            println!("Type:        {t}");
        }
        if let Some(tid) = f.template_id.as_deref() {
            println!("Template:    {tid}");
        }
        println!(
            "Enabled:     {}",
            f.enabled
                .map(|e| e.to_string())
                .unwrap_or_else(|| "-".into())
        );
        if let Some(ca) = f.created_at.as_deref() {
            println!("Created:     {ca}");
        }
    }
    Ok(())
}

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

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

    let mut body = json!({
        "name": name,
        "template_id": template_id,
    });

    if let Some(p) = inputs_file.as_deref() {
        body["inputs"] = read_json_file(p).await?;
    }

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

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

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

async fn update_hog_function(
    cx: &CommandContext,
    id: String,
    name: Option<String>,
    inputs_file: Option<PathBuf>,
    hog_file: Option<PathBuf>,
) -> Result<()> {
    let client = &cx.client;
    let env_id = env_id_required(client)?;

    let mut body = serde_json::Map::new();
    if let Some(n) = name {
        body.insert("name".into(), Value::String(n));
    }
    if let Some(p) = inputs_file.as_deref() {
        body.insert("inputs".into(), read_json_file(p).await?);
    }
    if let Some(p) = hog_file.as_deref() {
        let hog_src = read_text_file(p).await?;
        body.insert("hog".into(), Value::String(hog_src));
    }

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

    cx.confirm(&format!("update hog function `{id}`; continue?"))?;

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

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

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

async fn delete_hog_function(cx: &CommandContext, id: String) -> Result<()> {
    let client = &cx.client;
    let env_id = env_id_required(client)?;

    cx.confirm(&format!("soft-delete hog function `{id}`; continue?"))?;

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

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

// ── enable ────────────────────────────────────────────────────────────────────

async fn enable_hog_function(cx: &CommandContext, id: String) -> Result<()> {
    let client = &cx.client;
    let env_id = env_id_required(client)?;

    cx.confirm(&format!("enable hog function `{id}`; continue?"))?;

    let updated: HogFunction = client
        .patch(
            &format!("/api/environments/{env_id}/hog_functions/{id}/"),
            &json!({ "enabled": true }),
        )
        .await?;

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

// ── disable ───────────────────────────────────────────────────────────────────

async fn disable_hog_function(cx: &CommandContext, id: String) -> Result<()> {
    let client = &cx.client;
    let env_id = env_id_required(client)?;

    cx.confirm(&format!("disable hog function `{id}`; continue?"))?;

    let updated: HogFunction = client
        .patch(
            &format!("/api/environments/{env_id}/hog_functions/{id}/"),
            &json!({ "enabled": false }),
        )
        .await?;

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

// ── invoke ────────────────────────────────────────────────────────────────────

async fn invoke_hog_function(cx: &CommandContext, id: String, event_file: PathBuf) -> Result<()> {
    let client = &cx.client;
    let env_id = env_id_required(client)?;

    cx.confirm(&format!(
        "invoke hog function `{id}` with sample event; continue?"
    ))?;

    let event = read_json_file(&event_file).await?;
    let body = json!({ "event": event });

    let v: Value = client
        .post(
            &format!("/api/environments/{env_id}/hog_functions/{id}/invocations/"),
            &body,
        )
        .await?;

    if cx.json_mode {
        output::print_json(&v);
    } else {
        println!("Invoked hog function {id}");
        if let Some(status) = v.get("status").and_then(Value::as_str) {
            println!("Status: {status}");
        }
    }
    Ok(())
}

// ── logs ──────────────────────────────────────────────────────────────────────

async fn logs_hog_function(
    cx: &CommandContext,
    id: String,
    after: Option<String>,
    before: Option<String>,
) -> Result<()> {
    let client = &cx.client;
    let env_id = env_id_required(client)?;

    let mut qs: Vec<(String, String)> = Vec::new();
    if let Some(a) = after {
        qs.push(("after".into(), a));
    }
    if let Some(b) = before {
        qs.push(("before".into(), b));
    }

    let query = if qs.is_empty() {
        String::new()
    } else {
        let joined = qs
            .iter()
            .map(|(k, v)| format!("{}={}", k, urlencoding::encode(v)))
            .collect::<Vec<_>>()
            .join("&");
        format!("?{joined}")
    };

    let path = format!("/api/environments/{env_id}/hog_functions/{id}/logs/{query}");
    let v: Value = client.get(&path).await?;

    if cx.json_mode {
        output::print_json(&v);
    } else if let Some(results) = v.get("results").and_then(Value::as_array) {
        for entry in results {
            let ts = entry
                .get("timestamp")
                .and_then(Value::as_str)
                .unwrap_or("-");
            let lvl = entry.get("level").and_then(Value::as_str).unwrap_or("INFO");
            let msg = entry.get("message").and_then(Value::as_str).unwrap_or("");
            println!("{ts}  [{lvl}]  {msg}");
        }
    } else {
        output::print_json(&v);
    }
    Ok(())
}

// ── metrics ───────────────────────────────────────────────────────────────────

async fn metrics_hog_function(cx: &CommandContext, id: String) -> Result<()> {
    let client = &cx.client;
    let env_id = env_id_required(client)?;
    let v: Value = client
        .get(&format!(
            "/api/environments/{env_id}/hog_functions/{id}/metrics/"
        ))
        .await?;
    if cx.json_mode {
        output::print_json(&v);
    } else {
        println!("{v}");
    }
    Ok(())
}

// ── enable-backfills ──────────────────────────────────────────────────────────

async fn enable_backfills(cx: &CommandContext, id: String) -> Result<()> {
    let client = &cx.client;
    let env_id = env_id_required(client)?;

    cx.confirm(&format!(
        "enable backfills for hog function `{id}`; continue?"
    ))?;

    let v: Value = client
        .post(
            &format!("/api/environments/{env_id}/hog_functions/{id}/enable_backfills/"),
            &json!({}),
        )
        .await?;

    if cx.json_mode {
        output::print_json(&v);
    } else {
        println!("Enabled backfills for hog function {id}");
    }
    Ok(())
}

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

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

    #[test]
    fn hog_function_roundtrip_minimal() {
        let raw = r#"{
            "id": "abc-123",
            "name": "My Function",
            "type": "destination",
            "enabled": true,
            "deleted": false
        }"#;
        let f: HogFunction = serde_json::from_str(raw).unwrap();
        assert_eq!(f.id, "abc-123");
        assert_eq!(f.name, "My Function");
        assert_eq!(f.fn_type, Some("destination".to_string()));
        assert_eq!(f.enabled, Some(true));
    }

    #[test]
    fn hog_function_roundtrip_full() {
        let raw = r#"{
            "id": "def-456",
            "name": "Transform Events",
            "description": "Transforms events before sending",
            "type": "transformation",
            "template_id": "template-abc",
            "enabled": false,
            "deleted": false,
            "hog": "return event",
            "inputs": {"key": "value"},
            "inputs_schema": [],
            "filters": null,
            "mappings": null,
            "masking": null,
            "created_at": "2026-01-01T00:00:00Z",
            "updated_at": "2026-04-01T00:00:00Z",
            "created_by": {"id": 1, "email": "test@example.com"}
        }"#;
        let f: HogFunction = serde_json::from_str(raw).unwrap();
        assert_eq!(f.id, "def-456");
        assert_eq!(f.fn_type, Some("transformation".to_string()));
        assert_eq!(f.template_id, Some("template-abc".to_string()));
        assert_eq!(f.enabled, Some(false));
    }

    #[test]
    fn hog_function_type_field_serializes_correctly() {
        let f = HogFunction {
            id: "xyz".into(),
            name: "Test".into(),
            description: None,
            fn_type: Some("destination".into()),
            template_id: None,
            enabled: Some(true),
            deleted: None,
            hog: None,
            inputs: None,
            inputs_schema: None,
            filters: None,
            mappings: None,
            masking: None,
            created_at: None,
            updated_at: None,
            created_by: None,
        };
        let v = serde_json::to_value(&f).unwrap();
        // The field should serialize as "type" (the serde rename)
        assert_eq!(v.get("type").and_then(Value::as_str), Some("destination"));
        // Not as fn_type
        assert!(v.get("fn_type").is_none());
    }
}