urbit_http_api/
local_config.rs1use crate::ShipInterface;
2use std::fs::File;
3use std::io::prelude::*;
4use std::path::Path;
5use yaml_rust::{Yaml, YamlLoader};
6
7static BAREBONES_SHIP_CONFIG_YAML: &str = r#"
8# IP Address of your Urbit ship (default is local)
9ship_ip: "0.0.0.0"
10# Port that the ship is on
11ship_port: "8080"
12# The `+code` of your ship
13ship_code: "lidlut-tabwed-pillex-ridrup"
14"#;
15
16pub fn create_new_ship_config_file() -> Option<()> {
19 let file_path = Path::new("ship_config.yaml");
20 if file_path.exists() == false {
21 let mut file = File::create(file_path).ok()?;
22 file.write_all(&BAREBONES_SHIP_CONFIG_YAML.to_string().into_bytes())
23 .ok()?;
24 return Some(());
25 }
26 None
27}
28
29fn ship_interface_from_yaml(config: Yaml) -> Option<ShipInterface> {
31 let ip = config["ship_ip"].as_str()?;
32 let port = config["ship_port"].as_str()?;
33 let url = format!("http://{}:{}", ip, port);
34 let code = config["ship_code"].as_str()?;
35
36 ShipInterface::new(&url, code).ok()
37}
38
39pub fn ship_interface_from_local_config() -> Option<ShipInterface> {
42 ship_interface_from_config("ship_config.yaml")
43}
44
45pub fn ship_interface_from_config(path_to_file: &str) -> Option<ShipInterface> {
48 let yaml_str = std::fs::read_to_string(path_to_file).ok()?;
49 let yaml = YamlLoader::load_from_str(&yaml_str).ok()?[0].clone();
50 ship_interface_from_yaml(yaml)
51}
52
53pub fn default_cli_ship_interface_setup() -> ShipInterface {
57 if let Some(_) = create_new_ship_config_file() {
58 println!("Ship configuration file created. Please edit `ship_config.yaml` with your ship info and restart the application.");
59 std::process::exit(0);
60 }
61 if let Some(ship) = ship_interface_from_local_config() {
62 return ship;
63 }
64 println!("Failed to connect to Ship using information from local config.");
65 std::process::exit(1);
66}