Save to JSON in addition to printing (#1864)

* Save to JSON in addition to printing

* Save node details to json for mixnode

* Remove Cargo.locks

* Cli ergonomics

* Json output for gateway
This commit is contained in:
Drazen Urch
2023-01-09 12:51:21 +01:00
committed by GitHub
parent 88d813b9c1
commit 4a8a9096dd
22 changed files with 237 additions and 14536 deletions
+8 -8
View File
@@ -4,6 +4,7 @@
use super::OverrideConfig;
use crate::config::Config;
use crate::node::MixNode;
use crate::OutputFormat;
use crate::{commands::override_config, config::persistence::pathfinder::MixNodePathfinder};
use clap::Args;
use config::NymConfig;
@@ -62,13 +63,13 @@ impl From<Init> for OverrideConfig {
}
}
pub(crate) fn execute(args: &Init) {
pub(crate) fn execute(args: &Init, output: OutputFormat) {
let override_config_fields = OverrideConfig::from(args.clone());
let id = &override_config_fields.id;
println!("Initialising mixnode {}...", id);
eprintln!("Initialising mixnode {}...", id);
let already_init = if Config::default_config_file_path(Some(id)).exists() {
println!("Mixnode \"{}\" was already initialised before! Config information will be overwritten (but keys will be kept)!", id);
eprintln!("Mixnode \"{}\" was already initialised before! Config information will be overwritten (but keys will be kept)!", id);
true
} else {
false
@@ -101,16 +102,15 @@ pub(crate) fn execute(args: &Init) {
),
)
.expect("Failed to save sphinx keys");
println!("Saved mixnet identity and sphinx keypairs");
eprintln!("Saved mixnet identity and sphinx keypairs");
}
let config_save_location = config.get_config_file_save_location();
config
.save_to_file(None)
.expect("Failed to save the config file");
println!("Saved configuration file to {:?}", config_save_location);
println!("Mixnode configuration completed.\n\n\n");
eprintln!("Saved configuration file to {:?}", config_save_location);
eprintln!("Mixnode configuration completed.\n\n\n");
MixNode::new(config).print_node_details()
MixNode::new(config).print_node_details(output)
}
+9 -7
View File
@@ -62,13 +62,15 @@ struct OverrideConfig {
pub(crate) async fn execute(args: Cli) {
let bin_name = "nym-mixnode";
let output = args.output();
match args.command {
Commands::Describe(m) => describe::execute(m),
Commands::Init(m) => init::execute(&m),
Commands::Run(m) => run::execute(&m).await,
Commands::Init(m) => init::execute(&m, output),
Commands::Run(m) => run::execute(&m, output).await,
Commands::Sign(m) => sign::execute(&m),
Commands::Upgrade(m) => upgrade::execute(&m),
Commands::NodeDetails(m) => node_details::execute(&m),
Commands::NodeDetails(m) => node_details::execute(&m, output),
Commands::Completions(s) => s.generate(&mut crate::Cli::command(), bin_name),
Commands::GenerateFigSpec => fig_generate(&mut crate::Cli::command(), bin_name),
}
@@ -115,8 +117,8 @@ pub(crate) fn validate_bech32_address_or_exit(address: &str) {
bech32_address_validation::try_bech32_decode(address)
{
let error_message = format!("Error: wallet address decoding failed: {err}").red();
println!("{}", error_message);
println!("Exiting...");
error!("{}", error_message);
error!("Exiting...");
process::exit(1);
}
@@ -124,8 +126,8 @@ pub(crate) fn validate_bech32_address_or_exit(address: &str) {
bech32_address_validation::validate_bech32_prefix(&prefix, address)
{
let error_message = format!("Error: wallet address type is wrong, {err}").red();
println!("{}", error_message);
println!("Exiting...");
error!("{}", error_message);
error!("Exiting...");
process::exit(1);
}
}
+3 -2
View File
@@ -3,6 +3,7 @@
use crate::config::Config;
use crate::node::MixNode;
use crate::OutputFormat;
use clap::Args;
use config::NymConfig;
@@ -13,7 +14,7 @@ pub(crate) struct NodeDetails {
id: String,
}
pub(crate) fn execute(args: &NodeDetails) {
pub(crate) fn execute(args: &NodeDetails, output: OutputFormat) {
let config = match Config::load_from_file(Some(&args.id)) {
Ok(cfg) => cfg,
Err(err) => {
@@ -26,5 +27,5 @@ pub(crate) fn execute(args: &NodeDetails) {
}
};
MixNode::new(config).print_node_details()
MixNode::new(config).print_node_details(output)
}
+5 -4
View File
@@ -5,6 +5,7 @@ use super::OverrideConfig;
use crate::commands::{override_config, version_check};
use crate::config::Config;
use crate::node::MixNode;
use crate::OutputFormat;
use clap::Args;
use config::NymConfig;
use std::net::IpAddr;
@@ -76,8 +77,8 @@ fn special_addresses() -> Vec<&'static str> {
vec!["localhost", "127.0.0.1", "0.0.0.0", "::1", "[::1]"]
}
pub(crate) async fn execute(args: &Run) {
println!("Starting mixnode {}...", args.id);
pub(crate) async fn execute(args: &Run, output: OutputFormat) {
eprintln!("Starting mixnode {}...", args.id);
let mut config = match Config::load_from_file(Some(&args.id)) {
Ok(cfg) => cfg,
@@ -104,10 +105,10 @@ pub(crate) async fn execute(args: &Run) {
let mut mixnode = MixNode::new(config);
println!(
eprintln!(
"\nTo bond your mixnode you will need to install the Nym wallet, go to https://nymtech.net/get-involved and select the Download button.\n\
Select the correct version and install it to your machine. You will need to provide the following: \n ");
mixnode.print_node_details();
mixnode.print_node_details(output);
mixnode.run().await
}
+29 -2
View File
@@ -6,7 +6,7 @@ extern crate rocket;
use ::config::defaults::setup_env;
use build_information::BinaryBuildInformation;
use clap::{crate_version, Parser};
use clap::{crate_version, Parser, ValueEnum};
use lazy_static::lazy_static;
use logging::setup_logging;
@@ -24,6 +24,18 @@ fn pretty_build_info_static() -> &'static str {
&PRETTY_BUILD_INFORMATION
}
#[derive(Clone, ValueEnum)]
enum OutputFormat {
Json,
Text,
}
impl Default for OutputFormat {
fn default() -> Self {
OutputFormat::Text
}
}
#[derive(Parser)]
#[clap(author = "Nymtech", version, about, long_version = pretty_build_info_static())]
struct Cli {
@@ -31,14 +43,29 @@ struct Cli {
#[clap(short, long)]
pub(crate) config_env_file: Option<std::path::PathBuf>,
#[clap(short, long)]
pub(crate) output: Option<OutputFormat>,
#[clap(subcommand)]
command: commands::Commands,
}
impl Cli {
fn output(&self) -> OutputFormat {
if let Some(ref output) = self.output {
output.clone()
} else {
OutputFormat::default()
}
}
}
#[tokio::main]
async fn main() {
setup_logging();
println!("{}", banner());
if atty::is(atty::Stream::Stdout) {
println!("{}", banner());
}
let args = Cli::parse();
setup_env(args.config_env_file.as_ref());
+23 -29
View File
@@ -17,6 +17,7 @@ use crate::node::listener::Listener;
use crate::node::node_description::NodeDescription;
use crate::node::node_statistics::SharedNodeStats;
use crate::node::packet_delayforwarder::{DelayForwarder, PacketDelayForwardSender};
use crate::OutputFormat;
use ::crypto::asymmetric::{encryption, identity};
use colored::Colorize;
use config::NymConfig;
@@ -101,35 +102,28 @@ impl MixNode {
}
/// Prints relevant node details to the console
pub(crate) fn print_node_details(&self) {
println!(
"Identity Key: {}",
self.identity_keypair.public_key().to_base58_string()
);
println!(
"Sphinx Key: {}",
self.sphinx_keypair.public_key().to_base58_string()
);
println!("Owner Signature: {}", self.generate_owner_signature());
println!(
"Host: {} (bind address: {})",
self.config.get_announce_address(),
self.config.get_listening_address()
);
println!("Version: {}", self.config.get_version());
println!(
"Mix Port: {}, Verloc port: {}, Http Port: {}\n",
self.config.get_mix_port(),
self.config.get_verloc_port(),
self.config.get_http_api_port()
);
println!(
"You are bonding to wallet address: {}\n\n",
self.config
.get_wallet_address()
.map(|addr| addr.to_string())
.unwrap_or_else(|| "UNSPECIFIED".to_string())
);
pub(crate) fn print_node_details(&self, output: OutputFormat) {
let node_details = nym_types::mixnode::MixnodeNodeDetailsResponse {
identity_key: self.identity_keypair.public_key().to_base58_string(),
sphinx_key: self.sphinx_keypair.public_key().to_base58_string(),
owner_signature: self.generate_owner_signature(),
announce_address: self.config.get_announce_address(),
bind_address: self.config.get_listening_address().to_string(),
version: self.config.get_version().to_string(),
mix_port: self.config.get_mix_port(),
http_api_port: self.config.get_http_api_port(),
verloc_port: self.config.get_verloc_port(),
wallet_address: self.config.get_wallet_address().map(|x| x.to_string()),
};
match output {
OutputFormat::Json => println!(
"{}",
serde_json::to_string(&node_details)
.unwrap_or_else(|_| "Could not serialize node details".to_string())
),
OutputFormat::Text => println!("{}", node_details),
}
}
fn start_http_api(