| 1 | use std::path::{Path, PathBuf}; |
| 2 | |
| 3 | use codewhale_runtime::config::Config; |
| 4 | |
| 5 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 6 | pub(super) struct SetupPersistenceFacts { |
| 7 | pub(super) home_result: String, |
| 8 | pub(super) config_result: String, |
| 9 | pub(super) state_result: String, |
| 10 | pub(super) constitution_result: String, |
| 11 | pub(super) memory_result: String, |
| 12 | pub(super) result: String, |
| 13 | } |
| 14 | |
| 15 | impl Default for SetupPersistenceFacts { |
| 16 | fn default() -> Self { |
| 17 | Self { |
| 18 | home_result: "CODEWHALE_HOME not loaded".to_string(), |
| 19 | config_result: "config path not loaded".to_string(), |
| 20 | state_result: "setup state path not loaded".to_string(), |
| 21 | constitution_result: "constitution path not loaded".to_string(), |
| 22 | memory_result: "memory path not loaded".to_string(), |
| 23 | result: "persistence paths not loaded".to_string(), |
| 24 | } |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | impl SetupPersistenceFacts { |
| 29 | pub(super) fn from_paths( |
| 30 | config_path_override: Option<PathBuf>, |
| 31 | config: &Config, |
| 32 | codewhale_home: &Path, |
| 33 | ) -> Self { |
| 34 | let home_source = if codewhale_config::codewhale_home_is_explicit() { |
| 35 | "explicit" |
| 36 | } else { |
| 37 | "default" |
| 38 | }; |
| 39 | let home_presence = dir_presence(codewhale_home); |
| 40 | let config_path = codewhale_config::resolve_config_path(config_path_override) |
| 41 | .unwrap_or_else(|_| codewhale_home.join("config.toml")); |
| 42 | let state_path = codewhale_config::SetupState::path().unwrap_or_else(|_| { |
| 43 | codewhale_home.join(codewhale_config::setup_state::SETUP_STATE_FILE_NAME) |
| 44 | }); |
| 45 | let constitution_path = codewhale_config::UserConstitution::path() |
| 46 | .unwrap_or_else(|_| codewhale_home.join("constitution.json")); |
| 47 | let memory_path = config.memory_path(); |
| 48 | |
| 49 | let config_presence = file_presence(&config_path); |
| 50 | let state_presence = file_presence(&state_path); |
| 51 | let constitution_presence = file_presence(&constitution_path); |
| 52 | let memory_presence = file_presence(&memory_path); |
| 53 | |
| 54 | Self { |
| 55 | home_result: format!( |
| 56 | "{home_source} CODEWHALE_HOME at {} ({home_presence})", |
| 57 | codewhale_home.display() |
| 58 | ), |
| 59 | config_result: path_result(&config_path, config_presence), |
| 60 | state_result: path_result(&state_path, state_presence), |
| 61 | constitution_result: path_result(&constitution_path, constitution_presence), |
| 62 | memory_result: path_result(&memory_path, memory_presence), |
| 63 | result: format!( |
| 64 | "home_source={home_source}, home={home_presence}, config={config_presence}, setup_state={state_presence}, constitution={constitution_presence}, memory={memory_presence}, mode=read_only_review" |
| 65 | ), |
| 66 | } |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | fn path_result(path: &Path, presence: &'static str) -> String { |
| 71 | format!("{} ({presence})", path.display()) |
| 72 | } |
| 73 | |
| 74 | fn dir_presence(path: &Path) -> &'static str { |
| 75 | if path.is_dir() { |
| 76 | "present" |
| 77 | } else if path.exists() { |
| 78 | "exists-not-dir" |
| 79 | } else { |
| 80 | "missing" |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | fn file_presence(path: &Path) -> &'static str { |
| 85 | if path.is_file() { |
| 86 | "present" |
| 87 | } else if path.exists() { |
| 88 | "exists-not-file" |
| 89 | } else { |
| 90 | "missing" |
| 91 | } |
| 92 | } |
| 93 |