use std::fmt::Debug;
use std::net::SocketAddr;
use std::path::PathBuf;
use clap::ValueHint;
use internet2::addr::ServiceAddr;
use lnp_rpc::LNP_NODE_RPC_ENDPOINT;
use lnpbp::chain::Chain;
use microservices::shell::shell_setup;
const LNP_NODE_CTL_ENDPOINT: &str = "{data_dir}/ctl";
#[cfg(any(target_os = "linux"))]
pub const LNP_NODE_DATA_DIR: &'static str = "~/.lnp_node/{chain}";
#[cfg(any(target_os = "freebsd", target_os = "openbsd", target_os = "netbsd"))]
pub const LNP_NODE_DATA_DIR: &'static str = "~/.lnp_node/{chain}";
#[cfg(target_os = "macos")]
pub const LNP_NODE_DATA_DIR: &str = "~/Library/Application Support/LNP Node/{chain}";
#[cfg(target_os = "windows")]
pub const LNP_NODE_DATA_DIR: &'static str = "~\\AppData\\Local\\LNP Node\\{chain}";
#[cfg(target_os = "ios")]
pub const LNP_NODE_DATA_DIR: &'static str = "~/Documents/{chain}";
#[cfg(target_os = "android")]
pub const LNP_NODE_DATA_DIR: &'static str = "./{chain}";
pub const LNP_NODE_MSG_ENDPOINT: &str = "{data_dir}/msg";
pub const LNP_NODE_CONFIG: &str = "{data_dir}/lnp_node.toml";
pub const LNP_NODE_TOR_PROXY: &str = "127.0.0.1:9050";
pub const LNP_NODE_KEY_FILE: &str = "{data_dir}/node.key";
pub trait Options: Clone + Eq + Debug {
type Conf;
fn shared(&self) -> &Opts;
fn config(&self) -> Self::Conf;
}
#[derive(Parser, Clone, PartialEq, Eq, Debug)]
pub struct Opts {
#[clap(short, long, global = true, parse(from_occurrences))]
pub verbose: u8,
#[clap(
short,
long,
global = true,
default_value = LNP_NODE_DATA_DIR,
env = "LNP_NODE_DATA_DIR",
value_hint = ValueHint::DirPath
)]
pub data_dir: PathBuf,
#[clap(
short,
long,
global = true,
env = "LNP_NODE_CONFIG",
value_hint = ValueHint::FilePath
)]
pub config: Option<PathBuf>,
#[clap(
short = 'T',
long,
alias = "tor",
global = true,
env = "LNP_NODE_TOR_PROXY",
value_hint = ValueHint::Hostname
)]
pub tor_proxy: Option<Option<SocketAddr>>,
#[clap(
short = 'M',
long = "msg",
global = true,
env = "LNP_NODE_MSG_ENDPOINT",
default_value = LNP_NODE_MSG_ENDPOINT,
value_hint = ValueHint::FilePath
)]
pub msg_endpoint: ServiceAddr,
#[clap(
short = 'X',
long = "ctl",
global = true,
env = "LNP_NODE_CTL_ENDPOINT",
default_value = LNP_NODE_CTL_ENDPOINT,
value_hint = ValueHint::FilePath
)]
pub ctl_endpoint: ServiceAddr,
#[clap(
short = 'R',
long = "rpc",
global = true,
default_value = LNP_NODE_RPC_ENDPOINT,
env = "LNP_NODE_RPC_ENDPOINT"
)]
pub rpc_endpoint: ServiceAddr,
#[clap(
short = 'n',
long,
global = true,
alias = "network",
default_value = "signet",
env = "LNP_NODE_NETWORK"
)]
pub chain: Chain,
#[clap(
long,
global = true,
default_value("pandora.network"),
env = "LNP_NODE_ELECTRUM_SERVER",
value_hint = ValueHint::Hostname
)]
pub electrum_server: String,
#[clap(long, global = true, env = "LNP_NODE_ELECTRUM_PORT")]
pub electrum_port: Option<u16>,
#[clap(short = 't', long = "threaded")]
pub threaded_daemons: bool,
}
impl Opts {
pub fn process(&mut self) {
shell_setup(
self.verbose,
[&mut self.msg_endpoint, &mut self.ctl_endpoint, &mut self.rpc_endpoint],
&mut self.data_dir,
&[("{chain}", self.chain.to_string())],
);
}
}