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
// src/commands/action.rs
//! `bosshogg action` — list / get / create / update / delete / references / tag.
//!
//! Actions are project-scoped. Deletion is soft (PATCH deleted=true via
//! `client.delete`, since "actions" is in SOFT_DELETE_RESOURCES).

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

use crate::client::Client;
use crate::commands::context::CommandContext;
use crate::commands::util::read_json_file;
use crate::error::{BosshoggError, Result};
use crate::output;

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

#[derive(Deserialize, Serialize, Debug, Clone)]
pub struct Action {
    pub id: i64,
    pub name: String,
    #[serde(default)]
    pub description: Option<String>,
    #[serde(default)]
    pub post_to_slack: Option<bool>,
    #[serde(default)]
    pub slack_message_format: Option<String>,
    pub steps: serde_json::Value, // fluid
    #[serde(default)]
    pub deleted: bool,
    #[serde(default)]
    pub is_calculating: Option<bool>,
    #[serde(default)]
    pub created_at: Option<String>,
    #[serde(default)]
    pub created_by: Option<serde_json::Value>,
    #[serde(default)]
    pub updated_at: Option<String>,
    #[serde(default)]
    pub tags: Vec<String>,
    #[serde(default)]
    pub verified: Option<bool>,
}

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

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

#[derive(Subcommand, Debug)]
pub enum ActionCommand {
    /// List actions with optional search filter.
    List {
        #[arg(long)]
        search: Option<String>,
        /// Cap results at N rows (default: fetch all pages).
        #[arg(long)]
        limit: Option<usize>,
    },
    /// Get a single action by numeric id.
    Get { id: i64 },
    /// Create a new action.
    Create {
        #[arg(long)]
        name: String,
        /// Path to a JSON file containing the steps array.
        #[arg(long)]
        steps_file: PathBuf,
    },
    /// Update action fields.
    Update {
        id: i64,
        #[arg(long)]
        name: Option<String>,
        #[arg(long)]
        description: Option<String>,
        /// Path to a JSON file with updated steps.
        #[arg(long)]
        steps_file: Option<PathBuf>,
    },
    /// Soft-delete an action (PATCH deleted=true).
    Delete { id: i64 },
    /// List references to this action.
    References { id: i64 },
    /// Add or remove a tag on an action.
    Tag {
        id: i64,
        #[arg(long, conflicts_with = "remove")]
        add: Option<String>,
        #[arg(long, conflicts_with = "add")]
        remove: Option<String>,
    },
}

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

pub async fn execute(args: ActionArgs, cx: &CommandContext) -> Result<()> {
    match args.command {
        ActionCommand::List { search, limit } => list_actions(cx, search, limit).await,
        ActionCommand::Get { id } => get_action(cx, id).await,
        ActionCommand::Create { name, steps_file } => create_action(cx, name, steps_file).await,
        ActionCommand::Update {
            id,
            name,
            description,
            steps_file,
        } => update_action(cx, id, name, description, steps_file).await,
        ActionCommand::Delete { id } => delete_action(cx, id).await,
        ActionCommand::References { id } => references(cx, id).await,
        ActionCommand::Tag { id, add, remove } => tag_action(cx, id, add, remove).await,
    }
}

// ── Helpers ───────────────────────────────────────────────────────────────────

fn project_id_required(client: &Client) -> Result<&str> {
    client.project_id().ok_or_else(|| {
        BosshoggError::Config("no project_id configured; run `bosshogg configure`".into())
    })
}

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

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

async fn list_actions(
    cx: &CommandContext,
    search: Option<String>,
    limit: Option<usize>,
) -> Result<()> {
    let client = &cx.client;
    let project_id = project_id_required(client)?;

    let qs = if let Some(s) = search {
        format!("?search={}", urlencoding::encode(&s))
    } else {
        String::new()
    };

    let path = format!("/api/projects/{project_id}/actions/{qs}");
    let results: Vec<Action> = client.get_paginated(&path, limit).await?;

    if cx.json_mode {
        output::print_json(&ListOutput {
            count: results.len(),
            results,
        });
    } else {
        let headers = &["ID", "NAME", "TAGS", "CREATED_AT"];
        let rows: Vec<Vec<String>> = results
            .iter()
            .map(|a| {
                vec![
                    a.id.to_string(),
                    a.name.clone(),
                    a.tags.join(", "),
                    a.created_at.clone().unwrap_or_default(),
                ]
            })
            .collect();
        output::table::print(headers, &rows);
    }
    Ok(())
}

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

async fn get_action(cx: &CommandContext, id: i64) -> Result<()> {
    let client = &cx.client;
    let project_id = project_id_required(client)?;
    let action: Action = client
        .get(&format!("/api/projects/{project_id}/actions/{id}/"))
        .await?;
    print_action(&action, cx.json_mode);
    Ok(())
}

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

async fn create_action(cx: &CommandContext, name: String, steps_file: PathBuf) -> Result<()> {
    let client = &cx.client;
    let project_id = project_id_required(client)?;
    let steps = read_json_file(&steps_file).await?;
    let body = json!({ "name": name, "steps": steps });

    let created: Action = client
        .post(&format!("/api/projects/{project_id}/actions/"), &body)
        .await?;

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

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

async fn update_action(
    cx: &CommandContext,
    id: i64,
    name: Option<String>,
    description: Option<String>,
    steps_file: Option<PathBuf>,
) -> Result<()> {
    let client = &cx.client;
    let project_id = project_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(d) = description {
        body.insert("description".into(), Value::String(d));
    }
    if let Some(p) = steps_file.as_deref() {
        body.insert("steps".into(), read_json_file(p).await?);
    }

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

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

    let updated: Action = client
        .patch(
            &format!("/api/projects/{project_id}/actions/{id}/"),
            &Value::Object(body),
        )
        .await?;

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

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

async fn delete_action(cx: &CommandContext, id: i64) -> Result<()> {
    let client = &cx.client;
    let project_id = project_id_required(client)?;

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

    client
        .delete(&format!("/api/projects/{project_id}/actions/{id}/"))
        .await?;

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

// ── references ────────────────────────────────────────────────────────────────

async fn references(cx: &CommandContext, id: i64) -> Result<()> {
    let client = &cx.client;
    let project_id = project_id_required(client)?;
    let v: Value = client
        .get(&format!(
            "/api/projects/{project_id}/actions/{id}/references/"
        ))
        .await?;

    if cx.json_mode {
        output::print_json(&v);
    } else if let Some(arr) = v.as_array() {
        println!("References for action {id}:");
        for item in arr {
            println!("  {item}");
        }
    } else if let Some(results) = v.get("results").and_then(Value::as_array) {
        println!("References for action {id}:");
        for item in results {
            println!("  {item}");
        }
    } else {
        output::print_json(&v);
    }
    Ok(())
}

// ── tag ───────────────────────────────────────────────────────────────────────

async fn tag_action(
    cx: &CommandContext,
    id: i64,
    add: Option<String>,
    remove: Option<String>,
) -> Result<()> {
    let client = &cx.client;
    let project_id = project_id_required(client)?;

    // Fetch current tags first.
    let action: Action = client
        .get(&format!("/api/projects/{project_id}/actions/{id}/"))
        .await?;

    let mut tags = action.tags.clone();

    if let Some(tag) = add {
        if !tags.contains(&tag) {
            tags.push(tag);
        }
    } else if let Some(tag) = remove {
        tags.retain(|t| t != &tag);
    } else {
        return Err(BosshoggError::BadRequest(
            "provide --add TAG or --remove TAG".into(),
        ));
    }

    let body = json!({ "tags": tags });
    let updated: Action = client
        .patch(&format!("/api/projects/{project_id}/actions/{id}/"), &body)
        .await?;

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

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

fn print_action(action: &Action, json_mode: bool) {
    if json_mode {
        output::print_json(action);
    } else {
        println!("ID:          {}", action.id);
        println!("Name:        {}", action.name);
        if let Some(d) = action.description.as_deref() {
            println!("Description: {d}");
        }
        println!("Tags:        {}", action.tags.join(", "));
        println!("Deleted:     {}", action.deleted);
        if let Some(ca) = action.created_at.as_deref() {
            println!("Created:     {ca}");
        }
        if let Some(ua) = action.updated_at.as_deref() {
            println!("Updated:     {ua}");
        }
    }
}

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

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

    fn action_json(id: i64, name: &str) -> &'static str {
        // We use a helper below that takes id/name dynamically — leaking a
        // static string isn't practical, so use the parse helper instead.
        let _ = (id, name);
        r#"{
            "id": 1,
            "name": "My Action",
            "steps": [],
            "deleted": false,
            "tags": []
        }"#
    }

    #[test]
    fn action_roundtrip_minimal() {
        let raw = action_json(1, "My Action");
        let a: Action = serde_json::from_str(raw).unwrap();
        assert_eq!(a.id, 1);
        assert_eq!(a.name, "My Action");
        assert!(!a.deleted);
        assert!(a.tags.is_empty());
    }

    #[test]
    fn action_roundtrip_full() {
        let raw = r#"{
            "id": 42,
            "name": "Sign Up Action",
            "description": "User signs up",
            "post_to_slack": true,
            "slack_message_format": "New signup: {person.email}",
            "steps": [{"event": "$pageview", "url": "/signup"}],
            "deleted": false,
            "is_calculating": false,
            "created_at": "2026-01-01T00:00:00Z",
            "created_by": {"id": 1, "email": "admin@example.com"},
            "updated_at": "2026-04-01T00:00:00Z",
            "tags": ["marketing", "growth"],
            "verified": true
        }"#;
        let a: Action = serde_json::from_str(raw).unwrap();
        assert_eq!(a.id, 42);
        assert_eq!(a.tags, vec!["marketing", "growth"]);
        assert_eq!(a.verified, Some(true));
        assert_eq!(a.post_to_slack, Some(true));
    }

    #[test]
    fn action_missing_optional_fields_ok() {
        let raw = r#"{
            "id": 5,
            "name": "Minimal",
            "steps": null,
            "deleted": true,
            "tags": []
        }"#;
        let a: Action = serde_json::from_str(raw).unwrap();
        assert_eq!(a.id, 5);
        assert!(a.deleted);
        assert!(a.description.is_none());
    }
}