shepherd-cli 6.4.5

The canonical shepherd command-line interface over the per-project registry, run artifacts, and sprint pipeline.
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
//! Native project-memory commands over the canonical typed registry.

use std::time::{SystemTime, UNIX_EPOCH};

use rusqlite::params;
use shepherd::registry::{OpenMode, Registry};
use uuid::Uuid;

use crate::{
    ContextInputs, ExecutionContext,
    interface::{CliError, CliGlobals},
};

const USAGE: &str = "usage: shepherd mem <add|list|search|show|pin|unpin|rm>";
const LIST_COLUMNS: [&str; 5] = ["id", "kind", "title", "pinned", "created_at"];
const SEARCH_COLUMNS: [&str; 4] = ["id", "kind", "title", "pinned"];
const SHOW_COLUMNS: [&str; 8] = [
    "id",
    "kind",
    "title",
    "body",
    "tags",
    "pinned",
    "created_at",
    "updated_at",
];

#[derive(
    Clone,
    Debug,
    Eq,
    Hash,
    Ord,
    PartialEq,
    PartialOrd,
    clap::Args,
    serde::Deserialize,
    serde::Serialize,
)]
#[command(disable_help_flag = true, disable_help_subcommand = true)]
pub struct WaveB1MemCmd {
    #[command(subcommand)]
    action: Option<MemAction>,
}

#[derive(
    Clone,
    Debug,
    Eq,
    Hash,
    Ord,
    PartialEq,
    PartialOrd,
    clap::Subcommand,
    serde::Deserialize,
    serde::Serialize,
)]
enum MemAction {
    Add(MemAddCmd),
    List(MemListCmd),
    Search(MemSearchCmd),
    Show(MemShowCmd),
    Pin(MemIdCmd),
    Unpin(MemIdCmd),
    Rm(MemIdCmd),
    Delete(MemIdCmd),
}

#[derive(
    Clone,
    Debug,
    Eq,
    Hash,
    Ord,
    PartialEq,
    PartialOrd,
    clap::Args,
    serde::Deserialize,
    serde::Serialize,
)]
struct MemAddCmd {
    #[arg(long, default_value = "")]
    title: String,
    #[arg(long, default_value = "note")]
    kind: String,
    #[arg(long, default_value = "")]
    body: String,
    #[arg(long, default_value = "[]")]
    tags: String,
}

#[derive(
    Clone,
    Debug,
    Eq,
    Hash,
    Ord,
    PartialEq,
    PartialOrd,
    clap::Args,
    serde::Deserialize,
    serde::Serialize,
)]
struct MemListCmd {
    #[arg(long)]
    json: bool,
}

#[derive(
    Clone,
    Debug,
    Eq,
    Hash,
    Ord,
    PartialEq,
    PartialOrd,
    clap::Args,
    serde::Deserialize,
    serde::Serialize,
)]
struct MemSearchCmd {
    #[arg(long, default_value = "")]
    q: String,
    #[arg(long)]
    json: bool,
}

#[derive(
    Clone,
    Debug,
    Eq,
    Hash,
    Ord,
    PartialEq,
    PartialOrd,
    clap::Args,
    serde::Deserialize,
    serde::Serialize,
)]
struct MemShowCmd {
    id: Option<String>,
    #[arg(long)]
    json: bool,
}

#[derive(
    Clone,
    Debug,
    Eq,
    Hash,
    Ord,
    PartialEq,
    PartialOrd,
    clap::Args,
    serde::Deserialize,
    serde::Serialize,
)]
struct MemIdCmd {
    id: Option<String>,
}

#[derive(Debug)]
struct MemRow {
    id: String,
    kind: String,
    title: String,
    body: String,
    tags: String,
    pinned: i64,
    created_at: i64,
    updated_at: i64,
}

impl WaveB1MemCmd {
    pub(crate) fn run(self, globals: CliGlobals) -> Result<(), CliError> {
        let Some(action) = self.action else {
            return Err(CliError::message(USAGE));
        };
        match action {
            MemAction::Add(command) => command.run(globals),
            MemAction::List(command) => command.run(globals),
            MemAction::Search(command) => command.run(globals),
            MemAction::Show(command) => command.run(globals),
            MemAction::Pin(command) => command.set_pinned(globals, 1, "pin"),
            MemAction::Unpin(command) => command.set_pinned(globals, 0, "unpin"),
            MemAction::Rm(command) | MemAction::Delete(command) => command.remove(globals),
        }
    }
}

impl MemAddCmd {
    fn run(self, globals: CliGlobals) -> Result<(), CliError> {
        let mut context = context(globals)?;
        let registry = Registry::open_migrated(&context.registry_path).map_err(registry_error)?;
        let project_id = project_id(&registry)?;
        if self.title.is_empty() {
            return Err(CliError::message("--title required"));
        }
        let id = Uuid::now_v7().to_string();
        let now = now_seconds();
        registry
            .execute(
                "INSERT INTO mem_entries (id, project_id, kind, title, body, tags, pinned, created_at, updated_at) VALUES (?1, ?2, ?3, ?4, ?5, ?6, 0, ?7, ?7)",
                params![id, project_id, self.kind, self.title, self.body, self.tags, now],
            )
            .map_err(registry_error)?;
        write_stdout(&mut context, &id)
    }
}

impl MemListCmd {
    fn run(self, globals: CliGlobals) -> Result<(), CliError> {
        let mut context = context(globals)?;
        let registry = open_read_registry(&context)?;
        let project_id = project_id(&registry)?;
        let rows = query_mem_rows(
            &registry,
            "SELECT id, kind, title, body, tags, pinned, created_at, updated_at FROM mem_entries WHERE project_id = ?1 ORDER BY pinned DESC, created_at DESC",
            params![project_id],
        )?;
        if self.json {
            return write_stdout(&mut context, &render_list_json(&rows));
        }
        let values = rows
            .iter()
            .map(|row| {
                vec![
                    row.id.clone(),
                    row.kind.clone(),
                    row.title.clone(),
                    row.pinned.to_string(),
                    row.created_at.to_string(),
                ]
            })
            .collect::<Vec<_>>();
        write_if_nonempty(&mut context, &render_table(&LIST_COLUMNS, &values))
    }
}

impl MemSearchCmd {
    fn run(self, globals: CliGlobals) -> Result<(), CliError> {
        let mut context = context(globals)?;
        let registry = open_read_registry(&context)?;
        let project_id = project_id(&registry)?;
        if self.q.is_empty() {
            return Err(CliError::message("--q=<text> required for mem search"));
        }
        let pattern = format!("%{}%", self.q);
        let rows = query_mem_rows(
            &registry,
            "SELECT id, kind, title, body, tags, pinned, created_at, updated_at FROM mem_entries WHERE project_id = ?1 AND (title LIKE ?2 OR body LIKE ?2) ORDER BY pinned DESC, created_at DESC",
            params![project_id, pattern],
        )?;
        if self.json {
            return write_stdout(&mut context, &render_search_json(&rows));
        }
        let values = rows
            .iter()
            .map(|row| {
                vec![
                    row.id.clone(),
                    row.kind.clone(),
                    row.title.clone(),
                    row.pinned.to_string(),
                ]
            })
            .collect::<Vec<_>>();
        write_if_nonempty(&mut context, &render_table(&SEARCH_COLUMNS, &values))
    }
}

impl MemShowCmd {
    fn run(self, globals: CliGlobals) -> Result<(), CliError> {
        let mut context = context(globals)?;
        let registry = open_read_registry(&context)?;
        let project_id = project_id(&registry)?;
        let Some(id) = self.id else {
            return Err(CliError::message("usage: shepherd mem show <id>"));
        };
        let mut rows = query_mem_rows(
            &registry,
            "SELECT id, kind, title, body, tags, pinned, created_at, updated_at FROM mem_entries WHERE project_id = ?1 AND id = ?2",
            params![project_id, id],
        )?;
        let row = rows.pop();
        if self.json {
            return write_stdout(
                &mut context,
                &row.map_or_else(|| "null".to_owned(), |row| render_show_json(&row)),
            );
        }
        let values = row.map(|row| {
            vec![vec![
                row.id,
                row.kind,
                row.title,
                row.body,
                row.tags,
                row.pinned.to_string(),
                row.created_at.to_string(),
                row.updated_at.to_string(),
            ]]
        });
        write_if_nonempty(
            &mut context,
            &values.map_or_else(String::new, |rows| render_table(&SHOW_COLUMNS, &rows)),
        )
    }
}

impl MemIdCmd {
    fn set_pinned(self, globals: CliGlobals, value: i64, command: &str) -> Result<(), CliError> {
        let context = context(globals)?;
        let registry =
            Registry::open(&context.registry_path, OpenMode::ReadWrite).map_err(registry_error)?;
        let project_id = project_id(&registry)?;
        let Some(id) = self.id else {
            return Err(CliError::message(format!(
                "usage: shepherd mem {command} <id>"
            )));
        };
        registry
            .execute(
                "UPDATE mem_entries SET pinned = ?1, updated_at = ?2 WHERE id = ?3 AND project_id = ?4",
                params![value, now_seconds(), id, project_id],
            )
            .map_err(registry_error)?;
        Ok(())
    }

    fn remove(self, globals: CliGlobals) -> Result<(), CliError> {
        let mut context = context(globals)?;
        let registry =
            Registry::open(&context.registry_path, OpenMode::ReadWrite).map_err(registry_error)?;
        let project_id = project_id(&registry)?;
        let Some(id) = self.id else {
            return Err(CliError::message("usage: shepherd mem rm <id>"));
        };
        registry
            .execute(
                "DELETE FROM mem_entries WHERE project_id = ?1 AND id = ?2",
                params![project_id, id],
            )
            .map_err(registry_error)?;
        write_stdout(&mut context, &format!("shepherd mem rm: removed {id}"))
    }
}

fn context(globals: CliGlobals) -> Result<ExecutionContext, CliError> {
    let cwd = std::env::current_dir()
        .map_err(|error| CliError::message(format!("cannot resolve current directory: {error}")))?;
    let mut inputs = ContextInputs::from_environment(cwd)
        .map_err(|error| CliError::message(error.to_string()))?;
    inputs.explicit_config = globals.config;
    inputs.verbosity = globals.verbosity;
    ExecutionContext::discover(inputs).map_err(|error| CliError::message(error.to_string()))
}

fn open_read_registry(context: &ExecutionContext) -> Result<Registry, CliError> {
    Registry::open(&context.registry_path, OpenMode::ReadOnly).map_err(registry_error)
}

fn project_id(registry: &Registry) -> Result<String, CliError> {
    registry
        .query("SELECT id FROM projects LIMIT 1", [], |row| row.get(0))
        .map_err(registry_error)?
        .into_iter()
        .next()
        .ok_or_else(|| CliError::message("no project registered — run 'shepherd init' first"))
}

fn query_mem_rows<P: rusqlite::Params>(
    registry: &Registry,
    sql: &str,
    params: P,
) -> Result<Vec<MemRow>, CliError> {
    registry
        .query(sql, params, |row| {
            Ok(MemRow {
                id: row.get(0)?,
                kind: row.get(1)?,
                title: row.get(2)?,
                body: row.get(3)?,
                tags: row.get(4)?,
                pinned: row.get(5)?,
                created_at: row.get(6)?,
                updated_at: row.get(7)?,
            })
        })
        .map_err(registry_error)
}

fn render_list_json(rows: &[MemRow]) -> String {
    let items = rows
        .iter()
        .map(|row| {
            format!(
                "  {{\n    \"id\": {},\n    \"kind\": {},\n    \"title\": {},\n    \"pinned\": {},\n    \"created_at\": {}\n  }}",
                json(&row.id), json(&row.kind), json(&row.title), row.pinned, row.created_at
            )
        })
        .collect::<Vec<_>>();
    format!("[\n{}\n]", items.join(",\n"))
}

fn render_search_json(rows: &[MemRow]) -> String {
    let items = rows
        .iter()
        .map(|row| {
            format!(
                "  {{\n    \"id\": {},\n    \"kind\": {},\n    \"title\": {},\n    \"pinned\": {}\n  }}",
                json(&row.id), json(&row.kind), json(&row.title), row.pinned
            )
        })
        .collect::<Vec<_>>();
    format!("[\n{}\n]", items.join(",\n"))
}

fn render_show_json(row: &MemRow) -> String {
    format!(
        "{{\n  \"id\": {},\n  \"kind\": {},\n  \"title\": {},\n  \"body\": {},\n  \"tags\": {},\n  \"pinned\": {},\n  \"created_at\": {},\n  \"updated_at\": {}\n}}",
        json(&row.id),
        json(&row.kind),
        json(&row.title),
        json(&row.body),
        json(&row.tags),
        row.pinned,
        row.created_at,
        row.updated_at
    )
}

fn render_table(columns: &[&str], rows: &[Vec<String>]) -> String {
    if rows.is_empty() {
        return String::new();
    }
    let mut widths = columns
        .iter()
        .map(|column| column.len())
        .collect::<Vec<_>>();
    for row in rows {
        for (index, cell) in row.iter().enumerate() {
            widths[index] = widths[index].max(cell.len());
        }
    }
    let line = |cells: &[String]| {
        cells
            .iter()
            .enumerate()
            .map(|(index, cell)| format!("{cell:<width$}", width = widths[index]))
            .collect::<Vec<_>>()
            .join("  ")
    };
    let header = columns
        .iter()
        .map(|value| value.to_string())
        .collect::<Vec<_>>();
    let separator = widths
        .iter()
        .map(|width| "-".repeat(*width))
        .collect::<Vec<_>>();
    let mut output = vec![line(&header), line(&separator)];
    output.extend(rows.iter().map(|row| line(row)));
    output.join("\n")
}

fn json(value: &str) -> String {
    serde_json::to_string(value).expect("serializing a string cannot fail")
}

fn now_seconds() -> i64 {
    i64::try_from(
        SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap_or_default()
            .as_secs(),
    )
    .unwrap_or(i64::MAX)
}

fn write_if_nonempty(context: &mut ExecutionContext, output: &str) -> Result<(), CliError> {
    if output.is_empty() {
        Ok(())
    } else {
        write_stdout(context, output)
    }
}

fn write_stdout(context: &mut ExecutionContext, output: &str) -> Result<(), CliError> {
    let mut bytes = output.as_bytes().to_vec();
    bytes.push(b'\n');
    context
        .write_stdout(&bytes)
        .map_err(|error| CliError::message(format!("cannot write stdout: {error}")))
}

fn registry_error(error: shepherd::registry::Error) -> CliError {
    CliError::message(error.to_string())
}