agent-loadout 1.0.0

Loadout CLI: manage MCP servers, skills and secret references for AI coding clients
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
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
//! `loadout catalog ls|add|rm|enable|disable|online|search|show|refresh`
//! (#26). Catalogs are contacted only when online catalogs are on (`catalog
//! online on`) and only by these commands and the installs that name a
//! catalog entry (`server add --from catalog:<source>/<id>`, `skills add
//! catalog:<source>/<id>`). Catalog text is data from a third party.

use std::time::Duration;

use agent_loadout_core::catalog::{
    self, Catalog, CatalogEntry, CatalogEnv, CatalogSourceKind, HttpLimits, NewCatalog,
    ResultStatus, Results, UreqHttp,
};
use agent_loadout_core::sources::SourceRef;
use agent_loadout_core::store::StoreRead;
use agent_loadout_core::{CoreError, ErrorCode};
use clap::{Args, Subcommand, ValueEnum};
use serde_json::{Value, json};

use super::{Context, JSON_SCHEMA, error_json, print_json, require_store};

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

#[derive(Debug, Subcommand)]
enum CatalogCommand {
    /// List catalogs (built-in and added) and the online catalogs setting.
    Ls,
    /// Add an MCP registry (any server of the MCP Registry API) or a git
    /// tap (skill folders and/or a Claude Code plugin marketplace).
    Add {
        /// The registry's base URL, or the tap's git URL.
        url: String,
        /// What the URL is [default: tap for git URLs (ssh, file://, .git,
        /// github.com, gitlab.com, bitbucket.org), else registry].
        #[arg(long, value_enum)]
        kind: Option<KindArg>,
        /// Catalog id [default: from the URL].
        #[arg(long)]
        id: Option<String>,
        /// A tap's branch, tag or commit (a commit pins it).
        #[arg(long = "ref", value_name = "REF")]
        git_ref: Option<String>,
        /// Display name.
        #[arg(long)]
        title: Option<String>,
    },
    /// Remove an added catalog (built-ins are turned off instead).
    Rm { id: String },
    /// Turn a catalog on.
    Enable { id: String },
    /// Turn a catalog off: it is not searched or contacted.
    Disable { id: String },
    /// Show or set the online catalogs setting of this machine (off until
    /// turned on: nothing contacts a catalog before that).
    Online {
        #[arg(value_enum)]
        state: Option<OnOff>,
    },
    /// Search every enabled catalog (or one). An MCP registry needs at
    /// least 3 characters and skills.sh at least 2 (its ids are
    /// <owner>/<repo>/<skill>); a tap lists everything for an empty query.
    Search {
        query: String,
        /// Search only this catalog.
        #[arg(long, value_name = "ID")]
        catalog: Option<String>,
        /// The next page of a registry's results (the `next_cursor` of
        /// the last search).
        #[arg(long, requires = "catalog")]
        cursor: Option<String>,
    },
    /// Show one entry: <SOURCE>/<ID> (or catalog:<SOURCE>/<ID>).
    Show {
        #[arg(value_name = "SOURCE/ID")]
        entry: String,
    },
    /// Fetch again (every enabled catalog, or one): a tap is indexed again;
    /// a registry or skills.sh is asked whether it answers and its saved
    /// results are dropped only then. A failed refresh keeps them.
    Refresh { id: Option<String> },
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, ValueEnum)]
enum KindArg {
    Registry,
    Tap,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, ValueEnum)]
enum OnOff {
    On,
    Off,
}

/// The HTTP client of catalog commands.
pub(super) fn http(ctx: &Context) -> UreqHttp {
    UreqHttp::new(&ctx.env, HttpLimits::default())
}

pub fn run(ctx: &Context, args: &CatalogArgs) -> Result<u8, CoreError> {
    match &args.command {
        CatalogCommand::Ls => list(ctx),
        CatalogCommand::Add {
            url,
            kind,
            id,
            git_ref,
            title,
        } => {
            let kind = match kind.unwrap_or_else(|| guess_kind(url)) {
                KindArg::Registry => CatalogSourceKind::McpRegistry,
                KindArg::Tap => CatalogSourceKind::GitTap,
            };
            let store = ctx.open_store_for_change()?;
            let new = NewCatalog {
                kind,
                url: url.clone(),
                id: id.clone(),
                git_ref: git_ref.clone(),
                title: title.clone(),
            };
            let (id, commit) = catalog::add(&store, &new)?;
            changed(ctx, &format!("Added catalog {id}"), &id, commit.as_ref())
        }
        CatalogCommand::Rm { id } => {
            let store = require_store(ctx, "there is no catalog to remove")?;
            let sx = super::skills::skills_env(ctx);
            let commit = catalog::remove(&store, &sx, id)?;
            changed(ctx, &format!("Removed catalog {id}"), id, commit.as_ref())
        }
        CatalogCommand::Enable { id } | CatalogCommand::Disable { id } => {
            let enabled = matches!(args.command, CatalogCommand::Enable { .. });
            let store = ctx.open_store_for_change()?;
            let commit = catalog::set_enabled(&store, id, enabled)?;
            let verb = if enabled { "Turned on" } else { "Turned off" };
            changed(ctx, &format!("{verb} catalog {id}"), id, commit.as_ref())
        }
        CatalogCommand::Online { state } => online(ctx, *state),
        CatalogCommand::Search {
            query,
            catalog: only,
            cursor,
        } => search(ctx, query, only.as_deref(), cursor.as_deref()),
        CatalogCommand::Show { entry } => show(ctx, entry),
        CatalogCommand::Refresh { id } => refresh(ctx, id.as_deref()),
    }
}

/// `tap` for URLs git reads (ssh, scp-like, `file://`, `.git`, the big
/// git hosts), else `registry`.
fn guess_kind(url: &str) -> KindArg {
    let lower = url.trim().to_ascii_lowercase();
    let git_host = ["github.com", "gitlab.com", "bitbucket.org"]
        .iter()
        .any(|host| {
            lower.starts_with(&format!("https://{host}/"))
                || lower.starts_with(&format!("http://{host}/"))
        });
    let scp = lower
        .split_once(':')
        .is_some_and(|(user_host, rest)| user_host.contains('@') && !rest.starts_with("//"));
    if git_host
        || scp
        || lower.starts_with("ssh://")
        || lower.starts_with("git://")
        || lower.starts_with("file://")
        || lower.trim_end_matches('/').ends_with(".git")
    {
        KindArg::Tap
    } else {
        KindArg::Registry
    }
}

fn changed(
    ctx: &Context,
    text: &str,
    id: &str,
    commit: Option<&agent_loadout_core::store::CommitId>,
) -> Result<u8, CoreError> {
    if ctx.json {
        print_json(&json!({
            "schema": JSON_SCHEMA,
            "catalog": id,
            "commit": commit.map(ToString::to_string),
        }));
    } else {
        match commit {
            Some(commit) => println!("{text} (commit {}).", commit.short()),
            None => println!("Nothing changed."),
        }
    }
    Ok(0)
}

fn catalog_json(catalog: &Catalog) -> Value {
    json!({
        "id": catalog.id,
        "kind": catalog.kind.as_str(),
        "title": catalog.title,
        "url": SourceRef::redact_userinfo(&catalog.url),
        "ref": catalog.git_ref,
        "enabled": catalog.enabled,
        "builtin": catalog.builtin,
        "official": catalog.official,
        "attribution": catalog.attribution,
    })
}

fn list(ctx: &Context) -> Result<u8, CoreError> {
    let store = ctx.open_store()?;
    let catalogs = catalog::list(store.as_ref().map(|s| s as &dyn StoreRead))?;
    let setting = catalog::online(ctx.fs(), &ctx.home)?;
    let contacts = catalog::contacts(&catalogs);
    if ctx.json {
        print_json(&json!({
            "schema": JSON_SCHEMA,
            "online": setting.on,
            "decided": setting.decided(&contacts),
            "contacts": contacts,
            "catalogs": catalogs.iter().map(catalog_json).collect::<Vec<_>>(),
        }));
        return Ok(0);
    }
    println!("Online catalogs: {}", on_text(setting.on));
    for catalog in &catalogs {
        let mut notes = Vec::new();
        notes.push(catalog.kind.as_str().to_owned());
        if catalog.builtin {
            notes.push("built-in".to_owned());
        }
        if catalog.official {
            notes.push("official".to_owned());
        }
        if !catalog.enabled {
            notes.push("off".to_owned());
        }
        if let Some(git_ref) = &catalog.git_ref {
            notes.push(format!("ref {git_ref}"));
        }
        println!(
            "{:<20} {}  {}  ({})",
            catalog.id,
            catalog.title,
            SourceRef::redact_userinfo(&catalog.url),
            notes.join(", ")
        );
    }
    Ok(0)
}

fn on_text(on: bool) -> &'static str {
    if on {
        "on"
    } else {
        "off (nothing is contacted; turn on with `loadout catalog online on`)"
    }
}

fn online(ctx: &Context, state: Option<OnOff>) -> Result<u8, CoreError> {
    let store = ctx.open_store()?;
    let catalogs = catalog::list(store.as_ref().map(|s| s as &dyn StoreRead))?;
    let contacts = catalog::contacts(&catalogs);
    if let Some(state) = state {
        catalog::set_online(ctx.fs(), &ctx.home, state == OnOff::On, &contacts)?;
    }
    let setting = catalog::online(ctx.fs(), &ctx.home)?;
    if ctx.json {
        print_json(&json!({
            "schema": JSON_SCHEMA,
            "online": setting.on,
            "decided": setting.decided(&contacts),
            "contacts": contacts,
        }));
    } else {
        println!("Online catalogs: {}", on_text(setting.on));
        if setting.on {
            println!(
                "Searches, refreshes and catalog installs contact: {}.",
                contacts.join(", ")
            );
        }
    }
    Ok(0)
}

fn age_secs(age: Option<Duration>) -> Option<u64> {
    age.map(|age| age.as_secs())
}

fn age_text(age: Duration) -> String {
    let secs = age.as_secs();
    match secs {
        0..=59 => "just now".to_owned(),
        60..=3599 => format!("{} min ago", secs / 60),
        3600..=86_399 => format!("{} h ago", secs / 3600),
        _ => format!("{} days ago", secs / 86_400),
    }
}

/// An entry for `--json` (snake_case of the IPC keys).
pub(super) fn entry_json(entry: &CatalogEntry) -> Value {
    json!({
        "catalog": entry.catalog,
        "id": entry.id,
        "ref": entry.reference(),
        "kind": entry.kind.as_str(),
        "name": entry.name,
        "description": entry.description,
        "version": entry.version,
        "updated_at": entry.updated_at,
        "deprecated": entry.deprecated,
        "status_message": entry.status_message,
        "publisher": entry.publisher.as_ref().map(|p| json!({"name": p.name, "checked": p.checked})),
        "official": entry.official,
        "warning": entry.warning,
        "repository": entry.repository,
        "website": entry.website,
        "license": entry.license,
        "transports": entry.transports,
        "packages": entry.packages.iter().map(|p| json!({
            "registry_type": p.registry_type,
            "identifier": p.identifier,
            "version": p.version,
            "transport": p.transport,
            "pinned": p.pinned,
        })).collect::<Vec<_>>(),
        "remotes": entry.remotes.iter().map(|r| json!({"transport": r.transport, "url": r.url})).collect::<Vec<_>>(),
        "required_secrets": entry.required_secrets,
        "pinned": entry.pinned,
        "git_ref": entry.git_ref,
        "commit": entry.commit,
        "path": entry.path,
        "skills": entry.skills,
        "servers": entry.servers,
        "not_imported": entry.not_imported,
        "source": entry.source.as_ref().map(|s| json!({
            "kind": s.kind.as_str(),
            "url": s.url,
            "path": s.path,
            "git_ref": s.git_ref,
            "sha": s.sha,
        })),
        "install_mcp": entry.install_mcp,
        "install_skill": entry.install_skill,
        "problem": entry.problem,
        "installs": entry.installs,
    })
}

fn results_json(results: &Results) -> Value {
    json!({
        "catalog": results.catalog,
        "status": results.status.as_str(),
        "fetched_at": results.fetched_at.map(|at| agent_loadout_core::store::utc_datetime(at).to_string()),
        "age_secs": age_secs(results.age),
        "error": results.error.as_ref().map(error_json),
        "next_cursor": results.next_cursor,
        "attribution": results.attribution,
        "entries": results.entries.iter().map(entry_json).collect::<Vec<_>>(),
    })
}

/// One line of text from a catalog: control characters dropped, at most
/// `max` characters.
pub(super) fn plain(text: &str, max: usize) -> String {
    let clean: String = text
        .chars()
        .map(|c| if c.is_control() { ' ' } else { c })
        .collect();
    let clean = clean.split_whitespace().collect::<Vec<_>>().join(" ");
    if clean.chars().count() > max {
        let cut: String = clean.chars().take(max.saturating_sub(3)).collect();
        format!("{cut}...")
    } else {
        clean
    }
}

/// `1 install`, `950 installs`, `241,954 installs`.
fn installs_text(count: u64) -> String {
    let digits = count.to_string();
    let mut grouped = String::new();
    for (i, c) in digits.chars().enumerate() {
        if i > 0 && (digits.len() - i).is_multiple_of(3) {
            grouped.push(',');
        }
        grouped.push(c);
    }
    let noun = if count == 1 { "install" } else { "installs" };
    format!("{grouped} {noun}")
}

fn print_results(results: &Results) {
    let status = match (results.status, results.age) {
        (ResultStatus::Cached, Some(age)) => format!("cached, {}", age_text(age)),
        (ResultStatus::Stale, Some(age)) => {
            format!("offline: results from {}", age_text(age))
        }
        (ResultStatus::Off, Some(age)) => format!("online catalogs off: cached {}", age_text(age)),
        (status, _) => status.as_str().replace('_', " "),
    };
    println!("== {} ({status})", results.catalog);
    if let Some(attribution) = &results.attribution {
        println!("   {attribution}");
    }
    if let Some(err) = &results.error {
        println!("   [{}] {}", err.code(), err.message());
    }
    for entry in &results.entries {
        let version = entry
            .version
            .as_deref()
            .map(|v| format!(" {}", plain(v, 24)))
            .unwrap_or_default();
        let mut marks = Vec::new();
        if entry.official {
            marks.push("official");
        }
        if entry.deprecated {
            marks.push("deprecated");
        }
        if entry.problem.is_some() {
            marks.push("not installable");
        }
        let marks = if marks.is_empty() {
            String::new()
        } else {
            format!(" [{}]", marks.join(", "))
        };
        println!(
            "  {}  {}{version}{marks}{}",
            plain(&entry.reference(), 120),
            plain(&entry.name, 60),
            entry
                .installs
                .map(|n| format!(" ({})", installs_text(n)))
                .unwrap_or_default()
        );
        if let Some(description) = &entry.description {
            println!("      {}", plain(description, 100));
        }
    }
    if let Some(cursor) = &results.next_cursor {
        println!(
            "   More: --catalog {} --cursor {}",
            results.catalog,
            plain(cursor, 200)
        );
    }
}

fn search(
    ctx: &Context,
    query: &str,
    only: Option<&str>,
    cursor: Option<&str>,
) -> Result<u8, CoreError> {
    let store = ctx.open_store()?;
    let read = store.as_ref().map(|s| s as &dyn StoreRead);
    let sx = super::skills::skills_env(ctx);
    let http = http(ctx);
    let cx = CatalogEnv {
        sx: &sx,
        http: &http,
    };
    let results = match (only, cursor) {
        (Some(only), Some(_)) => vec![catalog::search_one(&cx, read, only, query, cursor)?],
        _ => catalog::search(&cx, read, query, only)?,
    };
    let answered = results.iter().any(|results| {
        !results.entries.is_empty()
            || matches!(
                results.status,
                ResultStatus::Fresh
                    | ResultStatus::Cached
                    | ResultStatus::Stale
                    | ResultStatus::QueryTooShort
                    | ResultStatus::Disabled
            )
    });
    if ctx.json {
        print_json(&json!({
            "schema": JSON_SCHEMA,
            "query": query,
            "results": results.iter().map(results_json).collect::<Vec<_>>(),
        }));
    } else {
        for results in &results {
            print_results(results);
        }
    }
    Ok(if answered { 0 } else { 1 })
}

/// `<source>/<id>` or `catalog:<source>/<id>`.
fn reference(text: &str) -> String {
    if text.starts_with(catalog::REF_PREFIX) {
        text.to_owned()
    } else {
        format!("{}{text}", catalog::REF_PREFIX)
    }
}

fn show(ctx: &Context, text: &str) -> Result<u8, CoreError> {
    let (source, id) = catalog::parse_ref(&reference(text))?;
    let store = ctx.open_store()?;
    let read = store.as_ref().map(|s| s as &dyn StoreRead);
    let sx = super::skills::skills_env(ctx);
    let http = http(ctx);
    let cx = CatalogEnv {
        sx: &sx,
        http: &http,
    };
    let shown = catalog::show(&cx, read, &source, &id)?;
    if ctx.json {
        print_json(&json!({
            "schema": JSON_SCHEMA,
            "status": shown.status.as_str(),
            "age_secs": age_secs(shown.age),
            "error": shown.error.as_ref().map(error_json),
            "entry": entry_json(&shown.entry),
        }));
        return Ok(0);
    }
    print_entry(&shown.entry);
    match (shown.status, shown.age) {
        (ResultStatus::Stale, Some(age)) => println!("(offline: from {})", age_text(age)),
        (ResultStatus::Off, Some(age)) => {
            println!("(online catalogs off: cached {})", age_text(age))
        }
        _ => {}
    }
    if let Some(err) = &shown.error {
        eprintln!("warning[{}]: {}", err.code(), err.message());
    }
    Ok(0)
}

/// An entry for people. Catalog text is third-party data: printed as
/// plain text.
pub(super) fn print_entry(entry: &CatalogEntry) {
    let line = |label: &str, value: &str| println!("{label:<18} {}", plain(value, 200));
    line("Entry:", &entry.reference());
    line("Name:", &entry.name);
    line("Kind:", entry.kind.as_str());
    if let Some(text) = &entry.description {
        line("Description:", text);
    }
    if let Some(publisher) = &entry.publisher {
        match &publisher.checked {
            Some(checked) => line("Publisher:", &format!("{} ({checked})", publisher.name)),
            None => line("Publisher:", &publisher.name),
        }
    }
    if let Some(installs) = entry.installs {
        line("Installs:", &installs_text(installs));
    }
    if entry.official {
        line("Official:", "yes (a repository of github.com/anthropics)");
    }
    if let Some(version) = &entry.version {
        line("Version:", version);
    }
    if let Some(updated) = &entry.updated_at {
        line("Updated:", updated);
    }
    if let Some(repo) = &entry.repository {
        // A registry's `repository.url` is the publisher's word: the
        // registry does not check it, and trust is decided on what runs.
        let label = if entry.server_json.is_some() {
            "Repository (claimed by the publisher, not verified):"
        } else {
            "Repository:"
        };
        line(label, repo);
    }
    if let Some(site) = &entry.website {
        line("Website:", site);
    }
    if let Some(license) = &entry.license {
        line("License:", license);
    }
    if !entry.transports.is_empty() {
        line("Transports:", &entry.transports.join(", "));
    }
    for package in &entry.packages {
        let version = package.version.as_deref().unwrap_or("no version");
        let pin = if package.pinned { "" } else { " (not pinned)" };
        line(
            "Package:",
            &format!(
                "{} {} {version}{pin}",
                package.registry_type, package.identifier
            ),
        );
    }
    for remote in &entry.remotes {
        line("Remote:", &format!("{} {}", remote.transport, remote.url));
    }
    if !entry.required_secrets.is_empty() {
        line("Needs secrets:", &entry.required_secrets.join(", "));
    }
    if entry.kind != agent_loadout_core::catalog::EntryKind::Server {
        match (&entry.commit, &entry.git_ref, entry.pinned) {
            (Some(commit), _, true) => line("Pinned to:", commit),
            (_, Some(git_ref), false) => line("Follows:", &format!("branch or tag {git_ref}")),
            (Some(commit), None, false) => {
                line("Follows:", &format!("the default branch (now at {commit})"))
            }
            _ => {}
        }
    }
    if let Some(path) = &entry.path {
        line("Folder:", path);
    }
    if let Some(source) = &entry.source {
        let mut text = source.kind.as_str().to_owned();
        if let Some(url) = &source.url {
            text.push_str(&format!(" {url}"));
        }
        if let Some(path) = &source.path {
            text.push_str(&format!(" ({path})"));
        }
        line("Source:", &text);
    }
    if !entry.skills.is_empty() {
        line("Skills:", &entry.skills.join(", "));
    }
    if !entry.servers.is_empty() {
        line("MCP servers:", &entry.servers.join(", "));
    }
    if !entry.not_imported.is_empty() {
        line("Not imported:", &entry.not_imported.join(", "));
    }
    if entry.deprecated {
        line(
            "Deprecated:",
            entry.status_message.as_deref().unwrap_or("yes"),
        );
    }
    if let Some(warning) = &entry.warning {
        line("Warning:", warning);
    }
    match &entry.problem {
        Some(problem) => line("Cannot install:", problem),
        None => {
            if entry.install_mcp {
                line(
                    "Add with:",
                    &format!("loadout server add --from {}", entry.reference()),
                );
            }
            if entry.install_skill {
                line(
                    "Install with:",
                    &format!("loadout skills add {}", entry.reference()),
                );
            }
        }
    }
}

fn refresh(ctx: &Context, only: Option<&str>) -> Result<u8, CoreError> {
    let store = ctx.open_store()?;
    let read = store.as_ref().map(|s| s as &dyn StoreRead);
    let sx = super::skills::skills_env(ctx);
    let http = http(ctx);
    let cx = CatalogEnv {
        sx: &sx,
        http: &http,
    };
    let results = catalog::refresh(&cx, read, only)?;
    let failed = results
        .iter()
        .any(|results| results.status == ResultStatus::Error);
    if ctx.json {
        print_json(&json!({
            "schema": JSON_SCHEMA,
            "query": Value::Null,
            "results": results.iter().map(results_json).collect::<Vec<_>>(),
        }));
    } else {
        for results in &results {
            match &results.error {
                Some(err) => println!(
                    "{}: failed\n  [{}] {}",
                    results.catalog,
                    err.code(),
                    err.message()
                ),
                None => println!(
                    "{}: refreshed ({} entries)",
                    results.catalog,
                    results.entries.len()
                ),
            }
        }
    }
    Ok(u8::from(failed))
}

/// A catalog reference given where it cannot be used.
pub(super) fn not_here(what: &str) -> CoreError {
    CoreError::new(
        ErrorCode::InvalidValue,
        format!("{what} does not apply to a catalog entry"),
    )
}

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

    #[test]
    fn installs_are_counted_in_full_with_separators() {
        assert_eq!(installs_text(0), "0 installs");
        assert_eq!(installs_text(1), "1 install");
        assert_eq!(installs_text(999), "999 installs");
        assert_eq!(installs_text(999_950), "999,950 installs");
        assert_eq!(installs_text(241_954_000), "241,954,000 installs");
    }
}