1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
use crate::node_interface::{NodeError, NodeInterface, Result};
use std::fs::File;
use std::io::prelude::*;
use std::path::Path;
use yaml_rust::{Yaml, YamlLoader};
static BAREBONES_CONFIG_YAML: &str = r#"
# IP Address of the node (default is local, edit if yours is different)
node_ip: "0.0.0.0"
# Port that the node is on (default is 9053, edit if yours is different)
node_port: "9053"
# API key for the node (edit if yours is different)
node_api_key: "hello"
"#;
pub fn acquire_node_interface_from_local_config() -> NodeInterface {
if !does_local_config_exist() {
println!("Could not find local `node-interface.yaml` file.\nCreating said file with basic defaults.\nPlease edit the yaml file and update it with your node parameters to ensure the CLI app can proceed.");
create_new_local_config_file().ok();
std::process::exit(0);
}
if let Err(e) = new_interface_from_local_config() {
println!(
"Could not parse local `node-interface.yaml` file.\nError: {:?}",
e
);
std::process::exit(0);
}
new_interface_from_local_config().unwrap()
}
pub fn does_local_config_exist() -> bool {
Path::new("node-interface.yaml").exists()
}
pub fn create_new_local_config_file() -> Result<()> {
let file_path = Path::new("node-interface.yaml");
if file_path.exists() == false {
let mut file = File::create(file_path).map_err(|_| {
NodeError::YamlError("Failed to create `node-interface.yaml` file".to_string())
})?;
file.write_all(&BAREBONES_CONFIG_YAML.to_string().into_bytes())
.map_err(|_| {
NodeError::YamlError(
"Failed to write to local `node-interface.yaml` file".to_string(),
)
})?;
}
Err(NodeError::YamlError(
"Local `node-interface.yaml` already exists.".to_string(),
))
}
pub fn new_interface_from_yaml(config: Yaml) -> Result<NodeInterface> {
let ip = config["node_ip"].as_str().ok_or(NodeError::YamlError(
"`node_ip` is not specified in the provided Yaml".to_string(),
))?;
let port = config["node_port"].as_str().ok_or(NodeError::YamlError(
"`node_port` is not specified in the provided Yaml".to_string(),
))?;
let api_key = config["node_api_key"].as_str().ok_or(NodeError::YamlError(
"`node_api_key` is not specified in the provided Yaml".to_string(),
))?;
Ok(NodeInterface::new(api_key, ip, port))
}
pub fn new_interface_from_local_config() -> Result<NodeInterface> {
let yaml_str = std::fs::read_to_string("node-interface.yaml").map_err(|_| {
NodeError::YamlError("Failed to read local `node-interface.yaml` file".to_string())
})?;
let yaml = YamlLoader::load_from_str(&yaml_str).unwrap()[0].clone();
new_interface_from_yaml(yaml)
}