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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
//! Validation execution logic and orchestration.
use anyhow::Result;
use colored::Colorize;
use std::path::PathBuf;
use crate::manifest::find_manifest_with_optional;
use super::command::{OutputFormat, ValidateCommand};
use super::results::ValidationResults;
use super::validators;
impl ValidateCommand {
/// Execute the validate command to check project configuration.
///
/// This method orchestrates the complete validation process, performing
/// checks according to the specified options and outputting results in
/// the requested format.
///
/// # Validation Process
///
/// 1. **Manifest Loading**: Locates and loads the manifest file
/// 2. **Basic Validation**: Checks syntax and required fields
/// 3. **Extended Checks**: Performs optional network and dependency checks
/// 4. **Result Compilation**: Aggregates all validation results
/// 5. **Output Generation**: Formats and displays results
/// 6. **Exit Code**: Returns success/failure based on results and strict mode
///
/// # Validation Ordering
///
/// Validations are performed in this order to provide early feedback:
/// 1. Manifest structure and syntax
/// 2. Dependency resolution (if `--resolve`)
/// 3. Source accessibility (if `--sources`)
/// 4. Local path validation (if `--paths`)
/// 5. Lockfile consistency (if `--check-lock`)
///
/// # Returns
///
/// - `Ok(())` if validation passes (or in strict mode, no warnings)
/// - `Err(anyhow::Error)` if:
/// - Manifest file is not found
/// - Manifest has syntax errors
/// - Critical validation failures occur
/// - Strict mode is enabled and warnings are present
///
/// # Examples
///
/// ```ignore
/// use agpm_cli::cli::validate::{ValidateCommand, OutputFormat};
///
/// let cmd = ValidateCommand {
/// file: None,
/// resolve: true,
/// check_lock: true,
/// sources: false,
/// paths: true,
/// format: OutputFormat::Text,
/// verbose: true,
/// quiet: false,
/// strict: false,
/// render: false,
/// };
/// // cmd.execute().await?;
/// ```
pub async fn execute(self) -> Result<()> {
self.execute_with_manifest_path(None).await
}
/// Execute the validate command with an optional manifest path.
///
/// This method performs validation of the agpm.toml manifest file and optionally
/// the associated lockfile. It can validate manifest syntax, source availability,
/// and dependency resolution consistency.
///
/// # Arguments
///
/// * `manifest_path` - Optional path to the agpm.toml file. If None, searches
/// for agpm.toml in current directory and parent directories. If the command
/// has a `file` field set, that takes precedence.
///
/// # Returns
///
/// - `Ok(())` if validation passes
/// - `Err(anyhow::Error)` if validation fails or manifest is invalid
///
/// # Examples
///
/// ```ignore
/// use agpm_cli::cli::validate::ValidateCommand;
/// use std::path::PathBuf;
///
/// let cmd = ValidateCommand {
/// file: None,
/// check_lock: false,
/// resolve: false,
/// format: OutputFormat::Text,
/// json: false,
/// paths: false,
/// fix: false,
/// };
///
/// cmd.execute_with_manifest_path(Some(PathBuf::from("./agpm.toml"))).await?;
/// ```
pub async fn execute_with_manifest_path(self, manifest_path: Option<PathBuf>) -> Result<()> {
// Find or use specified manifest file
let manifest_path = if let Some(ref path) = self.file {
PathBuf::from(path)
} else {
match find_manifest_with_optional(manifest_path) {
Ok(path) => path,
Err(e) => {
let error_msg =
"No agpm.toml found in current directory or any parent directory";
if matches!(self.format, OutputFormat::Json) {
let validation_results = ValidationResults {
valid: false,
errors: vec![error_msg.to_string()],
..Default::default()
};
println!("{}", serde_json::to_string_pretty(&validation_results)?);
return Err(e);
} else if !self.quiet {
println!("{} {}", "✗".red(), error_msg);
}
return Err(e);
}
}
};
self.execute_from_path(manifest_path).await
}
/// Executes validation using a specific manifest path
///
/// This method performs the same validation as `execute()` but accepts
/// an explicit manifest path instead of searching for it.
///
/// # Arguments
///
/// * `manifest_path` - Path to the manifest file to validate
///
/// # Returns
///
/// Returns `Ok(())` if validation succeeds
///
/// # Errors
///
/// Returns an error if:
/// - The manifest file doesn't exist
/// - The manifest has syntax errors
/// - Sources are invalid or unreachable (with --resolve flag)
/// - Dependencies have conflicts
pub async fn execute_from_path(self, manifest_path: PathBuf) -> Result<()> {
// For consistency with execute(), require the manifest to exist
if !manifest_path.exists() {
let error_msg = format!("Manifest file {} not found", manifest_path.display());
if matches!(self.format, OutputFormat::Json) {
let validation_results = ValidationResults {
valid: false,
errors: vec![error_msg],
..Default::default()
};
println!("{}", serde_json::to_string_pretty(&validation_results)?);
} else if !self.quiet {
println!("{} {}", "✗".red(), error_msg);
}
return Err(anyhow::anyhow!("Manifest file {} not found", manifest_path.display()));
}
// Validation results for JSON output
let mut validation_results = ValidationResults::default();
let mut warnings = Vec::new();
let mut errors = Vec::new();
// Load and validate manifest structure
let manifest = validators::validate_manifest(
&manifest_path,
&self.format,
self.verbose,
self.quiet,
&mut validation_results,
&mut warnings,
&mut errors,
)
.await?;
// Check if dependencies can be resolved
if self.resolve {
validators::validate_dependencies(
&manifest,
&self.format,
self.verbose,
self.quiet,
&mut validation_results,
&mut warnings,
&mut errors,
)
.await?;
}
// Check if sources are accessible
if self.sources {
validators::validate_sources(
&manifest,
&self.format,
self.verbose,
self.quiet,
&mut validation_results,
&mut warnings,
&mut errors,
)
.await?;
}
// Check local file paths
if self.paths {
let mut ctx = validators::ValidationContext::new(
&manifest,
&self.format,
self.verbose,
self.quiet,
&mut validation_results,
&mut warnings,
&mut errors,
);
validators::validate_paths(&mut ctx, &manifest_path).await?;
}
// Check lockfile consistency
if self.check_lock {
let project_dir = manifest_path.parent().unwrap();
let mut ctx = validators::ValidationContext::new(
&manifest,
&self.format,
self.verbose,
self.quiet,
&mut validation_results,
&mut warnings,
&mut errors,
);
validators::validate_lockfile(&mut ctx, project_dir).await?;
}
// Validate template rendering if requested
if self.render {
let project_dir = manifest_path.parent().unwrap();
let mut ctx = validators::ValidationContext::new(
&manifest,
&self.format,
self.verbose,
self.quiet,
&mut validation_results,
&mut warnings,
&mut errors,
);
validators::validate_templates(&mut ctx, project_dir).await?;
}
// Handle strict mode - treat warnings as errors
if self.strict && !warnings.is_empty() {
let error_msg = "Strict mode: Warnings treated as errors";
errors.extend(warnings.clone());
if matches!(self.format, OutputFormat::Json) {
validation_results.valid = false;
validation_results.errors = errors;
println!("{}", serde_json::to_string_pretty(&validation_results)?);
return Err(anyhow::anyhow!("Strict mode validation failed"));
} else if !self.quiet {
println!("{} {}", "✗".red(), error_msg);
}
return Err(anyhow::anyhow!("Strict mode validation failed"));
}
// Set final validation status
validation_results.valid = errors.is_empty();
validation_results.errors = errors;
validation_results.warnings = warnings;
// Output results
match self.format {
OutputFormat::Json => {
println!("{}", serde_json::to_string_pretty(&validation_results)?);
}
OutputFormat::Text => {
if !self.quiet && !validation_results.warnings.is_empty() {
for warning in &validation_results.warnings {
println!("⚠ Warning: {warning}");
}
}
// Individual validation steps already printed their success messages
}
}
Ok(())
}
}