Skip to main content

opal/
runtime.rs

1use dirs::{config_dir, home_dir};
2use std::env;
3use std::path::{Path, PathBuf};
4
5const DEFAULT_HOME_DIR: &str = ".opal";
6const REPO_CONFIG_DIR: &str = ".opal";
7
8fn opal_home() -> PathBuf {
9    if let Some(path) = env::var_os("OPAL_HOME")
10        && !path.is_empty()
11    {
12        let path = PathBuf::from(path);
13        if path.is_absolute() {
14            return path;
15        }
16        return env::current_dir()
17            .unwrap_or_else(|_| PathBuf::from("."))
18            .join(path);
19    }
20    home_dir()
21        .unwrap_or_else(|| PathBuf::from("."))
22        .join(DEFAULT_HOME_DIR)
23}
24
25pub fn runs_root() -> PathBuf {
26    opal_home()
27}
28
29pub fn session_dir(run_id: &str) -> PathBuf {
30    runs_root().join(run_id)
31}
32
33pub fn logs_dir(run_id: &str) -> PathBuf {
34    session_dir(run_id).join("logs")
35}
36
37pub fn cache_root() -> PathBuf {
38    opal_home().join("cache")
39}
40
41pub fn history_path() -> PathBuf {
42    opal_home().join("history.json")
43}
44
45pub fn resource_group_root() -> PathBuf {
46    opal_home().join("resource-groups")
47}
48
49pub fn config_dirs(workdir: &Path) -> Vec<PathBuf> {
50    let mut paths = Vec::new();
51    paths.push(workdir.join(REPO_CONFIG_DIR).join("config.toml"));
52    paths.push(opal_home().join("config.toml"));
53    if let Some(mut dir) = config_dir() {
54        dir.push("opal");
55        dir.push("config.toml");
56        paths.push(dir);
57    }
58    paths
59}