-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathhandler.rs
More file actions
227 lines (201 loc) · 7.76 KB
/
Copy pathhandler.rs
File metadata and controls
227 lines (201 loc) · 7.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
//! List command handler implementation
//!
//! **Purpose**: List all environments in the deployment workspace
//!
//! This handler scans the data directory for environments and extracts
//! summary information for display. It is a read-only operation that
//! does not modify any state or make any network calls.
//!
//! ## Design Strategy
//!
//! The list command scans local storage for environments:
//!
//! 1. **Directory Scan**: Find all environment directories in data/
//! 2. **Load Summaries**: Extract lightweight info from each environment
//! 3. **Graceful Degradation**: Continue on per-environment errors
//! 4. **Report Failures**: Include failed environments in the result
//!
//! ## Design Rationale
//!
//! This command works directly with the data directory rather than through
//! the repository abstraction because:
//!
//! - Need to enumerate all environments (repository has no list method)
//! - Must handle partially corrupted data gracefully
//! - Performance: lightweight scanning without full deserialization where possible
use std::fs;
use std::path::Path;
use std::sync::Arc;
use tracing::{instrument, warn};
use super::errors::ListCommandHandlerError;
use super::info::{EnvironmentList, EnvironmentSummary};
use crate::application::traits::RepositoryProvider;
use crate::domain::environment::name::EnvironmentName;
use crate::domain::environment::repository::EnvironmentRepository;
use crate::domain::environment::state::AnyEnvironmentState;
/// `ListCommandHandler` scans and lists all environments
///
/// **Purpose**: Read-only enumeration of environments in the workspace
///
/// This handler scans the data directory and extracts summary information
/// for each environment found. It handles partial failures gracefully,
/// continuing to list valid environments even when some fail to load.
///
/// ## Error Handling
///
/// - **Empty directory**: Returns empty list (not an error)
/// - **Per-environment errors**: Collected and reported, don't stop listing
/// - **Fatal errors**: Directory not found, permission denied
pub struct ListCommandHandler {
file_repository_factory: Arc<dyn RepositoryProvider>,
data_directory: Arc<Path>,
}
impl ListCommandHandler {
/// Create a new `ListCommandHandler`
#[must_use]
pub fn new(
file_repository_factory: Arc<dyn RepositoryProvider>,
data_directory: Arc<Path>,
) -> Self {
Self {
file_repository_factory,
data_directory,
}
}
/// Execute the list command workflow
///
/// Scans the data directory and extracts summary information for all
/// environments found.
///
/// # Returns
///
/// * `Ok(EnvironmentList)` - List of environment summaries (may include failures)
/// * `Err(ListCommandHandlerError)` - If the data directory cannot be accessed
///
/// # Errors
///
/// Returns an error if:
/// * Data directory does not exist
/// * Permission denied accessing data directory
#[instrument(
name = "list_command",
skip_all,
fields(
command_type = "list",
data_directory = %self.data_directory.display()
)
)]
pub fn execute(&self) -> Result<EnvironmentList, ListCommandHandlerError> {
// Verify data directory exists
if !self.data_directory.exists() {
return Err(ListCommandHandlerError::DataDirectoryNotFound {
path: self.data_directory.to_path_buf(),
});
}
// Scan for environment directories
let env_dirs = self.scan_environment_directories()?;
// Load summaries for each environment
let (summaries, failures) = self.load_environment_summaries(&env_dirs);
Ok(EnvironmentList::new(
summaries,
failures,
self.data_directory.to_string_lossy().to_string(),
))
}
/// Scan the data directory for environment subdirectories
fn scan_environment_directories(&self) -> Result<Vec<String>, ListCommandHandlerError> {
let entries = fs::read_dir(&self.data_directory).map_err(|e| {
if e.kind() == std::io::ErrorKind::PermissionDenied {
ListCommandHandlerError::PermissionDenied {
path: self.data_directory.to_path_buf(),
}
} else {
ListCommandHandlerError::ScanError {
message: e.to_string(),
}
}
})?;
let mut env_names = Vec::new();
for entry in entries {
let entry = match entry {
Ok(e) => e,
Err(e) => {
warn!("Failed to read directory entry: {e}");
continue;
}
};
// Only consider directories (environments are stored in subdirectories)
let path = entry.path();
if !path.is_dir() {
continue;
}
// Check if this directory contains an environment.json file
let env_file = path.join("environment.json");
if !env_file.exists() {
continue;
}
// Extract directory name as environment name
if let Some(name) = path.file_name().and_then(|n| n.to_str()) {
env_names.push(name.to_string());
}
}
Ok(env_names)
}
/// Load summaries for all discovered environments
///
/// Returns a tuple of (successful summaries, failed environments)
fn load_environment_summaries(
&self,
env_names: &[String],
) -> (Vec<EnvironmentSummary>, Vec<(String, String)>) {
let mut summaries = Vec::new();
let mut failures = Vec::new();
for name in env_names {
match self.load_environment_summary(name) {
Ok(summary) => summaries.push(summary),
Err(error) => {
warn!(
environment = %name,
error = %error,
"Failed to load environment"
);
failures.push((name.clone(), error));
}
}
}
(summaries, failures)
}
/// Load summary for a single environment
fn load_environment_summary(&self, name: &str) -> Result<EnvironmentSummary, String> {
// Validate environment name
let env_name = EnvironmentName::new(name.to_string())
.map_err(|e| format!("Invalid environment name: {e}"))?;
// Create repository for the base data directory
// (repository internally handles {base_dir}/{env_name}/environment.json)
let repository = self
.file_repository_factory
.create(self.data_directory.to_path_buf());
// Load environment from repository
let any_env = Self::load_environment(&repository, &env_name)?;
// Extract summary
Ok(Self::extract_summary(&any_env))
}
/// Load environment from repository
fn load_environment(
repository: &Arc<dyn EnvironmentRepository + Send + Sync>,
env_name: &EnvironmentName,
) -> Result<AnyEnvironmentState, String> {
repository
.load(env_name)
.map_err(|e| format!("Failed to load environment: {e}"))?
.ok_or_else(|| format!("Environment '{env_name}' not found in repository"))
}
/// Extract summary information from an environment
fn extract_summary(any_env: &AnyEnvironmentState) -> EnvironmentSummary {
let name = any_env.name().to_string();
let state = any_env.state_display_name().to_string();
let provider = any_env.provider_display_name().to_string();
let created_at = any_env.created_at().to_rfc3339();
EnvironmentSummary::new(name, state, provider, created_at)
}
}