use std::path::{Path, PathBuf};
const STATE_DIR: &str = ".octoprint-env";
const RECORD: &str = "instance.json";
const API_KEY_FILE: &str = "api-key";
#[derive(Debug, Clone)]
pub struct Scripted {
pub url: String,
pub api_key: String,
pub printing: bool,
}
pub fn scripted() -> Scripted {
let state = workspace_root().join(STATE_DIR);
let record = read_or_explain(&state.join(RECORD));
let key = read_or_explain(&state.join(API_KEY_FILE));
let record: printobserver_types::serde_json::Value =
printobserver_types::serde_json::from_str(&record).expect("the record is JSON");
Scripted {
url: record["url"]
.as_str()
.expect("the record names a URL")
.to_owned(),
api_key: key.trim().to_owned(),
printing: record["printing"].as_bool().unwrap_or(false),
}
}
fn read_or_explain(path: &Path) -> String {
std::fs::read_to_string(path).unwrap_or_else(|error| {
panic!(
"the scripted OctoPrint environment is not up: {} could not be read ({error}). \
Run `just octoprint-up` first; this tier drives a real OctoPrint and has no \
fixture to fall back to.",
path.display()
)
})
}
fn workspace_root() -> PathBuf {
PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.join("..")
.join("..")
}