Extracted args
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Jake Hillion 2021-06-16 22:16:22 +01:00
parent 1e8f9da839
commit 794f5baee3
2 changed files with 61 additions and 46 deletions

52
node/src/args.rs Normal file
View File

@ -0,0 +1,52 @@
use std::net::SocketAddr;
use std::net::ToSocketAddrs;
use std::path::PathBuf;
use std::io;
use structopt::StructOpt;
pub const DEFAULT_PORT: u16 = 8443;
const DEFAULT_PORT_STR: &str = "8443";
#[derive(Debug, StructOpt)]
pub enum Args {
/// Run the node
Run(Run),
}
impl Args {
pub fn from_args() -> Self {
StructOpt::from_args()
}
}
#[derive(Debug, StructOpt)]
pub struct Run {
/// Exit once the node has caught up
#[structopt(short = "s", long = "short")]
short: bool,
/// Fork and run as a daemon
#[structopt(short = "d", long = "daemon")]
daemon: bool,
/// Port on which to run the node
#[structopt(short = "p", long = "port", default_value = DEFAULT_PORT_STR)]
port: u16,
/// Path in which to store node content
#[structopt(long = "path", default_value = ".unnamed/node")]
path: PathBuf,
/// Nodes to initially connect to
#[structopt(long = "initial_nodes", parse(try_from_str = parse_connection_address))]
initial_nodes: Vec<SocketAddr>,
}
fn parse_connection_address(arg: &str) -> io::Result<SocketAddr> {
let mut addrs = arg
.to_socket_addrs()
.or_else(|_| (arg, DEFAULT_PORT).to_socket_addrs())?;
Ok(addrs.next().expect("no socket addr"))
}

View File

@ -1,53 +1,16 @@
use std::net::SocketAddr;
use std::net::ToSocketAddrs;
use std::path::PathBuf;
mod args;
use std::io;
#[derive(Debug)]
enum Error {}
use structopt::StructOpt;
fn main() -> Result<(), Error> {
let node = args::Args::from_args();
pub const DEFAULT_PORT: u16 = 8443;
const DEFAULT_PORT_STR: &str = "8443";
#[derive(Debug, StructOpt)]
enum Node {
/// Run the node
Run {
/// Exit once the node has caught up
#[structopt(short = "s", long = "short")]
short: bool,
/// Fork and run as a daemon
#[structopt(short = "d", long = "daemon")]
daemon: bool,
/// Port on which to run the node
#[structopt(short = "p", long = "port", default_value = DEFAULT_PORT_STR)]
port: u16,
/// Path in which to store node content
#[structopt(long = "path", default_value = ".unnamed/node")]
path: PathBuf,
/// Nodes to initially connect to
#[structopt(long = "initial_nodes", parse(try_from_str = parse_connection_address))]
initial_nodes: Vec<SocketAddr>,
},
match node {
args::Args::Run(args) => run(&args),
}
}
fn parse_connection_address(arg: &str) -> io::Result<SocketAddr> {
let mut addrs = arg
.to_socket_addrs()
.or_else(|_| (arg, DEFAULT_PORT).to_socket_addrs())?;
Ok(addrs.next().expect("no socket addr"))
}
fn main() {
let node = Node::from_args();
println!("{:?}", node);
}
fn run() -> Result<(), ()> {
fn run(args: &args::Run) -> Result<(), Error> {
Ok(())
}