executesoft 0.1.6

ExecuteSoft repository automation CLI
use crate::cli::{Cli, Commands, DeployCommand};
use crate::db::run_db_command;
use crate::dev::run_dev;
use crate::gateway::run_gateway_command;
use crate::service::run_service_command;
use crate::service_local::setup_current_service;
use crate::util::{Result, make_vars, repo_root, run_cmd, run_make};

pub(crate) fn run_cli(cli: Cli) -> Result<()> {
    match cli.command {
        Commands::Service { command } => run_service_command(command),
        Commands::Gateway { command } => run_gateway_command(command),
        Commands::Db { command } => run_db_command(command),
        Commands::Setup(args) => setup_current_service(&args.vars),
        Commands::Dev { command } => run_dev(command),
        Commands::Sync => run_sync(),
        Commands::Release(args) => run_release(&args.vars),
        Commands::ReleaseSync(args) => run_release_sync(&args.vars),
        Commands::Deploy { command } => run_deploy_command(command),
    }
}

fn run_deploy_command(command: DeployCommand) -> Result<()> {
    match command {
        DeployCommand::Kubernetes(args) => {
            run_sync()?;
            run_make(
                &repo_root().join("devops"),
                "deploy-kubernetes",
                &make_vars(&args.vars),
            )
        }
        DeployCommand::Docker(args) => run_make(
            &repo_root().join("devops"),
            "deploy-docker",
            &make_vars(&args.vars),
        ),
        DeployCommand::Migrations(args) => run_make(
            &repo_root().join("devops"),
            "apply-migrations",
            &make_vars(&args.vars),
        ),
        DeployCommand::ObservabilityUp(args) => run_make(
            &repo_root().join("devops"),
            "observability-full-up",
            &make_vars(&args.vars),
        ),
        DeployCommand::ObservabilityDown(args) => run_make(
            &repo_root().join("devops"),
            "observability-full-down",
            &make_vars(&args.vars),
        ),
        DeployCommand::BackupNow(args) => run_make(
            &repo_root().join("devops"),
            "backup-now",
            &make_vars(&args.vars),
        ),
        DeployCommand::InstallBackupCron(args) => run_make(
            &repo_root().join("devops"),
            "install-backup-cron",
            &make_vars(&args.vars),
        ),
        DeployCommand::ReleaseSync(args) => run_release_sync(&args.vars),
        DeployCommand::ReloadCaddy(args) => run_make(
            &repo_root().join("devops"),
            "reload-caddy",
            &make_vars(&args.vars),
        ),
        DeployCommand::RestartCdn(args) => run_make(
            &repo_root().join("devops"),
            "restart-cdn",
            &make_vars(&args.vars),
        ),
        DeployCommand::Recreate(args) => run_make(
            &repo_root().join("devops"),
            "recreate",
            &make_vars(&args.vars),
        ),
    }
}

fn run_sync() -> Result<()> {
    run_cmd(
        &repo_root(),
        "devops/droplet/scripts/sync-service-assets.sh",
        &[],
    )
}

fn run_release(args: &[String]) -> Result<()> {
    run_make(&repo_root().join("devops"), "release", &make_vars(args))
}

fn run_release_sync(args: &[String]) -> Result<()> {
    run_make(
        &repo_root().join("devops"),
        "release-sync",
        &make_vars(args),
    )
}