vade-cli 0.1.1

A command-line tool to deploy applications on Linux servers
mod app_deployment;
mod app_name;
mod cli;
mod commands;
mod config;
mod templating;
mod util;

use crate::cli::ServerSetupCommand;
use crate::commands::{destroy, server_setup};
use crate::util::RelativePathResolver;
use app_deployment::AppDeployment;
use clap::Parser;
use cli::{Cli, Command, DeployCommand};
use commands::{create, deploy};
use miette::Report;

fn main() -> Result<(), Report> {
    let cli = Cli::parse();
    match cli.command {
        Command::ServerSetup(cmd) => server_setup(cmd),
        Command::Create(cmd) => create::execute(&cmd.app_name, &cmd.out_dir),
        Command::Destroy(cmd) => destroy::execute(&cmd.app_name, &cmd.out_dir),
        Command::Deploy(cmd) => deploy(cmd),
    }
}

fn server_setup(command: ServerSetupCommand) -> Result<(), Report> {
    server_setup::ServerSetup {
        out_dir: command.out_dir,
    }
    .execute()
}

fn deploy(command: DeployCommand) -> Result<(), Report> {
    let uses_default_config_path = command.configuration_file.is_none();
    let config_path = command.configuration_file.unwrap_or("vade.toml".into());
    let (config, config_src) =
        config::load(&command.app_name, &config_path, uses_default_config_path)?;

    // safety: we know that config_path is a file, hence its path always has a parent
    let config_parent_path = config_path
        .canonicalize()
        .unwrap()
        .parent()
        .unwrap()
        .to_owned();
    let path_resolver = RelativePathResolver::with_root(config_parent_path);

    let deployment = AppDeployment::from_config(
        &command.app_name,
        config,
        &config_src,
        &command.set_json,
        &path_resolver,
    )?;
    deploy::execute(command.app_name, deployment, command.out_dir, &config_src)
}