From b026e9107f875c3b6ae61b1bfa00ca4bd4bdfee0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C4=99drzej=20Stuczy=C5=84ski?= Date: Thu, 30 Mar 2023 15:51:49 +0100 Subject: [PATCH] standarised output-json in client init --- clients/native/Cargo.toml | 3 +- clients/native/src/commands/init.rs | 31 ++++++++--------- clients/socks5/Cargo.toml | 2 +- clients/socks5/src/commands/init.rs | 34 +++++++++---------- .../network-requester/Cargo.toml | 2 +- .../network-requester/src/cli/init.rs | 28 +++++++-------- 6 files changed, 45 insertions(+), 55 deletions(-) diff --git a/clients/native/Cargo.toml b/clients/native/Cargo.toml index 9a92353283..1adefd9679 100644 --- a/clients/native/Cargo.toml +++ b/clients/native/Cargo.toml @@ -34,7 +34,7 @@ tokio = { version = "1.24.1", features = ["rt-multi-thread", "net", "signal"] } tokio-tungstenite = "0.14" # websocket ## internal -nym-bin-common = { path = "../../common/bin-common" } +nym-bin-common = { path = "../../common/bin-common", features = ["serde_json"] } client-core = { path = "../../common/client-core", features = ["fs-surb-storage"] } nym-coconut-interface = { path = "../../common/coconut-interface" } nym-config = { path = "../../common/config" } @@ -52,4 +52,3 @@ validator-client = { path = "../../common/client-libs/validator-client", feature websocket-requests = { path = "websocket-requests" } [dev-dependencies] -serde_json = { workspace = true } # for the "textsend" example diff --git a/clients/native/src/commands/init.rs b/clients/native/src/commands/init.rs index 95ba859f80..606de14c78 100644 --- a/clients/native/src/commands/init.rs +++ b/clients/native/src/commands/init.rs @@ -8,6 +8,7 @@ use crate::{ error::ClientError, }; use clap::Args; +use nym_bin_common::output_format::OutputFormat; use nym_config::NymConfig; use nym_crypto::asymmetric::identity; use nym_sphinx::addressing::clients::Recipient; @@ -71,9 +72,8 @@ pub(crate) struct Init { #[clap(long, hide = true)] enabled_credentials_mode: Option, - /// Save a summary of the initialization to a json file - #[clap(long)] - output_json: bool, + #[clap(short, long, default_value_t = OutputFormat::default())] + output: OutputFormat, } impl From for OverrideConfig { @@ -97,6 +97,7 @@ pub struct InitResults { #[serde(flatten)] client_core: client_core::init::InitResults, client_listening_port: String, + client_address: Recipient, } impl InitResults { @@ -104,6 +105,7 @@ impl InitResults { Self { client_core: client_core::init::InitResults::new(config.get_base(), address), client_listening_port: config.get_listening_port().to_string(), + client_address: *address, } } } @@ -111,12 +113,13 @@ impl InitResults { impl Display for InitResults { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { writeln!(f, "{}", self.client_core)?; - write!(f, "Client listening port: {}", self.client_listening_port) + writeln!(f, "Client listening port: {}", self.client_listening_port)?; + write!(f, "address of this client: {}", self.client_address) } } pub(crate) async fn execute(args: &Init) -> Result<(), ClientError> { - println!("Initialising client..."); + eprintln!("Initialising client..."); let id = &args.id; @@ -125,14 +128,14 @@ pub(crate) async fn execute(args: &Init) -> Result<(), ClientError> { // in case we're using old config, try to upgrade it // (if we're using the current version, it's a no-op) try_upgrade_v1_1_13_config(id)?; - println!("Client \"{id}\" was already initialised before"); + eprintln!("Client \"{id}\" was already initialised before"); } // Usually you only register with the gateway on the first init, however you can force // re-registering if wanted. let user_wants_force_register = args.force_register_gateway; if user_wants_force_register { - println!("Instructed to force registering gateway. This might overwrite keys!"); + eprintln!("Instructed to force registering gateway. This might overwrite keys!"); } // If the client was already initialized, don't generate new keys and don't re-register with @@ -167,26 +170,20 @@ pub(crate) async fn execute(args: &Init) -> Result<(), ClientError> { let address = client_core::init::get_client_address_from_stored_keys(config.get_base())?; let init_results = InitResults::new(&config, &address); - println!("{init_results}"); + println!("{}", args.output.format(&init_results)); - // Output summary to a json file, if specified - if args.output_json { - client_core::init::output_to_json(&init_results, "client_init_results.json"); - } - - println!("\nThe address of this client is: {address}\n"); Ok(()) } fn print_saved_config(config: &Config) { let config_save_location = config.get_config_file_save_location(); - println!("Saved configuration file to {config_save_location:?}"); - println!("Using gateway: {}", config.get_base().get_gateway_id()); + eprintln!("Saved configuration file to {config_save_location:?}"); + eprintln!("Using gateway: {}", config.get_base().get_gateway_id()); log::debug!("Gateway id: {}", config.get_base().get_gateway_id()); log::debug!("Gateway owner: {}", config.get_base().get_gateway_owner()); log::debug!( "Gateway listener: {}", config.get_base().get_gateway_listener() ); - println!("Client configuration completed.\n"); + eprintln!("Client configuration completed.\n"); } diff --git a/clients/socks5/Cargo.toml b/clients/socks5/Cargo.toml index 2865a75ac7..b350b5cae9 100644 --- a/clients/socks5/Cargo.toml +++ b/clients/socks5/Cargo.toml @@ -19,7 +19,7 @@ tokio = { version = "1.24.1", features = ["rt-multi-thread", "net", "signal"] } url = "2.2" # internal -nym-bin-common = { path = "../../common/bin-common" } +nym-bin-common = { path = "../../common/bin-common", features = ["serde_json"] } client-core = { path = "../../common/client-core", features = ["fs-surb-storage"] } nym-coconut-interface = { path = "../../common/coconut-interface" } nym-config = { path = "../../common/config" } diff --git a/clients/socks5/src/commands/init.rs b/clients/socks5/src/commands/init.rs index e3e50ec389..4e10b603a9 100644 --- a/clients/socks5/src/commands/init.rs +++ b/clients/socks5/src/commands/init.rs @@ -1,4 +1,4 @@ -// Copyright 2021 - Nym Technologies SA +// Copyright 2021-2023 - Nym Technologies SA // SPDX-License-Identifier: Apache-2.0 use crate::commands::try_upgrade_v1_1_13_config; @@ -7,6 +7,7 @@ use crate::{ error::Socks5ClientError, }; use clap::Args; +use nym_bin_common::output_format::OutputFormat; use nym_config::NymConfig; use nym_crypto::asymmetric::identity; use nym_socks5_client_core::config::Config; @@ -75,9 +76,8 @@ pub(crate) struct Init { #[clap(long, hide = true)] enabled_credentials_mode: Option, - /// Save a summary of the initialization to a json file - #[clap(long)] - output_json: bool, + #[clap(short, long, default_value_t = OutputFormat::default())] + output: OutputFormat, } impl From for OverrideConfig { @@ -99,6 +99,7 @@ pub struct InitResults { #[serde(flatten)] client_core: client_core::init::InitResults, socks5_listening_port: String, + client_address: Recipient, } impl InitResults { @@ -106,6 +107,7 @@ impl InitResults { Self { client_core: client_core::init::InitResults::new(config.get_base(), address), socks5_listening_port: config.get_socks5().get_listening_port().to_string(), + client_address: *address, } } } @@ -113,12 +115,13 @@ impl InitResults { impl Display for InitResults { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { writeln!(f, "{}", self.client_core)?; - write!(f, "SOCKS5 listening port: {}", self.socks5_listening_port) + write!(f, "SOCKS5 listening port: {}", self.socks5_listening_port)?; + write!(f, "address of this client: {}", self.client_address) } } pub(crate) async fn execute(args: &Init) -> Result<(), Socks5ClientError> { - println!("Initialising client..."); + eprintln!("Initialising client..."); let id = &args.id; let provider_address = &args.provider; @@ -128,14 +131,14 @@ pub(crate) async fn execute(args: &Init) -> Result<(), Socks5ClientError> { // in case we're using old config, try to upgrade it // (if we're using the current version, it's a no-op) try_upgrade_v1_1_13_config(id)?; - println!("SOCKS5 client \"{id}\" was already initialised before"); + eprintln!("SOCKS5 client \"{id}\" was already initialised before"); } // Usually you only register with the gateway on the first init, however you can force // re-registering if wanted. let user_wants_force_register = args.force_register_gateway; if user_wants_force_register { - println!("Instructed to force registering gateway. This might overwrite keys!"); + eprintln!("Instructed to force registering gateway. This might overwrite keys!"); } // If the client was already initialized, don't generate new keys and don't re-register with @@ -175,26 +178,21 @@ pub(crate) async fn execute(args: &Init) -> Result<(), Socks5ClientError> { let address = client_core::init::get_client_address_from_stored_keys(config.get_base())?; let init_results = InitResults::new(&config, &address); - println!("{}", init_results); + println!("{}", args.output.format(&init_results)); - // Output summary to a json file, if specified - if args.output_json { - client_core::init::output_to_json(&init_results, "socks5_client_init_results.json"); - } - - println!("\nThe address of this client is: {}\n", address); + eprintln!("\nThe address of this client is: {}\n", address); Ok(()) } fn print_saved_config(config: &Config) { let config_save_location = config.get_config_file_save_location(); - println!("Saved configuration file to {:?}", config_save_location); - println!("Using gateway: {}", config.get_base().get_gateway_id()); + eprintln!("Saved configuration file to {:?}", config_save_location); + eprintln!("Using gateway: {}", config.get_base().get_gateway_id()); log::debug!("Gateway id: {}", config.get_base().get_gateway_id()); log::debug!("Gateway owner: {}", config.get_base().get_gateway_owner()); log::debug!( "Gateway listener: {}", config.get_base().get_gateway_listener() ); - println!("Client configuration completed.\n"); + eprintln!("Client configuration completed.\n"); } diff --git a/service-providers/network-requester/Cargo.toml b/service-providers/network-requester/Cargo.toml index fe39370b8e..af2f8cecb9 100644 --- a/service-providers/network-requester/Cargo.toml +++ b/service-providers/network-requester/Cargo.toml @@ -34,7 +34,7 @@ url = { workspace = true } client-core = { path = "../../common/client-core" } nym-config = { path = "../../common/config" } nym-crypto = { path = "../../common/crypto" } -nym-bin-common = { path = "../../common/bin-common"} +nym-bin-common = { path = "../../common/bin-common", features = ["serde_json"] } nym-network-defaults = { path = "../../common/network-defaults" } nym-sdk = { path = "../../sdk/rust/nym-sdk" } nym-sphinx = { path = "../../common/nymsphinx" } diff --git a/service-providers/network-requester/src/cli/init.rs b/service-providers/network-requester/src/cli/init.rs index ef9320b9a2..3861027f7e 100644 --- a/service-providers/network-requester/src/cli/init.rs +++ b/service-providers/network-requester/src/cli/init.rs @@ -8,6 +8,7 @@ use crate::{ error::NetworkRequesterError, }; use clap::Args; +use nym_bin_common::output_format::OutputFormat; use nym_config::NymConfig; use nym_crypto::asymmetric::identity; use nym_sphinx::addressing::clients::Recipient; @@ -49,9 +50,8 @@ pub(crate) struct Init { #[clap(long)] enabled_credentials_mode: Option, - /// Save a summary of the initialization to a json file - #[clap(long)] - output_json: bool, + #[clap(short, long, default_value_t = OutputFormat::default())] + output: OutputFormat, } impl From for OverrideConfig { @@ -71,12 +71,14 @@ impl From for OverrideConfig { pub struct InitResults { #[serde(flatten)] client_core: client_core::init::InitResults, + client_address: Recipient, } impl InitResults { fn new(config: &Config, address: &Recipient) -> Self { Self { client_core: client_core::init::InitResults::new(config.get_base(), address), + client_address: *address, } } } @@ -88,7 +90,7 @@ impl Display for InitResults { } pub(crate) async fn execute(args: &Init) -> Result<(), NetworkRequesterError> { - println!("Initialising client..."); + eprintln!("Initialising client..."); let id = &args.id; @@ -97,14 +99,14 @@ pub(crate) async fn execute(args: &Init) -> Result<(), NetworkRequesterError> { // in case we're using old config, try to upgrade it // (if we're using the current version, it's a no-op) try_upgrade_v1_1_13_config(id)?; - println!("Client \"{id}\" was already initialised before"); + eprintln!("Client \"{id}\" was already initialised before"); } // Usually you only register with the gateway on the first init, however you can force // re-registering if wanted. let user_wants_force_register = args.force_register_gateway; if user_wants_force_register { - println!("Instructed to force registering gateway. This might overwrite keys!"); + eprintln!("Instructed to force registering gateway. This might overwrite keys!"); } // If the client was already initialized, don't generate new keys and don't re-register with @@ -142,26 +144,20 @@ pub(crate) async fn execute(args: &Init) -> Result<(), NetworkRequesterError> { let address = client_core::init::get_client_address_from_stored_keys(config.get_base())?; let init_results = InitResults::new(&config, &address); - println!("{init_results}"); + println!("{}", args.output.format(&init_results)); - // Output summary to a json file, if specified - if args.output_json { - client_core::init::output_to_json(&init_results, "client_init_results.json"); - } - - println!("\nThe address of this client is: {address}\n"); Ok(()) } fn print_saved_config(config: &Config) { let config_save_location = config.get_config_file_save_location(); - println!("Saved configuration file to {config_save_location:?}"); - println!("Using gateway: {}", config.get_base().get_gateway_id()); + eprintln!("Saved configuration file to {config_save_location:?}"); + eprintln!("Using gateway: {}", config.get_base().get_gateway_id()); log::debug!("Gateway id: {}", config.get_base().get_gateway_id()); log::debug!("Gateway owner: {}", config.get_base().get_gateway_owner()); log::debug!( "Gateway listener: {}", config.get_base().get_gateway_listener() ); - println!("Client configuration completed.\n"); + eprintln!("Client configuration completed.\n"); }