kayro-cli 0.1.0

Create, build, and run Kayro desktop apps for humans and agents
Documentation
mod args;
mod build;
mod cargo_command;
mod cargo_progress;
mod dev;
mod error;
mod output;
mod packaging;
mod process;
mod project;
mod scaffold;
mod target;
mod vite_path;

use std::{env, ffi::OsStr, process::ExitCode};

use clap::{Parser as _, error::ErrorKind};

use crate::{
    args::{Cli, KayroCommand, ProjectOptions},
    output::Reporter,
    process::Cancellation,
    project::Project,
};

pub fn entry() -> ExitCode {
    let arguments = env::args_os().collect::<Vec<_>>();
    let json_requested = arguments
        .iter()
        .any(|argument| argument == OsStr::new("--json"));
    let cli = match Cli::try_parse_from(arguments) {
        Ok(cli) => cli,
        Err(error) if json_requested && error.kind() == ErrorKind::DisplayHelp => {
            Reporter::clap_display("help", &error.to_string());
            return ExitCode::SUCCESS;
        }
        Err(error) if json_requested && error.kind() == ErrorKind::DisplayVersion => {
            Reporter::clap_display("version", error.to_string().trim());
            return ExitCode::SUCCESS;
        }
        Err(error)
            if matches!(
                error.kind(),
                ErrorKind::DisplayHelp | ErrorKind::DisplayVersion
            ) =>
        {
            let _ = error.print();
            return ExitCode::SUCCESS;
        }
        Err(error) if json_requested => {
            Reporter::argument_error(&error.to_string());
            return ExitCode::from(2);
        }
        Err(error) => {
            let code = u8::try_from(error.exit_code()).unwrap_or(2);
            let _ = error.print();
            return ExitCode::from(code);
        }
    };

    let reporter = Reporter::new(cli.json, cli.quiet, cli.verbose, cli.color);
    reporter.begin(command_name(&cli.command), cli.dry_run);
    let result = run(&cli, &reporter);
    match result {
        Ok(command) => {
            reporter.completed(command, cli.dry_run);
            ExitCode::SUCCESS
        }
        Err(error) => {
            reporter.error(&error);
            ExitCode::from(error.exit_code())
        }
    }
}

fn command_name(command: &KayroCommand) -> &'static str {
    match command {
        KayroCommand::New(_) => "new",
        KayroCommand::Init(_) => "init",
        KayroCommand::Dev(_) => "dev",
        KayroCommand::Build(_) => "build",
    }
}

fn run<'a>(cli: &'a Cli, reporter: &Reporter) -> Result<&'a str, error::CliError> {
    let cancellation = if cli.dry_run {
        Cancellation::default()
    } else {
        Cancellation::install()?
    };

    match &cli.command {
        KayroCommand::New(arguments) => {
            scaffold::create_new(arguments, reporter, &cancellation, cli.dry_run)?;
            Ok("new")
        }
        KayroCommand::Init(arguments) => {
            scaffold::initialize(arguments, reporter, &cancellation, cli.dry_run)?;
            Ok("init")
        }
        KayroCommand::Dev(arguments) => {
            let project = discover_project(&arguments.project, reporter)?;
            dev::run(&project, arguments, reporter, &cancellation, cli.dry_run)?;
            Ok("dev")
        }
        KayroCommand::Build(arguments) => {
            let project = discover_project(&arguments.project, reporter)?;
            build::run(&project, arguments, reporter, &cancellation, cli.dry_run)?;
            Ok("build")
        }
    }
}

fn discover_project(
    options: &ProjectOptions,
    reporter: &Reporter,
) -> Result<Project, error::CliError> {
    let project = Project::discover(options.config.as_deref(), options.project_dir.as_deref())?;
    reporter.project(&project);
    Ok(project)
}