logbrew-cli 0.1.27

Public command-line interface for LogBrew.
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
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
//! Support-ticket command grammar.

use super::unknown_resource;
use crate::{
    CliError, Command, HelpTopic, SupportContextReplyOptions, SupportTarget,
    SupportTicketCreateOptions, SupportTicketLifecycleStatus, SupportTicketListOptions,
};

/// Recovery shown for invalid support command shapes.
const SUPPORT_NEXT_STEP: &str = "run logbrew support --help";

/// One value-taking support create field.
#[derive(Clone, Copy)]
enum CreateField {
    /// Support category.
    Category,
    /// Concise summary.
    Title,
    /// Reproduction details.
    Description,
    /// Related project identifier.
    ProjectId,
    /// Affected environment.
    Environment,
    /// Affected runtime.
    Runtime,
    /// Affected framework.
    Framework,
    /// Affected SDK package.
    SdkPackage,
    /// Affected SDK version.
    SdkVersion,
    /// Affected release.
    Release,
    /// Related trace identifier.
    TraceId,
    /// Related event identifier.
    EventId,
}

/// Canonical metadata for one value-taking support create flag.
struct CreateFieldSpec {
    /// Destination field.
    field: CreateField,
    /// Stable duplicate-detection name.
    canonical_flag: &'static str,
    /// User-facing spelling.
    visible_flag: &'static str,
}

/// Parses the authenticated support-ticket workflow.
pub(super) fn parse_support(args: &[String]) -> Result<Command, CliError> {
    let Some((action, tail)) = args.split_first() else {
        return Ok(Command::Help {
            topic: HelpTopic::Support,
            json: false,
        });
    };
    if action == "--json" && tail.is_empty() {
        return Ok(Command::Help {
            topic: HelpTopic::Support,
            json: true,
        });
    }
    match action.as_str() {
        "create" => parse_create(tail),
        "list" | "tickets" => parse_list(tail),
        "show" | "ticket" => parse_detail(tail),
        "context" => parse_context_history(tail),
        "reply" => parse_context_reply(tail),
        "close" => parse_lifecycle(tail, SupportTicketLifecycleStatus::Closed, "support close"),
        "reopen" => parse_lifecycle(tail, SupportTicketLifecycleStatus::Open, "support reopen"),
        other => Err(unknown_resource(other, SUPPORT_NEXT_STEP)),
    }
}

/// Parses one support context history read.
fn parse_context_history(args: &[String]) -> Result<Command, CliError> {
    let (ticket_id, json) = parse_ticket_id_and_json(args)?;
    Ok(Command::Support {
        target: SupportTarget::ContextHistory { ticket_id },
        json,
    })
}

/// Parses one idempotent support context reply.
fn parse_context_reply(args: &[String]) -> Result<Command, CliError> {
    let Some((ticket_id, tail)) = args.split_first() else {
        return Err(CliError::MissingArgument {
            argument: "ticket_id",
            next: "provide a support ticket id",
        });
    };
    if !crate::ids::is_support_ticket_id(ticket_id) {
        return Err(CliError::InvalidSupportTicketId);
    }

    let mut options = SupportContextReplyOptions {
        ticket_id: ticket_id.clone(),
        ..SupportContextReplyOptions::default()
    };
    let mut json = false;
    let mut seen = Vec::new();
    let mut index = 0;
    while let Some(raw) = tail.get(index) {
        let (flag, inline_value) = split_flag(raw);
        match flag {
            "--context" => {
                options.context = take_support_reply_value(
                    tail,
                    &mut index,
                    inline_value,
                    &mut seen,
                    "--context",
                    "--context",
                )?;
            }
            "--retry-key" | "--idempotency-key" => {
                options.retry_key = take_support_reply_value(
                    tail,
                    &mut index,
                    inline_value,
                    &mut seen,
                    "--retry-key",
                    if flag == "--idempotency-key" {
                        "--idempotency-key"
                    } else {
                        "--retry-key"
                    },
                )?;
            }
            "--diagnostics" => {
                reject_inline(inline_value, "--diagnostics")?;
                mark_seen(&mut seen, "--diagnostics")?;
                options.diagnostics = true;
            }
            "--json" => {
                reject_inline(inline_value, "--json")?;
                mark_seen(&mut seen, "--json")?;
                json = true;
            }
            _ => return Err(CliError::InvalidSupportContextReply),
        }
        index += 1;
    }
    let context = options.context.trim().to_owned();
    if !(1..=4000).contains(&context.chars().count()) {
        return Err(CliError::InvalidSupportContext);
    }
    options.context = context;
    if !(1..=128).contains(&options.retry_key.len())
        || !options
            .retry_key
            .bytes()
            .all(|byte| matches!(byte, b'!'..=b'~'))
    {
        return Err(CliError::InvalidSupportRetryKey);
    }
    Ok(Command::Support {
        target: SupportTarget::ReplyContext(Box::new(options)),
        json,
    })
}

/// Parses one exact public ticket id followed only by optional JSON output.
fn parse_ticket_id_and_json(args: &[String]) -> Result<(String, bool), CliError> {
    let Some((ticket_id, tail)) = args.split_first() else {
        return Err(CliError::MissingArgument {
            argument: "ticket_id",
            next: "provide a support ticket id",
        });
    };
    if !crate::ids::is_support_ticket_id(ticket_id) {
        return Err(CliError::InvalidSupportTicketId);
    }
    let mut json = false;
    let mut seen = Vec::new();
    for value in tail {
        if value == "--json" {
            mark_seen(&mut seen, "--json")?;
            json = true;
        } else {
            return Err(CliError::InvalidSupportContextCommand);
        }
    }
    Ok((ticket_id.clone(), json))
}

/// Parses support-ticket creation flags.
fn parse_create(args: &[String]) -> Result<Command, CliError> {
    let mut options = SupportTicketCreateOptions::default();
    let mut json = false;
    let mut seen = Vec::new();
    let mut index = 0;
    while let Some(raw) = args.get(index) {
        let (flag, inline_value) = split_flag(raw);
        if let Some(spec) = create_field_spec(flag) {
            let value = take_flag_value(
                args,
                &mut index,
                inline_value,
                &mut seen,
                spec.canonical_flag,
                spec.visible_flag,
            )?;
            set_create_field(&mut options, spec.field, value);
            index += 1;
            continue;
        }
        match flag {
            "--json" => {
                reject_inline(inline_value, "--json")?;
                mark_seen(&mut seen, "--json")?;
                json = true;
            }
            "--diagnostics" => {
                reject_inline(inline_value, "--diagnostics")?;
                mark_seen(&mut seen, "--diagnostics")?;
                options.diagnostics = true;
            }
            value if value.starts_with('-') => return Err(unknown_flag(value)),
            value => return Err(unexpected_argument(value, "support create")),
        }
        index += 1;
    }
    require_create_field(
        options.category.as_str(),
        "category",
        "provide --category with a supported support category",
    )?;
    require_create_field(
        options.title.as_str(),
        "title",
        "provide --title with a concise summary",
    )?;
    require_create_field(
        options.description.as_str(),
        "description",
        "provide --description with reproducible details",
    )?;
    validate_category(options.category.as_str())?;
    Ok(Command::Support {
        target: SupportTarget::Create(Box::new(options)),
        json,
    })
}

/// Resolves one value-taking create flag to its canonical field.
fn create_field_spec(flag: &str) -> Option<CreateFieldSpec> {
    let (field, canonical_flag) = match flag {
        "--category" => (CreateField::Category, "--category"),
        "--title" => (CreateField::Title, "--title"),
        "--description" => (CreateField::Description, "--description"),
        "--project" | "--project-id" => (CreateField::ProjectId, "--project"),
        "--environment" | "--env" => (CreateField::Environment, "--environment"),
        "--runtime" => (CreateField::Runtime, "--runtime"),
        "--framework" => (CreateField::Framework, "--framework"),
        "--sdk-package" => (CreateField::SdkPackage, "--sdk-package"),
        "--sdk-version" => (CreateField::SdkVersion, "--sdk-version"),
        "--release" => (CreateField::Release, "--release"),
        "--trace-id" => (CreateField::TraceId, "--trace-id"),
        "--event-id" => (CreateField::EventId, "--event-id"),
        _ => return None,
    };
    Some(CreateFieldSpec {
        field,
        canonical_flag,
        visible_flag: match flag {
            "--project-id" => "--project-id",
            "--env" => "--env",
            _ => canonical_flag,
        },
    })
}

/// Assigns one parsed support create field.
fn set_create_field(options: &mut SupportTicketCreateOptions, field: CreateField, value: String) {
    match field {
        CreateField::Category => options.category = value,
        CreateField::Title => options.title = value,
        CreateField::Description => options.description = value,
        CreateField::ProjectId => options.project_id = Some(value),
        CreateField::Environment => options.environment = Some(value),
        CreateField::Runtime => options.runtime = Some(value),
        CreateField::Framework => options.framework = Some(value),
        CreateField::SdkPackage => options.sdk_package = Some(value),
        CreateField::SdkVersion => options.sdk_version = Some(value),
        CreateField::Release => options.release = Some(value),
        CreateField::TraceId => options.trace_id = Some(value),
        CreateField::EventId => options.event_id = Some(value),
    }
}

/// Parses support-ticket history filters.
fn parse_list(args: &[String]) -> Result<Command, CliError> {
    let mut options = SupportTicketListOptions::default();
    let mut json = false;
    let mut seen = Vec::new();
    let mut index = 0;
    while let Some(raw) = args.get(index) {
        let (flag, inline_value) = split_flag(raw);
        match flag {
            "--json" => {
                reject_inline(inline_value, "--json")?;
                mark_seen(&mut seen, "--json")?;
                json = true;
            }
            "--project" | "--project-id" => {
                options.project_id = Some(take_flag_value(
                    args,
                    &mut index,
                    inline_value,
                    &mut seen,
                    "--project",
                    if flag == "--project-id" {
                        "--project-id"
                    } else {
                        "--project"
                    },
                )?);
            }
            "--status" => {
                options.status = Some(take_support_string(
                    args,
                    &mut index,
                    inline_value,
                    &mut seen,
                    "--status",
                )?);
            }
            "--source" => {
                options.source = Some(take_support_string(
                    args,
                    &mut index,
                    inline_value,
                    &mut seen,
                    "--source",
                )?);
            }
            "--category" => {
                let category =
                    take_support_string(args, &mut index, inline_value, &mut seen, "--category")?;
                validate_category(category.as_str())?;
                options.category = Some(category);
            }
            "--release" => {
                options.release = Some(take_support_string(
                    args,
                    &mut index,
                    inline_value,
                    &mut seen,
                    "--release",
                )?);
            }
            "--limit" => {
                let limit =
                    take_support_string(args, &mut index, inline_value, &mut seen, "--limit")?;
                if !limit.parse::<u32>().is_ok_and(|value| value > 0) {
                    return Err(CliError::InvalidLimit(limit));
                }
                options.limit = Some(limit);
            }
            "--pagination" => {
                let pagination =
                    take_support_string(args, &mut index, inline_value, &mut seen, "--pagination")?;
                if pagination != "cursor" {
                    return Err(CliError::UnknownPagination);
                }
                options.pagination = Some(pagination);
            }
            "--cursor-time" => {
                options.cursor_time = Some(take_support_string(
                    args,
                    &mut index,
                    inline_value,
                    &mut seen,
                    "--cursor-time",
                )?);
            }
            "--cursor-id" => {
                options.cursor_id = Some(take_support_string(
                    args,
                    &mut index,
                    inline_value,
                    &mut seen,
                    "--cursor-id",
                )?);
            }
            value if value.starts_with('-') => return Err(unknown_flag(value)),
            value => return Err(unexpected_argument(value, "support list")),
        }
        index += 1;
    }
    finish_list(options, json)
}

/// Validates and builds one support-ticket list command.
fn finish_list(options: SupportTicketListOptions, json: bool) -> Result<Command, CliError> {
    validate_cursor(&options)?;
    Ok(Command::Support {
        target: SupportTarget::List(Box::new(options)),
        json,
    })
}

/// Parses one support-ticket detail read.
fn parse_detail(args: &[String]) -> Result<Command, CliError> {
    let Some((ticket_id, tail)) = args.split_first() else {
        return Err(CliError::MissingArgument {
            argument: "ticket_id",
            next: "provide a support ticket id",
        });
    };
    if ticket_id.starts_with('-') {
        return Err(CliError::MissingArgument {
            argument: "ticket_id",
            next: "provide a support ticket id",
        });
    }
    if !crate::ids::is_support_ticket_id(ticket_id) {
        return Err(CliError::InvalidSupportTicketId);
    }
    let mut json = false;
    let mut seen = Vec::new();
    for value in tail {
        if value == "--json" {
            mark_seen(&mut seen, "--json")?;
            json = true;
        } else if value.starts_with('-') {
            return Err(unknown_flag(value));
        } else {
            return Err(unexpected_argument(value, "support show"));
        }
    }
    Ok(Command::Support {
        target: SupportTarget::Detail(ticket_id.clone()),
        json,
    })
}

/// Parses one public support-ticket lifecycle update.
fn parse_lifecycle(
    args: &[String],
    status: SupportTicketLifecycleStatus,
    command: &'static str,
) -> Result<Command, CliError> {
    let Some((ticket_id, tail)) = args.split_first() else {
        return Err(CliError::MissingArgument {
            argument: "ticket_id",
            next: "provide a support ticket id",
        });
    };
    if !crate::ids::is_support_ticket_id(ticket_id) {
        return Err(CliError::InvalidSupportTicketId);
    }
    let mut json = false;
    let mut seen = Vec::new();
    for value in tail {
        if value == "--json" {
            mark_seen(&mut seen, "--json")?;
            json = true;
        } else if value.starts_with('-') {
            return Err(unknown_flag(value));
        } else {
            return Err(unexpected_argument(value, command));
        }
    }
    Ok(Command::Support {
        target: SupportTarget::UpdateStatus {
            ticket_id: ticket_id.clone(),
            status,
        },
        json,
    })
}

/// Validates the opt-in support cursor shape.
fn validate_cursor(options: &SupportTicketListOptions) -> Result<(), CliError> {
    if options
        .cursor_id
        .as_deref()
        .is_some_and(|value| !crate::ids::is_support_ticket_id(value))
    {
        return Err(CliError::InvalidSupportTicketId);
    }
    match (
        options.pagination.as_deref(),
        options.cursor_time.as_ref(),
        options.cursor_id.as_ref(),
    ) {
        (None | Some("cursor"), None, None) | (Some("cursor"), Some(_), Some(_)) => Ok(()),
        (None, _, _) => Err(CliError::InvalidSupportCursor(String::from(
            "cursor fields require --pagination cursor",
        ))),
        (Some("cursor"), _, _) => Err(CliError::InvalidSupportCursor(String::from(
            "--cursor-time and --cursor-id must be used together",
        ))),
        (Some(_), _, _) => Err(CliError::UnknownPagination),
    }
}

/// Validates one canonical support category without retaining invalid input.
fn validate_category(category: &str) -> Result<(), CliError> {
    if matches!(
        category,
        "sdk_install_failure"
            | "ingest_failure"
            | "auth_failure"
            | "project_setup"
            | "dashboard_issue"
            | "docs_confusion"
            | "cli_issue"
            | "mobile_issue"
            | "billing_question"
            | "other"
    ) {
        Ok(())
    } else {
        Err(CliError::UnknownSupportCategory)
    }
}

/// Requires a non-empty support create field.
fn require_create_field(
    value: &str,
    argument: &'static str,
    next: &'static str,
) -> Result<(), CliError> {
    if value.trim().is_empty() {
        Err(CliError::MissingArgument { argument, next })
    } else {
        Ok(())
    }
}

/// Reads one value-taking support flag.
fn take_support_string(
    args: &[String],
    index: &mut usize,
    inline_value: Option<&str>,
    seen: &mut Vec<&'static str>,
    flag: &'static str,
) -> Result<String, CliError> {
    take_flag_value(args, index, inline_value, seen, flag, flag)
}

/// Reads one support flag value after duplicate validation.
fn take_flag_value(
    args: &[String],
    index: &mut usize,
    inline_value: Option<&str>,
    seen: &mut Vec<&'static str>,
    canonical_flag: &'static str,
    visible_flag: &'static str,
) -> Result<String, CliError> {
    mark_seen(seen, canonical_flag)?;
    let value = inline_value.unwrap_or_else(|| {
        *index += 1;
        args.get(*index).map_or("", String::as_str)
    });
    if value.is_empty() || value.starts_with('-') {
        Err(CliError::MissingFlagValue {
            flag: visible_flag,
            next: missing_value_next(visible_flag),
        })
    } else {
        Ok(value.to_owned())
    }
}

/// Reads one context-reply value across the full public string domain.
fn take_support_reply_value(
    args: &[String],
    index: &mut usize,
    inline_value: Option<&str>,
    seen: &mut Vec<&'static str>,
    canonical_flag: &'static str,
    visible_flag: &'static str,
) -> Result<String, CliError> {
    mark_seen(seen, canonical_flag)?;
    let value = inline_value.unwrap_or_else(|| {
        *index += 1;
        args.get(*index).map_or("", String::as_str)
    });
    if value.is_empty() {
        Err(CliError::MissingFlagValue {
            flag: visible_flag,
            next: missing_value_next(visible_flag),
        })
    } else {
        Ok(value.to_owned())
    }
}

/// Rejects `--flag=value` for valueless flags.
fn reject_inline(value: Option<&str>, flag: &'static str) -> Result<(), CliError> {
    if value.is_some() {
        Err(CliError::UnknownFlag {
            flag: flag.to_owned(),
            next: SUPPORT_NEXT_STEP,
        })
    } else {
        Ok(())
    }
}

/// Records one canonical support flag.
fn mark_seen(seen: &mut Vec<&'static str>, flag: &'static str) -> Result<(), CliError> {
    if seen.contains(&flag) {
        Err(CliError::DuplicateFlag {
            flag,
            next: if flag == "--json" {
                "use --json once"
            } else {
                SUPPORT_NEXT_STEP
            },
        })
    } else {
        seen.push(flag);
        Ok(())
    }
}

/// Splits inline support flag values.
fn split_flag(value: &str) -> (&str, Option<&str>) {
    value
        .split_once('=')
        .map_or((value, None), |(flag, value)| (flag, Some(value)))
}

/// Builds a support-specific unknown flag error.
fn unknown_flag(flag: &str) -> CliError {
    CliError::UnknownFlag {
        flag: flag.to_owned(),
        next: SUPPORT_NEXT_STEP,
    }
}

/// Builds a support-specific unexpected positional error.
fn unexpected_argument(argument: &str, command: &'static str) -> CliError {
    CliError::UnexpectedArgument {
        argument: argument.to_owned(),
        command,
        next: SUPPORT_NEXT_STEP,
    }
}

/// Returns a concrete missing-value recovery for support flags.
fn missing_value_next(flag: &'static str) -> &'static str {
    match flag {
        "--category" => "provide a value after --category",
        "--title" => "provide a value after --title",
        "--description" => "provide a value after --description",
        "--project" => "provide a value after --project",
        "--project-id" => "provide a value after --project-id",
        "--environment" => "provide a value after --environment",
        "--env" => "provide a value after --env",
        "--runtime" => "provide a value after --runtime",
        "--framework" => "provide a value after --framework",
        "--sdk-package" => "provide a value after --sdk-package",
        "--sdk-version" => "provide a value after --sdk-version",
        "--release" => "provide a value after --release",
        "--trace-id" => "provide a value after --trace-id",
        "--event-id" => "provide a value after --event-id",
        "--status" => "provide a value after --status",
        "--source" => "provide a value after --source",
        "--limit" => "provide a value after --limit",
        "--pagination" => "provide a value after --pagination",
        "--cursor-time" => "provide a value after --cursor-time",
        "--cursor-id" => "provide a value after --cursor-id",
        "--context" => "provide a value after --context",
        "--retry-key" => "provide a value after --retry-key",
        "--idempotency-key" => "provide a value after --idempotency-key",
        _ => "provide a value after the flag",
    }
}