|
12 | 12 |
|
13 | 13 | use std::process; |
14 | 14 |
|
15 | | -use clap::{Parser, Subcommand}; |
16 | | -use torrust_dependency_installer::{Dependency, DependencyManager}; |
17 | | -use tracing::{error, info}; |
18 | | - |
19 | | -/// Manage development dependencies for E2E tests |
20 | | -#[derive(Parser)] |
21 | | -#[command(name = "dependency-installer")] |
22 | | -#[command(version)] |
23 | | -#[command(about = "Manage development dependencies for E2E tests", long_about = None)] |
24 | | -struct Cli { |
25 | | - #[command(subcommand)] |
26 | | - command: Commands, |
27 | | - |
28 | | - /// Enable verbose output |
29 | | - #[arg(short, long, global = true)] |
30 | | - verbose: bool, |
31 | | -} |
32 | | - |
33 | | -#[derive(Subcommand)] |
34 | | -enum Commands { |
35 | | - /// Check if dependencies are installed |
36 | | - Check { |
37 | | - /// Specific tool to check (if omitted, checks all) |
38 | | - #[arg(short, long)] |
39 | | - tool: Option<String>, |
40 | | - }, |
41 | | - |
42 | | - /// List all available tools and their status |
43 | | - List, |
44 | | -} |
| 15 | +use torrust_dependency_installer::app; |
45 | 16 |
|
46 | 17 | fn main() { |
47 | | - let exit_code = match run() { |
| 18 | + let exit_code = match app::run() { |
48 | 19 | Ok(()) => 0, |
49 | 20 | Err(e) => { |
50 | 21 | eprintln!("Error: {e}"); |
51 | | - |
52 | | - // Determine exit code based on error type |
53 | | - let error_msg = e.to_string(); |
54 | | - if error_msg.contains("not installed") || error_msg.contains("Missing") { |
55 | | - 1 // Missing dependency |
56 | | - } else if error_msg.contains("Unknown tool") || error_msg.contains("invalid") { |
57 | | - 2 // Invalid argument |
58 | | - } else { |
59 | | - 3 // Internal error |
60 | | - } |
| 22 | + error_to_exit_code(e.as_ref()) |
61 | 23 | } |
62 | 24 | }; |
63 | 25 |
|
64 | 26 | process::exit(exit_code); |
65 | 27 | } |
66 | 28 |
|
67 | | -fn run() -> Result<(), Box<dyn std::error::Error>> { |
68 | | - let cli = Cli::parse(); |
69 | | - |
70 | | - // Initialize tracing based on verbose flag |
71 | | - // Must set environment variable before calling init_tracing() |
72 | | - if cli.verbose { |
73 | | - std::env::set_var("RUST_LOG", "debug"); |
74 | | - } |
75 | | - torrust_dependency_installer::init_tracing(); |
76 | | - |
77 | | - let manager = DependencyManager::new(); |
78 | | - |
79 | | - match cli.command { |
80 | | - Commands::Check { tool } => handle_check(&manager, tool), |
81 | | - Commands::List => handle_list(&manager), |
82 | | - } |
83 | | -} |
84 | | - |
85 | | -fn handle_check( |
86 | | - manager: &DependencyManager, |
87 | | - tool: Option<String>, |
88 | | -) -> Result<(), Box<dyn std::error::Error>> { |
89 | | - match tool { |
90 | | - Some(tool_name) => check_specific_tool(manager, &tool_name), |
91 | | - None => check_all_tools(manager), |
92 | | - } |
93 | | -} |
94 | | - |
95 | | -fn check_all_tools(manager: &DependencyManager) -> Result<(), Box<dyn std::error::Error>> { |
96 | | - info!("Checking all dependencies"); |
97 | | - println!("Checking dependencies...\n"); |
98 | | - |
99 | | - let results = manager.check_all()?; |
100 | | - let mut missing_count = 0; |
101 | | - |
102 | | - for result in &results { |
103 | | - if result.installed { |
104 | | - println!("✓ {}: installed", result.tool); |
105 | | - } else { |
106 | | - println!("✗ {}: not installed", result.tool); |
107 | | - missing_count += 1; |
108 | | - } |
109 | | - } |
110 | | - |
111 | | - println!(); |
112 | | - if missing_count > 0 { |
113 | | - let msg = format!( |
114 | | - "Missing {missing_count} out of {} required dependencies", |
115 | | - results.len() |
116 | | - ); |
117 | | - error!("{}", msg); |
118 | | - eprintln!("{msg}"); |
119 | | - Err(msg.into()) |
120 | | - } else { |
121 | | - info!("All dependencies are installed"); |
122 | | - println!("All dependencies are installed"); |
123 | | - Ok(()) |
124 | | - } |
125 | | -} |
126 | | - |
127 | | -fn check_specific_tool( |
128 | | - manager: &DependencyManager, |
129 | | - tool_name: &str, |
130 | | -) -> Result<(), Box<dyn std::error::Error>> { |
131 | | - info!(tool = tool_name, "Checking specific tool"); |
132 | | - |
133 | | - // Parse tool name to Dependency enum |
134 | | - let dep = parse_tool_name(tool_name)?; |
135 | | - let detector = manager.get_detector(dep); |
136 | | - |
137 | | - let installed = detector.is_installed()?; |
138 | | - |
139 | | - if installed { |
140 | | - info!(tool = detector.name(), "Tool is installed"); |
141 | | - println!("✓ {}: installed", detector.name()); |
142 | | - Ok(()) |
| 29 | +/// Determine exit code based on error type |
| 30 | +/// |
| 31 | +/// # Exit Codes |
| 32 | +/// |
| 33 | +/// - 1: Missing dependencies (contains "not installed" or "Missing") |
| 34 | +/// - 2: Invalid arguments (contains "Unknown tool" or "invalid") |
| 35 | +/// - 3: Internal error (all other errors) |
| 36 | +fn error_to_exit_code(error: &dyn std::error::Error) -> i32 { |
| 37 | + let error_msg = error.to_string(); |
| 38 | + if error_msg.contains("not installed") || error_msg.contains("Missing") { |
| 39 | + 1 // Missing dependency |
| 40 | + } else if error_msg.contains("Unknown tool") || error_msg.contains("invalid") { |
| 41 | + 2 // Invalid argument |
143 | 42 | } else { |
144 | | - let msg = format!("{}: not installed", detector.name()); |
145 | | - error!(tool = detector.name(), "Tool is not installed"); |
146 | | - eprintln!("✗ {msg}"); |
147 | | - Err(msg.into()) |
148 | | - } |
149 | | -} |
150 | | - |
151 | | -fn handle_list(manager: &DependencyManager) -> Result<(), Box<dyn std::error::Error>> { |
152 | | - info!("Listing all available tools"); |
153 | | - println!("Available tools:\n"); |
154 | | - |
155 | | - let results = manager.check_all()?; |
156 | | - for result in results { |
157 | | - let status = if result.installed { |
158 | | - "installed" |
159 | | - } else { |
160 | | - "not installed" |
161 | | - }; |
162 | | - println!("- {} ({status})", result.tool); |
163 | | - } |
164 | | - |
165 | | - Ok(()) |
166 | | -} |
167 | | - |
168 | | -fn parse_tool_name(name: &str) -> Result<Dependency, String> { |
169 | | - match name.to_lowercase().as_str() { |
170 | | - "cargo-machete" | "machete" => Ok(Dependency::CargoMachete), |
171 | | - "opentofu" | "tofu" => Ok(Dependency::OpenTofu), |
172 | | - "ansible" => Ok(Dependency::Ansible), |
173 | | - "lxd" => Ok(Dependency::Lxd), |
174 | | - _ => { |
175 | | - // List of available tools - should be kept in sync with the match arms above |
176 | | - const AVAILABLE_TOOLS: &str = "cargo-machete, opentofu, ansible, lxd"; |
177 | | - Err(format!( |
178 | | - "Unknown tool: {name}. Available: {AVAILABLE_TOOLS}" |
179 | | - )) |
180 | | - } |
| 43 | + 3 // Internal error |
181 | 44 | } |
182 | 45 | } |
0 commit comments