coralogix-cli 0.1.18

Coralogix CLI - the observability backbone for AI agents and engineering teams.
Documentation
//! `cx init`: the single guided entry point for onboarding.
//!
//! A thin alias that chains `cx profiles add` and `cx skills install` in order,
//! ending in a success message. It holds no independent profile or skill logic
//! of its own — each step delegates to the command that owns it.
//!
//! **Idempotent by design:** if any profile already exists, the profile step is
//! skipped; if the cx skills are already installed, the skills step is skipped
//! too. Re-running `cx init` therefore never re-prompts, clobbers, or reinstalls
//! an existing setup. To reconfigure a profile use `cx profiles add --force`; to
//! update the skills use `cx skills install`.
//!
//! The profile step only runs on a fresh machine (no profiles yet), so it always
//! creates the *first* profile — which means no name prompt and no "set as
//! default?" question. Two modes, chosen automatically (never via a flag), like
//! `cx profiles add`:
//!
//! * **Quick (interactive)** — bare `cx init` on a terminal. Guided prompts:
//!   OAuth browser login (no auth-method question), a region prompt, and the
//!   first-profile safety questions. Credential storage, output format, and
//!   label are defaulted (file / JSON / none) rather than asked.
//! * **Advanced (non-interactive)** — the setup is fully specified by flags (or
//!   there is no terminal). The flags make the *profile* step prompt-free; the
//!   skills step still asks its scope question unless `--global`/`--local` (or
//!   `--no-skills`) answers it — so the fully prompt-free coding-agent
//!   one-liner is: `cx init --url … --api-key $CX_API_KEY --global`. Without a
//!   scope flag and with no terminal, the skills step is skipped with a
//!   warning.

use anyhow::Result;

use crate::commands::{profiles, skills};
use crate::config::list_profile_names;

/// Name of the profile init creates on a fresh machine.
const DEFAULT_PROFILE_NAME: &str = "default";

/// Resolved arguments for `cx init`. The credential values come from the
/// shared global flags (`--api-key`, `--region`); the rest are `init`-local
/// flags.
pub struct InitArgs {
    /// `--url`: Coralogix URL the region is derived from.
    pub url: Option<String>,
    /// `--region`: region short-name, alternative to `--url`.
    pub region: Option<String>,
    /// `--api-key` / `CX_API_KEY`.
    pub api_key: Option<String>,
    /// `--oauth`: force OAuth browser login even when an API key is available.
    pub oauth: bool,
    /// Whether to run the skills install step (`--no-skills` turns it off).
    pub install_skills: bool,
    /// `--agent`: agents passed through to the skills installer.
    pub agents: Vec<String>,
    /// Install scope for skills (`--global` / `--local`); `None` asks (quick)
    /// or skips the step (non-interactive).
    pub scope: Option<skills::SkillsScope>,
}

/// Orchestrate the onboarding chain: profile → skills → success.
pub async fn run_init(args: InitArgs) -> Result<()> {
    let InitArgs {
        url,
        region,
        api_key,
        oauth,
        install_skills,
        agents,
        scope,
    } = args;

    // ── Step 1: profile ─────────────────────────────────────────────────────────
    // Idempotent: skip profile setup when the user already has one. A fresh run
    // creates the first (and thus default) profile — no name / set-default
    // prompts to worry about.
    if list_profile_names()?.is_empty() {
        // Auth: quick mode (bare `cx init`, no key) defaults to OAuth without
        // asking the auth-method question; an explicit key uses the API-key
        // path; `--oauth` forces OAuth regardless.
        let oauth = oauth || api_key.is_none();

        println!("Setting up your Coralogix profile...\n");
        profiles::run_add(profiles::AddArgs {
            name: Some(DEFAULT_PROFILE_NAME.to_string()),
            url,
            region,
            api_key,
            oauth,
            force: false,
            set_default: false,
            quick: true,
        })
        .await?;
    } else {
        println!("A Coralogix profile is already configured - skipping profile setup.");
    }

    // ── Step 2: skills ──────────────────────────────────────────────────────────
    // Idempotent, like the profile step: if cx skills are already installed,
    // skip rather than reinstall (`skip_if_installed`). Updating is the
    // explicit command's job (`cx skills install` reinstalls to pull the
    // latest). Detection lives inside `run_install` — its single probe — and
    // init only branches on the reported outcome, so the slow npx spawns are
    // never duplicated on the first-run path.
    //
    // The skills step must never brick onboarding: `skills::run_install` owns
    // the npx/scope diagnostics and fails hard on a missing prerequisite; init
    // downgrades that to a warning and continues, so a working profile still
    // counts as a successful setup.
    if install_skills {
        println!("\nInstalling the cx agent skills for coding agents...");
        match skills::run_install(skills::InstallOptions {
            scope,
            agents,
            skip_if_installed: true,
        }) {
            Ok(skills::InstallOutcome::Installed) => {}
            Ok(skills::InstallOutcome::AlreadyInstalled) => {
                println!(
                    "cx agent skills are already installed - skipping.\n\
                     Update them anytime with `cx skills install`."
                );
            }
            Err(error) => eprintln!("warning: skipped the agent-skills install: {error:#}"),
        }
    }

    // ── Done ────────────────────────────────────────────────────────────────────
    print_success();
    Ok(())
}

fn print_success() {
    println!("\ncx is ready to go.");
    println!("Try it out:");
    println!("  cx logs 'source logs | limit 10'");
    println!("  cx schema        # discover every command as JSON");
}