Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: automated node config path args #85

Merged
merged 5 commits into from
Jul 5, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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`)");
Copy link
Collaborator

@niklaslong niklaslong Jul 5, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We might actually want to return a result here instead of panicking for future proofing? (A custom runner would likely need Results to function properly).

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