Skip to content
This repository has been archived by the owner on Feb 3, 2023. It is now read-only.

consolidate env vars into one file #990

Merged
merged 12 commits into from
Feb 12, 2019
15 changes: 9 additions & 6 deletions cli/src/cli/run.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use cli::{self, package};
use colored::*;
use error::DefaultResult;
use holochain_common::env_vars::{get_env_var_value, EnvVar};
use holochain_conductor_api::{
conductor::{mount_conductor_from_config, CONDUCTOR},
config::*,
Expand Down Expand Up @@ -28,7 +29,7 @@ pub fn run(
cli::package(true, Some(package::DEFAULT_BUNDLE_FILE_NAME.into()))?;
}

let agent_name = env::var("HC_AGENT").ok();
let agent_name = get_env_var_value(EnvVar::Agent).ok();
let agent = AgentId::generate_fake(&agent_name.unwrap_or_else(|| String::from("testAgent")));
let agent_config = AgentConfiguration {
id: AGENT_CONFIG_ID.into(),
Expand Down Expand Up @@ -60,7 +61,9 @@ pub fn run(
storage,
};

let interface_type = env::var("HC_INTERFACE").ok().unwrap_or_else(|| interface);
let interface_type = get_env_var_value(EnvVar::Interface)
.ok()
.unwrap_or_else(|| interface);
let driver = if interface_type == String::from("websocket") {
InterfaceDriver::Websocket { port }
} else if interface_type == String::from("http") {
Expand All @@ -85,15 +88,15 @@ pub fn run(
rules,
};

let n3h_path = env::var("HC_N3H_PATH").ok();
let n3h_path = get_env_var_value(EnvVar::N3hPath).ok();

// create an n3h network config if the --networked flag is set
// or if a value where to find n3h has been put into the
// HC_N3H_PATH environment variable
let network_config = if networked || n3h_path.is_some() {
let n3h_mode = env::var("HC_N3H_MODE").ok();
let n3h_persistence_path = env::var("HC_N3H_WORK_DIR").ok();
let n3h_bootstrap_node = env::var("HC_N3H_BOOTSTRAP_NODE").ok();
let n3h_mode = get_env_var_value(EnvVar::N3hMode).ok();
let n3h_persistence_path = get_env_var_value(EnvVar::N3hWorkDir).ok();
let n3h_bootstrap_node = get_env_var_value(EnvVar::N3hBootstrapNode).ok();
let mut n3h_bootstrap = Vec::new();

if n3h_bootstrap_node.is_some() {
Expand Down
31 changes: 31 additions & 0 deletions common/src/env_vars.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use std::{env::VarError, ffi::OsStr};

pub enum EnvVar {
Agent,
Interface,
N3hPath,
N3hMode,
N3hWorkDir,
N3hBootstrapNode,
SimpleLoggerMute,
TargetPrefix,
}

impl AsRef<OsStr> for EnvVar {
Connoropolous marked this conversation as resolved.
Show resolved Hide resolved
fn as_ref(&self) -> &OsStr {
match &self {
EnvVar::Agent => &OsStr::new("HC_AGENT"),
EnvVar::Interface => &OsStr::new("HC_INTERFACE"),
EnvVar::N3hPath => &OsStr::new("HC_N3H_PATH"),
EnvVar::N3hMode => &OsStr::new("HC_N3H_MODE"),
EnvVar::N3hWorkDir => &OsStr::new("HC_N3H_WORK_DIR"),
EnvVar::N3hBootstrapNode => &OsStr::new("HC_N3H_BOOTSTRAP_NODE"),
EnvVar::SimpleLoggerMute => &OsStr::new("HC_SIMPLE_LOGGER_MUTE"),
EnvVar::TargetPrefix => &OsStr::new("HC_TARGET_PREFIX"),
}
}
}

pub fn get_env_var_value(env_var: EnvVar) -> Result<String, VarError> {
Connoropolous marked this conversation as resolved.
Show resolved Hide resolved
std::env::var(env_var)
}
1 change: 1 addition & 0 deletions common/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
pub mod env_vars;
pub mod paths;
1 change: 1 addition & 0 deletions core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ num-derive = "0.2"
config = "0.8"
regex = "1"
toml = "0.4.8"
holochain_common = { path = "../common" }
holochain_core_types = { path = "../core_types" }
holochain_core_types_derive = { path = "../core_types_derive" }
holochain_cas_implementations = { path = "../cas_implementations" }
Expand Down
1 change: 1 addition & 0 deletions core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ extern crate num_traits;
extern crate regex;

extern crate config;
extern crate holochain_common;
extern crate holochain_net;
#[macro_use]
extern crate holochain_wasm_utils;
Expand Down
3 changes: 2 additions & 1 deletion core/src/logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
//! which is separate from standard logging via the log crate warn! info! debug! logging that
//! gets emitted globaly from the conductor.
use chrono::Local;
use holochain_common::env_vars::{get_env_var_value, EnvVar};
use std::sync::{mpsc, Arc, Mutex};

/// trait that defines the logging functionality that holochain_core requires
Expand All @@ -21,7 +22,7 @@ pub struct SimpleLogger {
#[cfg_attr(tarpaulin, skip)]
impl Logger for SimpleLogger {
fn log(&mut self, msg: String) {
if std::env::var("HC_SIMPLE_LOGGER_MUTE").is_err() {
if get_env_var_value(EnvVar::SimpleLoggerMute).is_err() {
let date = Local::now();
println!("{}:{}", date.format("%Y-%m-%d %H:%M:%S"), msg);
}
Expand Down
1 change: 1 addition & 0 deletions wasm_utils/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ authors = ["Holochain Core Dev Team <devcore@holochain.org>"]
serde = "1.0"
serde_derive = "1.0"
serde_json = { version = "1.0", features = ["preserve_order"] }
holochain_common = { path = "../common" }
holochain_core_types = { path = "../core_types" }
holochain_core_types_derive = { path = "../core_types_derive" }

Expand Down
5 changes: 4 additions & 1 deletion wasm_utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@ extern crate serde;
extern crate serde_json;
#[macro_use]
extern crate serde_derive;
extern crate holochain_common;
pub extern crate holochain_core_types;
#[macro_use]
pub extern crate holochain_core_types_derive;

use holochain_common::env_vars::{get_env_var_value, EnvVar};

/// ignore api_serialization because it is nothing but structs to hold serialization
#[cfg_attr(tarpaulin, skip)]
pub mod api_serialization;
Expand All @@ -18,7 +21,7 @@ pub mod macros;
pub mod memory;

pub fn wasm_target_dir(test_path: &str, wasm_path: &str) -> String {
match std::env::var("HC_TARGET_PREFIX") {
match get_env_var_value(EnvVar::TargetPrefix) {
Ok(prefix) => format!("{}{}{}target", prefix, test_path, wasm_path),
Err(_) => format!("{}target", wasm_path),
}
Expand Down