coralogix-cli 0.1.19

Coralogix CLI - the observability backbone for AI agents and engineering teams.
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
//! Skills install step: shells out to the vercel-labs `skills` npx installer
//! to install the cx agent skills bundle (`coralogix/cx-cli/skills`).
//!
//! Owns no skill/agent logic of its own beyond driving the one scope
//! question (global vs local) and passing flags through to the installer.

use std::collections::BTreeSet;
use std::io::IsTerminal;
use std::process::Command;

use anyhow::{bail, Context, Result};
use inquire::Select;
use serde::Deserialize;

/// The source passed to `skills add`. Points at the repo's user-facing
/// `skills/` subdirectory, not the repo root: `--skill '*'` installs every
/// `SKILL.md` the installer discovers, and the repo root also carries the
/// contributor-only dev skills under `.claude/skills/` (add-command,
/// run-tests, ...) which must never reach end users.
pub const SKILLS_SOURCE: &str = "coralogix/cx-cli/skills";

/// The source the installer records in its lock file for skills installed from
/// this repo. It stores the repo slug without the subdir, so already-installed
/// detection matches on this rather than [`SKILLS_SOURCE`].
const INSTALLED_SOURCE: &str = "coralogix/cx-cli";

/// The command a user can run by hand when cx fails the install.
const MANUAL_INSTALL_CMD: &str = "npx skills add coralogix/cx-cli/skills";

/// Where to browse the installed skills and read their contents. Shown in the
/// compact summary as a pointer for manually reviewing what was installed;
/// the installer's own verbose output (including its security-risk table) is
/// suppressed in the non-interactive run, and this page does not replace that
/// table — use `cx skills install --interactive` to see the installer's full
/// output.
const SKILLS_REVIEW_URL: &str = "https://skills.sh/coralogix/cx-cli";

/// Where the installer puts skills: the user's home (`~/`) or the project (`./`).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SkillsScope {
    Global,
    Local,
}

pub struct InstallOptions {
    /// Scope from `--global`/`--local`; `None` asks interactively.
    pub scope: Option<SkillsScope>,
    /// Agents passed through to the installer's `-a` (overrides auto-detect).
    pub agents: Vec<String>,
    /// When true (init), an already-installed detection skips the install
    /// (idempotent onboarding). When false (`cx skills install`), it
    /// reinstalls to update.
    pub skip_if_installed: bool,
}

/// What [`run_install`] did, so callers (init) can message accordingly
/// without probing the installer themselves.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum InstallOutcome {
    /// The installer ran (fresh install or reinstall-to-update).
    Installed,
    /// Skipped: cx skills already installed in the target scope. Only
    /// returned with [`InstallOptions::skip_if_installed`].
    AlreadyInstalled,
}

/// The pre-flight gate run_install evaluates before touching the installer,
/// kept pure so both failure branches are unit-testable without a terminal.
#[derive(Debug, PartialEq, Eq)]
enum Preflight {
    Fail(String),
    /// `scope` is `None` when it still needs to be asked interactively.
    Proceed {
        scope: Option<SkillsScope>,
    },
}

fn preflight(
    npx_available: bool,
    scope: Option<SkillsScope>,
    stdin_is_terminal: bool,
) -> Preflight {
    if !npx_available {
        return Preflight::Fail(
            "Node.js/npx is required to install the cx agent skills.\n\
             Install Node.js (https://nodejs.org) and rerun, or skip the skills install."
                .to_string(),
        );
    }
    if scope.is_none() && !stdin_is_terminal {
        return Preflight::Fail(
            "no skills install scope - pass --global or --local \
             (--global-skills/--local-skills on `cx init`)"
                .into(),
        );
    }
    Preflight::Proceed { scope }
}

/// Install with at most one question (scope), then a fully
/// non-interactive `npx skills add` run.
pub fn run_install(opts: InstallOptions) -> Result<InstallOutcome> {
    let scope = match preflight(npx_available(), opts.scope, std::io::stdin().is_terminal()) {
        Preflight::Fail(message) => bail!(message),
        Preflight::Proceed { scope } => match scope {
            Some(scope) => scope,
            None => ask_scope()?,
        },
    };

    // The single already-installed probe (npx spawns are slow — seconds on a
    // cold first run — so callers must not duplicate it). Detection is scoped
    // to the scope we're about to install into, so skills present in the
    // other scope don't muddy the result.
    let installed = installed_cx_skills(scope);
    if !installed.is_empty() {
        if opts.skip_if_installed {
            return Ok(InstallOutcome::AlreadyInstalled);
        }
        println!(
            "cx agent skills already installed ({}) - reinstalling to update.",
            installed_summary(&installed)
        );
    }

    let args = build_install_args(scope, &opts.agents);

    // The `-y` installer runs fully non-interactively, so it needs no terminal
    // and its verbose TUI (banner, per-skill table, risk matrix) is pure noise.
    // Capture it and replace it with a one-line summary; only surface the raw
    // output on failure, where the error detail matters. The user-driven
    // interactive walk (`run_advanced_install`) keeps full passthrough.
    println!("Installing cx agent skills…");

    let output = Command::new(npx_program())
        .args(&args)
        .output()
        .context("failed to run npx")?;
    if !output.status.success() {
        // Show the installer's own diagnostics before the retry hint. Node
        // CLIs report errors on either stream, so surface both.
        for captured in [&output.stdout, &output.stderr] {
            let text = String::from_utf8_lossy(captured);
            let trimmed = text.trim();
            if !trimmed.is_empty() {
                eprintln!("{trimmed}");
            }
        }
        bail!(
            "skills installer exited with {}.\n\
             You can retry manually with: {MANUAL_INSTALL_CMD}",
            output.status
        );
    }

    print_install_summary(scope);
    Ok(InstallOutcome::Installed)
}

/// The compact success line shown after a non-interactive install, in place of
/// the installer's verbose output. Points at the skill listing so the user can
/// review what was installed, since the installer's verbose output was hidden.
fn print_install_summary(scope: SkillsScope) {
    let scope_label = match scope {
        SkillsScope::Global => "global",
        SkillsScope::Local => "local",
    };
    // Re-query so the count reflects what's actually on disk now.
    let count = installed_cx_skills(scope).len();
    if count > 0 {
        println!(
            "✓ Installed {count} cx agent skills ({scope_label}). \
             Browse them at: {SKILLS_REVIEW_URL}"
        );
    } else {
        // Installer succeeded but detection came up empty (e.g. an older
        // installer without `--json`); still confirm and point to review.
        println!(
            "✓ cx agent skills installed ({scope_label}). \
             Browse them at: {SKILLS_REVIEW_URL}"
        );
    }
}

/// Advanced install: run the raw installer with no flags and let the user
/// walk its full interactive flow (skill/agent selection, scope, method).
pub fn run_advanced_install() -> Result<()> {
    println!("Running the skills installer interactively: {MANUAL_INSTALL_CMD}");
    let status = Command::new(npx_program())
        .args(["skills", "add", SKILLS_SOURCE])
        .status()
        .context(
            "failed to run npx - Node.js is required for the skills install \
             (https://nodejs.org)",
        )?;
    if !status.success() {
        bail!("skills installer exited with {status}");
    }
    Ok(())
}

// ── Building blocks ───────────────────────────────────────────────────────────

/// `npx` resolves through a `.cmd` shim on Windows; `Command` needs the
/// real file name there.
fn npx_program() -> &'static str {
    if cfg!(windows) {
        "npx.cmd"
    } else {
        "npx"
    }
}

fn npx_available() -> bool {
    Command::new(npx_program())
        .arg("--version")
        .output()
        .map(|out| out.status.success())
        .unwrap_or(false)
}

/// One entry of `skills ls --json` output; extra fields are ignored.
#[derive(Deserialize)]
struct LsSkill {
    name: String,
    #[serde(default)]
    source: Option<String>,
}

/// cx skills already installed in `scope`, according to the installer itself
/// (`npx skills ls [-g] --json`). A skill counts as ours when its lock-file
/// source is [`INSTALLED_SOURCE`]. Any failure (old installer without `--json`,
/// unparseable output) counts as nothing installed, so the install proceeds.
///
/// Copy installs (`--copy`) drop the lock-file source and so are not detected;
/// the installer's default is symlink, which preserves it.
fn installed_cx_skills(scope: SkillsScope) -> Vec<String> {
    let mut cmd = Command::new(npx_program());
    cmd.args(["-y", "skills", "ls"]);
    if scope == SkillsScope::Global {
        cmd.arg("-g");
    }
    cmd.arg("--json");
    let Ok(output) = cmd.output() else {
        return Vec::new();
    };
    if !output.status.success() {
        return Vec::new();
    }
    let Ok(skills) = serde_json::from_slice::<Vec<LsSkill>>(&output.stdout) else {
        return Vec::new();
    };
    skills
        .into_iter()
        .filter(|skill| skill.source.as_deref() == Some(INSTALLED_SOURCE))
        .map(|skill| skill.name)
        .collect::<BTreeSet<_>>()
        .into_iter()
        .collect()
}

/// Arguments for the fully non-interactive install.
///
/// The leading `-y` is npx's own auto-approve (first-run package download
/// prompt); the trailing `-y` is the installer's skip-all-confirmations.
fn build_install_args(scope: SkillsScope, agents: &[String]) -> Vec<String> {
    let mut args: Vec<String> = vec![
        "-y".into(),
        "skills".into(),
        "add".into(),
        SKILLS_SOURCE.into(),
        "--skill".into(),
        "*".into(),
        "-y".into(),
    ];
    if scope == SkillsScope::Global {
        args.push("-g".into());
    }
    if !agents.is_empty() {
        args.push("-a".into());
        args.extend(agents.iter().cloned());
    }
    args
}

/// e.g. "3 skills: cx-alerts, cx-dashboards, ..."
fn installed_summary(installed: &[String]) -> String {
    const SHOWN: usize = 3;
    let names = installed
        .iter()
        .take(SHOWN)
        .map(String::as_str)
        .collect::<Vec<_>>()
        .join(", ");
    let suffix = if installed.len() > SHOWN { ", ..." } else { "" };
    format!("{} skills: {names}{suffix}", installed.len())
}

fn ask_scope() -> Result<SkillsScope> {
    const GLOBAL: &str = "Global (~/) - available in every project";
    const LOCAL: &str = "Local (./) - this project only";
    let chosen = Select::new(
        "Where should the agent skills be installed?",
        vec![GLOBAL, LOCAL],
    )
    .with_help_message(
        "Skills teach coding agents (Claude Code, Cursor, Codex, ...) how to use cx.",
    )
    .prompt()?;
    Ok(if chosen == GLOBAL {
        SkillsScope::Global
    } else {
        SkillsScope::Local
    })
}

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

    #[test]
    fn install_args_local_scope() {
        let args = build_install_args(SkillsScope::Local, &[]);
        assert_eq!(
            args,
            ["-y", "skills", "add", SKILLS_SOURCE, "--skill", "*", "-y"]
        );
    }

    #[test]
    fn install_args_global_scope() {
        let args = build_install_args(SkillsScope::Global, &[]);
        assert_eq!(
            args,
            [
                "-y",
                "skills",
                "add",
                SKILLS_SOURCE,
                "--skill",
                "*",
                "-y",
                "-g"
            ]
        );
    }

    #[test]
    fn install_args_pass_agents_through() {
        let args = build_install_args(
            SkillsScope::Global,
            &["claude-code".to_string(), "cursor".to_string()],
        );
        assert_eq!(
            args,
            [
                "-y",
                "skills",
                "add",
                SKILLS_SOURCE,
                "--skill",
                "*",
                "-y",
                "-g",
                "-a",
                "claude-code",
                "cursor"
            ]
        );
    }

    #[test]
    fn summary_truncates_long_skill_lists() {
        let skills: Vec<String> = ["cx-a", "cx-b", "cx-c", "cx-d"]
            .iter()
            .map(|s| s.to_string())
            .collect();
        assert_eq!(
            installed_summary(&skills),
            "4 skills: cx-a, cx-b, cx-c, ..."
        );
        assert_eq!(installed_summary(&skills[..2]), "2 skills: cx-a, cx-b");
    }

    #[test]
    fn ls_json_matches_only_our_source_ignoring_extra_fields() {
        // Our skills carry the repo source; a same-named skill from another
        // source and a copy-installed skill (null source) are not ours.
        let json = r#"[
            {"name": "cx-alerts", "path": "/p", "scope": "global",
             "agents": ["Claude Code"], "source": "coralogix/cx-cli",
             "sourceUrl": null, "sourceType": "github"},
            {"name": "cx-other", "source": "someone/else"},
            {"name": "cx-copied", "source": null}
        ]"#;
        let skills: Vec<LsSkill> = serde_json::from_str(json).unwrap();
        let ours: Vec<&str> = skills
            .iter()
            .filter(|s| s.source.as_deref() == Some(INSTALLED_SOURCE))
            .map(|s| s.name.as_str())
            .collect();
        assert_eq!(ours, ["cx-alerts"]);
    }

    // ── preflight: both failure branches and the two proceed branches ─────────

    #[test]
    fn preflight_no_npx_fails_actionably() {
        let Preflight::Fail(message) = preflight(false, None, true) else {
            panic!("expected Fail");
        };
        assert!(message.contains("Node.js"));
    }

    #[test]
    fn preflight_no_scope_no_tty_fails_naming_the_flags() {
        let Preflight::Fail(message) = preflight(true, None, false) else {
            panic!("expected Fail");
        };
        assert!(message.contains("--global") && message.contains("--local"));
    }

    #[test]
    fn preflight_no_scope_with_tty_asks() {
        assert_eq!(
            preflight(true, None, true),
            Preflight::Proceed { scope: None }
        );
    }

    #[test]
    fn preflight_scope_flag_proceeds_without_asking() {
        assert_eq!(
            preflight(true, Some(SkillsScope::Local), false),
            Preflight::Proceed {
                scope: Some(SkillsScope::Local)
            }
        );
    }
}