Skip to content

Commit

Permalink
ref: move node config args to NodeMetaData
Browse files Browse the repository at this point in the history
  • Loading branch information
Mirko-von-Leipzig committed Jul 5, 2021
1 parent 14e6145 commit f40f463
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 34 deletions.
29 changes: 29 additions & 0 deletions src/setup/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ use std::{

use crate::setup::node::Action;

// The names of the files the node configurations will be written to.
const ZEBRA_CONFIG: &str = "zebra.toml";
const ZCASHD_CONFIG: &str = "zcash.conf";

const CONFIG: &str = "config.toml";
const DEFAULT_PORT: u16 = 8080;

Expand Down Expand Up @@ -65,6 +69,16 @@ pub(super) enum NodeKind {
Zcashd,
}

impl NodeKind {
/// Path to the configuration file for this [NodeKind]
pub(super) fn config_filepath(&self) -> std::path::PathBuf {
match self {
NodeKind::Zebra => std::env::current_dir().unwrap().join(ZEBRA_CONFIG),
NodeKind::Zcashd => std::env::current_dir().unwrap().join(ZCASHD_CONFIG),
}
}
}

/// Node configuration read from the `config.toml` file.
#[derive(Clone)]
pub(super) struct NodeMetaData {
Expand All @@ -91,6 +105,21 @@ impl NodeMetaData {
let mut start_args = args_from(&config_file.start_command);
let start_command = start_args.remove(0);

// insert the config file path into start args
let config_path = config_file.kind.config_filepath();
match config_file.kind {
NodeKind::Zebra => {
// Zebra's final arg must be `start`, so we insert the actual args before it.
let n = start_args.len();
assert!(n > 1, "Expected at least one arg for Zebra (`start`)");
start_args.insert(n - 1, "--config".into());
start_args.insert(n, config_path.into_os_string());
}
NodeKind::Zcashd => {
start_args.push(format!("-conf={}", config_path.to_str().unwrap()).into());
}
}

Ok(Self {
kind: config_file.kind,
path: config_file.path,
Expand Down
38 changes: 4 additions & 34 deletions src/setup/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@ use tokio::process::{Child, Command};

use std::{fs, io, net::SocketAddr, process::Stdio, time::Duration};

// The names of the files the node configurations will be written to.
const ZEBRA_CONFIG: &str = "zebra.toml";
const ZCASHD_CONFIG: &str = "zcash.conf";

/// Actions to prepare node state on start.
pub enum Action {
/// Performs no action
Expand Down Expand Up @@ -67,30 +63,11 @@ impl Node {
let config = NodeConfig::new();
let meta = NodeMetaData::new()?;

let mut node = Self {
Ok(Self {
config,
meta,
process: None,
};

// insert the config file cmd args
match node.meta.kind {
NodeKind::Zebra => {
let n = node.meta.start_args.len();
assert!(n > 1, "Expected at least one arg for Zebra (`start`)");
node.meta.start_args.insert(n - 1, "--config".into());
node.meta
.start_args
.insert(n, node.config_filepath().into_os_string());
}
NodeKind::Zcashd => {
node.meta
.start_args
.push(format!("-conf={}", node.config_filepath().to_str().unwrap()).into());
}
}

Ok(node)
})
}

/// Returns the (external) address of the node.
Expand Down Expand Up @@ -291,7 +268,7 @@ impl Node {
}

fn generate_config_file(&self) -> io::Result<()> {
let path = self.config_filepath();
let path = self.meta.kind.config_filepath();
let content = match self.meta.kind {
NodeKind::Zebra => ZebraConfigFile::generate(&self.config)
.map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))?,
Expand All @@ -301,20 +278,13 @@ impl Node {
fs::write(path, content)
}

fn config_filepath(&self) -> std::path::PathBuf {
match self.meta.kind {
NodeKind::Zebra => std::env::current_dir().unwrap().join(ZEBRA_CONFIG),
NodeKind::Zcashd => std::env::current_dir().unwrap().join(ZCASHD_CONFIG),
}
}

fn cleanup(&self) -> io::Result<()> {
self.cleanup_config_file()?;
self.cleanup_cache()
}

fn cleanup_config_file(&self) -> io::Result<()> {
let path = self.config_filepath();
let path = self.meta.kind.config_filepath();
match fs::remove_file(path) {
// File may not exist, so we surpress the error.
Err(e) if e.kind() != std::io::ErrorKind::NotFound => Err(e),
Expand Down

0 comments on commit f40f463

Please sign in to comment.