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
use crate::ShipInterface;
use std::fs::File;
use std::io::prelude::*;
use std::path::Path;
use yaml_rust::{Yaml, YamlLoader};
static BAREBONES_SHIP_CONFIG_YAML: &str = r#"
# IP Address of your Urbit ship (default is local)
ship_ip: "0.0.0.0"
# Port that the ship is on
ship_port: "8080"
# The `+code` of your ship
ship_code: "lidlut-tabwed-pillex-ridrup"
"#;
pub fn create_new_ship_config_file() -> Option<()> {
let file_path = Path::new("ship_config.yaml");
if file_path.exists() == false {
let mut file = File::create(file_path).ok()?;
file.write_all(&BAREBONES_SHIP_CONFIG_YAML.to_string().into_bytes())
.ok()?;
return Some(());
}
None
}
fn ship_interface_from_yaml(config: Yaml) -> Option<ShipInterface> {
let ip = config["ship_ip"].as_str()?;
let port = config["ship_port"].as_str()?;
let url = format!("http://{}:{}", ip, port);
let code = config["ship_code"].as_str()?;
ShipInterface::new(&url, code).ok()
}
pub fn ship_interface_from_local_config() -> Option<ShipInterface> {
ship_interface_from_config("ship_config.yaml")
}
pub fn ship_interface_from_config(path_to_file: &str) -> Option<ShipInterface> {
let yaml_str = std::fs::read_to_string(path_to_file).ok()?;
let yaml = YamlLoader::load_from_str(&yaml_str).ok()?[0].clone();
ship_interface_from_yaml(yaml)
}
pub fn default_cli_ship_interface_setup() -> ShipInterface {
if let Some(_) = create_new_ship_config_file() {
println!("Ship configuration file created. Please edit `ship_config.yaml` with your ship info and restart the application.");
std::process::exit(0);
}
if let Some(ship) = ship_interface_from_local_config() {
return ship;
}
println!("Failed to connect to Ship using information from local config.");
std::process::exit(1);
}