spacetimedb-cli 0.1.1

A command line interface for SpacetimeDB
mod config;
mod subcommands;
mod util;
use crate::config::Config;
use anyhow;
use clap::ArgMatches;
use clap::Command;
use std::vec;
use subcommands::*;

// Postgres table output example
//  id  |         created_at         |              email               | email_is_verified | unsubscribed |         updated_at         | signed_pre_release_nda
// -----+----------------------------+----------------------------------+-------------------+--------------+----------------------------+------------------------
//    3 | 2021-09-14 16:15:15.588103 | alessandro@clockworklabs.io      | f                 | f            | 2022-02-02 15:30:19.954958 | t
//    1 | 2021-09-13 19:25:54.022595 | tyler@clockworklabs.io           | f                 | f            | 2022-02-02 15:27:20.231872 | f
//    4 | 2021-09-14 17:21:36.542987 | carterminshull@hotmail.com       | f                 | f            | 2022-02-02 15:27:20.231872 | f
//    5 | 2021-09-16 13:15:32.673589 | yoyeswell@gmail.com              | t                 | f            | 2022-02-02 15:27:20.231872 | f

#[tokio::main]
async fn main() -> Result<(), anyhow::Error> {
    let config = Config::load();
    // Save a default version to disk
    config.save();

    let (cmd, subcommand_args) = util::match_subcommand_or_exit(get_command());
    exec_subcommand(config, &cmd, &subcommand_args).await?;

    Ok(())
}

fn get_command() -> Command<'static> {
    Command::new("stdb")
        .args_conflicts_with_subcommands(true)
        .subcommand_required(true)
        .subcommands(get_subcommands())
        .help_template(
            "\
┌──────────────────────────────────────────────────────────┐
│ SpacetimeDB Command Line Tool                            │
│ Easily interact with a SpacetimeDB cluster               │
│                                                          │
│ Please give us feedback at:                              │
│ https://github.com/clockworklabs/SpacetimeDB/issues      │
└──────────────────────────────────────────────────────────┘
Usage:
{usage}

Options:
{options}

Commands:
{subcommands}
",
        )
}

fn get_subcommands() -> Vec<Command<'static>> {
    vec![
        version::cli(),
        init::cli(),
        update::cli(),
        rm::cli(),
        logs::cli(),
        call::cli(),
        describe::cli(),
        schema::cli(),
        identity::cli(),
        energy::cli(),
        metrics::cli(),
        sql::cli(),
        revert::cli(),
        name::cli(),
    ]
}

async fn exec_subcommand(config: Config, cmd: &str, args: &ArgMatches) -> Result<(), anyhow::Error> {
    match cmd {
        "version" => version::exec(config, args).await,
        "identity" => identity::exec(config, args).await,
        "call" => call::exec(config, args).await,
        "describe" => describe::exec(config, args).await,
        "schema" => schema::exec(config, args).await,
        "energy" => energy::exec(config, args).await,
        "init" => init::exec(config, args).await,
        "rm" => rm::exec(config, args).await,
        "logs" => logs::exec(config, args).await,
        "metrics" => metrics::exec(config, args).await,
        "sql" => sql::exec(config, args).await,
        "revert" => revert::exec(config, args).await,
        "update" => update::exec(config, args).await,
        "name" => name::exec(config, args).await,
        unknown => Err(anyhow::anyhow!("Invalid subcommand: {}", unknown)),
    }
}