partiri-cli 0.1.5

partiri CLI — Deploy and manage services on Partiri Cloud
use clap::Parser;

mod cli;
mod client;
mod config;
mod error;
mod modules;
mod output;

use cli::{Cli, Commands, ProjectCommands, ServiceCommands, WorkspaceCommands};
use client::ApiClient;
use config::PartiriConfig;

fn main() {
    let cli = Cli::parse();
    if let Err(e) = run(cli) {
        output::print_error(&*e);
        std::process::exit(1);
    }
}

fn run(cli: Cli) -> error::Result<()> {
    match cli.command {
        Commands::Auth => {
            modules::auth::run()?;
        }

        Commands::Init => {
            modules::init::run()?;
        }

        Commands::Validate => {
            let config = PartiriConfig::load()?;
            modules::validate::run(&config)?;
        }

        // Pull does not require an existing config file
        Commands::Service(ServiceCommands::Pull) => {
            let client = ApiClient::new()?;
            modules::service::pull::run(&client)?;
        }

        Commands::Service(cmd) => {
            let client = ApiClient::new()?;
            let config = PartiriConfig::load()?;
            match cmd {
                ServiceCommands::Create => modules::service::create::run(&client, config)?,
                ServiceCommands::Push => modules::service::push::run(&client, &config)?,
                ServiceCommands::Metrics => modules::service::status::run(&client, &config)?,
                ServiceCommands::Logs => modules::service::logs::run(&client, &config)?,
                ServiceCommands::Jobs => modules::jobs::run_list(&client, &config)?,
                ServiceCommands::Deploy => modules::service::deploy::run(&client, &config)?,
                ServiceCommands::Pause => modules::service::pause::run(&client, &config)?,
                ServiceCommands::Unpause => modules::service::unpause::run(&client, &config)?,
                ServiceCommands::Kill => modules::service::kill::run(&client, &config)?,
                ServiceCommands::Token => modules::service::token::run(&client, config)?,
                ServiceCommands::Pull => unreachable!(),
            }
        }

        Commands::Projects(ProjectCommands::List) => {
            let client = ApiClient::new()?;
            modules::projects::run_list(&client)?;
        }

        Commands::Projects(ProjectCommands::Create) => {
            let client = ApiClient::new()?;
            modules::projects::run_create(&client)?;
        }

        Commands::Workspaces(WorkspaceCommands::List) => {
            let client = ApiClient::new()?;
            modules::workspaces::run_list(&client)?;
        }
    }

    Ok(())
}