urbit_http_api/
local_config.rs

1use 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
16/// Attempts to create a new `ship_config.yaml` with the barebones yaml inside.
17/// Returns `None` if file already exists.
18pub 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
29/// Based on the provided input config yaml, create a ShipInterface
30fn 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
39/// Opens a local `ship_config.yaml` file and uses the
40/// data inside to create a `ShipInterface`
41pub fn ship_interface_from_local_config() -> Option<ShipInterface> {
42    ship_interface_from_config("ship_config.yaml")
43}
44
45/// Opens the yaml file specified by `path_to_file` and uses the
46/// data inside to create a `ShipInterface`
47pub 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
53/// A function for CLI apps which first attempts to create a new local ship config file if one does not exist and exits with a helpful message.
54/// If a config does exist, then it tries to connect to the Urbit Ship specified in the config.
55/// If connection fails then prints a message telling the user to check their local config.
56pub 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}