use crate::commands;
use clap::{ArgAction, Parser, Subcommand, builder::BoolishValueParser};
use tracing::debug;
const LONG_ABOUT: &str = "\
CLI for building with Turnkey Verifiable Cloud.
Some commands accept multiple configuration input types.
Configuration values are resolved in this order, highest priority first:
1. Command-line flag (e.g. --app-id)
2. Environment variable (e.g. TVC_APP_ID)
3. Config file value (--config-file)
4. Built-in default
Special rules (exceptions to the order above):
--pivot-args replaces the config file's list entirely (does not append)
Debug-mode flags (--dangerous-deploy-debug-mode and
--dangerous-enable-debug-mode-deployments) are opt-in only: the flag
or its env var can turn debug mode ON, but its absence never turns OFF
a config file that enables it. To disable debug mode, set it false in
the config file (or omit it) and do not pass the flag.
Authentication:
Local: run `tvc login` once; commands then read ~/.config/turnkey/.
CI: set TVC_ORG_ID, TVC_API_KEY_PUBLIC, and TVC_API_KEY_PRIVATE
to authenticate without files. Env vars take precedence over local
config files. Setting some but not all three required vars will error.
Interactive behavior:
By default, commands may prompt when stdin is a TTY. Use --non-interactive
or set TVC_NON_INTERACTIVE=true to disable prompts and fail fast instead.";
#[derive(Debug, Parser)]
#[command(about = "CLI for building with Turnkey Verifiable Cloud", long_about = LONG_ABOUT)]
pub struct Cli {
#[arg(
long,
global = true,
env = "TVC_NON_INTERACTIVE",
action = ArgAction::SetTrue,
value_parser = BoolishValueParser::new()
)]
non_interactive: bool,
#[command(subcommand)]
command: Commands,
}
impl Cli {
pub async fn run() -> anyhow::Result<()> {
let args = Cli::parse();
debug!(
command = args.command.name(),
non_interactive = args.non_interactive,
"dispatching"
);
let non_interactive = args.non_interactive;
match args.command {
Commands::Deploy { command } => match command {
DeployCommands::Approve(args) => {
commands::deploy::approve::run(args, non_interactive).await
}
DeployCommands::GetStatus(args) => commands::deploy::get_status::run(args).await,
DeployCommands::ProvisioningDetails(args) => {
commands::deploy::provisioning_details::run(args).await
}
DeployCommands::PostShare(args) => commands::deploy::post_share::run(args).await,
DeployCommands::Status(args) => commands::deploy::status::run(args).await,
DeployCommands::Create(args) => {
commands::deploy::create::run(args, non_interactive).await
}
DeployCommands::Init(args) => {
commands::deploy::init::run(args, non_interactive).await
}
DeployCommands::DebugLogs(args) => commands::deploy::debug_logs::run(args).await,
DeployCommands::Delete(args) => commands::deploy::delete::run(args).await,
DeployCommands::Restore(args) => commands::deploy::restore::run(args).await,
},
Commands::App { command } => match command {
AppCommands::Status(args) => commands::app::status::run(args).await,
AppCommands::List(args) => commands::app::list::run(args).await,
AppCommands::Create(args) => {
commands::app::create::run(args, non_interactive).await
}
AppCommands::Init(args) => commands::app::init::run(args, non_interactive).await,
AppCommands::SetLiveDeploy(args) => commands::app::set_live_deploy::run(args).await,
AppCommands::Delete(args) => commands::app::delete::run(args).await,
},
Commands::Keys { command } => match command {
KeysCommands::GenerateQuorumKey(args) => {
commands::keys::generate_quorum_key::run(args).await
}
KeysCommands::InitQuorumKey(args) => {
commands::keys::init_quorum_key::run(args).await
}
KeysCommands::ReEncryptShare(args) => {
commands::keys::re_encrypt_share::run(args).await
}
},
Commands::Login(args) => commands::login::run(args, non_interactive).await,
}
}
}
#[derive(Debug, Subcommand)]
enum Commands {
Login(commands::login::Args),
Deploy {
#[command(subcommand)]
command: DeployCommands,
},
App {
#[command(subcommand)]
command: AppCommands,
},
Keys {
#[command(subcommand)]
command: KeysCommands,
},
}
impl Commands {
fn name(&self) -> &'static str {
match self {
Commands::Login(_) => "login",
Commands::Deploy { command } => command.name(),
Commands::App { command } => command.name(),
Commands::Keys { command } => command.name(),
}
}
}
#[derive(Debug, Subcommand)]
enum DeployCommands {
Approve(commands::deploy::approve::Args),
GetStatus(commands::deploy::get_status::Args),
ProvisioningDetails(commands::deploy::provisioning_details::Args),
PostShare(commands::deploy::post_share::Args),
Status(commands::deploy::status::Args),
#[command(
long_about = commands::deploy::create::LONG_ABOUT,
after_help = commands::deploy::PORT_GUIDANCE
)]
Create(commands::deploy::create::Args),
Init(commands::deploy::init::Args),
#[command(long_about = commands::deploy::debug_logs::LONG_ABOUT)]
DebugLogs(commands::deploy::debug_logs::Args),
Delete(commands::deploy::delete::Args),
Restore(commands::deploy::restore::Args),
}
impl DeployCommands {
fn name(&self) -> &'static str {
match self {
DeployCommands::Approve(_) => "deploy approve",
DeployCommands::GetStatus(_) => "deploy get-status",
DeployCommands::ProvisioningDetails(_) => "deploy provisioning-details",
DeployCommands::PostShare(_) => "deploy post-share",
DeployCommands::Status(_) => "deploy status",
DeployCommands::Create(_) => "deploy create",
DeployCommands::Init(_) => "deploy init",
DeployCommands::DebugLogs(_) => "deploy debug-logs",
DeployCommands::Delete(_) => "deploy delete",
DeployCommands::Restore(_) => "deploy restore",
}
}
}
#[derive(Debug, Subcommand)]
enum AppCommands {
Status(commands::app::status::Args),
List(commands::app::list::Args),
Create(commands::app::create::Args),
Init(commands::app::init::Args),
SetLiveDeploy(commands::app::set_live_deploy::Args),
Delete(commands::app::delete::Args),
}
#[derive(Debug, Subcommand)]
enum KeysCommands {
GenerateQuorumKey(commands::keys::generate_quorum_key::Args),
InitQuorumKey(commands::keys::init_quorum_key::Args),
ReEncryptShare(commands::keys::re_encrypt_share::Args),
}
impl AppCommands {
fn name(&self) -> &'static str {
match self {
AppCommands::Status(_) => "app status",
AppCommands::List(_) => "app list",
AppCommands::Create(_) => "app create",
AppCommands::Init(_) => "app init",
AppCommands::SetLiveDeploy(_) => "app set-live-deploy",
AppCommands::Delete(_) => "app delete",
}
}
}
impl KeysCommands {
fn name(&self) -> &'static str {
match self {
KeysCommands::GenerateQuorumKey(_) => "keys generate-quorum-key",
KeysCommands::InitQuorumKey(_) => "keys init-quorum-key",
KeysCommands::ReEncryptShare(_) => "keys re-encrypt-share",
}
}
}