use std::path::PathBuf;
use std::process::ExitCode;
use clap::{Args, Parser, Subcommand};
use stratum_node_cli::{
exit, ApplyOptions, Confirmation, LoopbackUrl, Output, OutputFormat, Session, SetupOptions,
DEFAULT_URL,
};
use stratum_node_core::DidNostr;
const EXIT_CODES: &str = "\
Exit codes:
0 success; for `status`, the node is ready
1 local failure (key, token or resume file, clock, output)
2 the node could not be reached
3 status: the node is unconfigured
4 status: the node is degraded
5 the node's response does not follow the API contract
6 previewed but not applied: no confirmation (pass --yes)
7 a local check refused the node's answer (claim body, backup manifest)
10 bad_request 11 unauthorized 12 forbidden
13 not_found 14 conflict 15 payload_too_large
16 unsupported_media_type 17 invalid (also an invalid preview)
18 too_many_challenges 19 backup_refused 20 internal
21 unknown server error code
64 invalid command line
Server errors are printed as `code: message`.";
#[derive(Debug, Parser)]
#[command(name = "stratum", version, after_help = EXIT_CODES)]
struct Cli {
#[arg(long, global = true, env = "STRATUM_URL", default_value = DEFAULT_URL)]
url: LoopbackUrl,
#[arg(long, global = true, env = "STRATUM_KEY_FILE", value_name = "FILE")]
key_file: Option<PathBuf>,
#[arg(long, global = true)]
json: bool,
#[command(subcommand)]
command: Command,
}
#[derive(Debug, Subcommand)]
enum Command {
Key {
#[command(subcommand)]
command: KeyCommand,
},
Status,
Config {
#[command(subcommand)]
command: ConfigCommand,
},
Setup(SetupArgs),
SignChallenge {
#[arg(long, value_name = "FILE")]
claim_body_file: PathBuf,
#[arg(long, value_name = "DID")]
expect_node: DidNostr,
},
Audit {
#[command(subcommand)]
command: AuditCommand,
},
Backup {
#[arg(long, value_name = "ID")]
operation_id: Option<String>,
#[arg(long, value_name = "DID")]
expect_node: Option<DidNostr>,
},
}
#[derive(Debug, Subcommand)]
enum KeyCommand {
Generate {
#[arg(long, value_name = "FILE")]
out: PathBuf,
},
Show,
}
#[derive(Debug, Subcommand)]
enum ConfigCommand {
Schema,
Show,
Preview(PreviewArgs),
Apply(ApplyArgs),
}
#[derive(Debug, Subcommand)]
enum AuditCommand {
Checkpoint,
}
#[derive(Debug, Args)]
struct ChangeArgs {
#[arg(long = "set", value_name = "PATH=VALUE")]
sets: Vec<String>,
#[arg(long, value_name = "N")]
base_revision: Option<u64>,
}
#[derive(Debug, Args)]
struct PreviewArgs {
#[command(flatten)]
changes: ChangeArgs,
}
#[derive(Debug, Args)]
struct ApplyArgs {
#[command(flatten)]
changes: ChangeArgs,
#[arg(long, value_name = "ID")]
operation_id: Option<String>,
#[arg(long, short = 'y')]
yes: bool,
}
#[derive(Debug, Args)]
struct SetupArgs {
#[arg(long, value_name = "FILE")]
token_file: PathBuf,
#[arg(long = "set", value_name = "PATH=VALUE")]
sets: Vec<String>,
#[arg(long, value_name = "DID")]
expect_node: Option<DidNostr>,
#[arg(long)]
show_text: bool,
#[arg(long, value_name = "FILE")]
resume_file: Option<PathBuf>,
#[arg(long, short = 'y')]
yes: bool,
}
trait YesFlag {
fn yes(&self) -> bool;
fn confirmation(&self) -> Confirmation {
if self.yes() {
Confirmation::Assumed
} else {
Confirmation::Prompt
}
}
}
impl YesFlag for ApplyArgs {
fn yes(&self) -> bool {
self.yes
}
}
impl YesFlag for SetupArgs {
fn yes(&self) -> bool {
self.yes
}
}
fn main() -> ExitCode {
let cli = match Cli::try_parse() {
Ok(cli) => cli,
Err(err) => {
let code = if err.use_stderr() {
exit::USAGE
} else {
exit::OK
};
let _printed = err.print();
return ExitCode::from(code);
}
};
let output = Output::new(if cli.json {
OutputFormat::Json
} else {
OutputFormat::Human
});
let session = Session::new(cli.url, cli.key_file, output);
let result = match cli.command {
Command::Key {
command: KeyCommand::Generate { out },
} => session.key_generate(&out),
Command::Key {
command: KeyCommand::Show,
} => session.key_show(),
Command::Status => session.status(),
Command::Config { command } => match command {
ConfigCommand::Schema => session.config_schema(),
ConfigCommand::Show => session.config_show(),
ConfigCommand::Preview(args) => {
session.config_preview(&args.changes.sets, args.changes.base_revision)
}
ConfigCommand::Apply(args) => session.config_apply(&ApplyOptions {
confirmation: args.confirmation(),
sets: args.changes.sets,
base_revision: args.changes.base_revision,
operation_id: args.operation_id,
}),
},
Command::Setup(args) => session.setup(&SetupOptions {
confirmation: args.confirmation(),
token_file: args.token_file,
sets: args.sets,
resume_file: args.resume_file,
expect_node: args.expect_node,
show_text: args.show_text,
}),
Command::SignChallenge {
claim_body_file,
expect_node,
} => session.sign_challenge(&claim_body_file, &expect_node),
Command::Audit {
command: AuditCommand::Checkpoint,
} => session.audit_checkpoint(),
Command::Backup {
operation_id,
expect_node,
} => session.backup(operation_id.as_deref(), expect_node.as_ref()),
};
match result {
Ok(code) => ExitCode::from(code),
Err(err) => {
output.error(&err);
ExitCode::from(err.exit_code())
}
}
}