qualia_cli/handlers/
app.rs1use crate::cli::AppAction;
2use qualia_client_core::qapp_install::{
3 install_package_atomic, load_install_registry, reconcile_registry_with_disk, uninstall_package,
4 InstallPolicy,
5};
6use qualia_client_core::qapp_paths::{qapps_dir, resolve_active_package_dir};
7use qualia_client_core::qapp_registry::{QappPackageManifest, QAPP_PACKAGE_MANIFEST};
8use std::fs;
9use std::path::{Path, PathBuf};
10
11fn resolve_storage_path(custom: Option<PathBuf>) -> PathBuf {
12 custom.unwrap_or_else(|| PathBuf::from(qualia_client_core::state::dirs_default_path()))
13}
14
15pub fn handle(action: &AppAction) -> Result<(), Box<dyn std::error::Error>> {
17 match action {
18 AppAction::List { storage } => {
19 let storage = resolve_storage_path(storage.clone());
20 let registry = load_install_registry(&storage).map_err(|e| {
21 format!(
22 "Failed to read QApp registry from {}: {e}",
23 storage.display()
24 )
25 })?;
26 let qdir = qapps_dir(&storage);
27
28 if registry.packages.is_empty() {
29 println!("No QApps currently installed in {}", qdir.display());
30 return Ok(());
31 }
32
33 println!("Installed QApps in {}:", qdir.display());
34 println!(
35 "{:<32} {:<12} {:<24} {:<8}",
36 "PACKAGE ID", "VERSION", "INSTALLED AT", "REVOKED"
37 );
38 println!("{:-<78}", "");
39
40 for (id, entry) in ®istry.packages {
41 let date_str = if entry.installed_at_unix > 0 {
42 chrono::DateTime::from_timestamp(entry.installed_at_unix as i64, 0)
43 .map(|dt| dt.format("%Y-%m-%d %H:%M").to_string())
44 .unwrap_or_else(|| "unknown".into())
45 } else {
46 "bundled/legacy".into()
47 };
48
49 let revoked_str = if entry.revoked { "YES" } else { "no" };
50 println!(
51 "{:<32} {:<12} {:<24} {:<8}",
52 id, entry.active_version, date_str, revoked_str
53 );
54 }
55 Ok(())
56 }
57 AppAction::Install {
58 source,
59 storage,
60 production,
61 } => {
62 let storage = resolve_storage_path(storage.clone());
63 let policy = if *production {
64 InstallPolicy::Production
65 } else {
66 InstallPolicy::Development
67 };
68
69 println!(
70 "Installing QApp from {} (policy: {:?})...",
71 source.display(),
72 policy
73 );
74
75 let entry = install_package_atomic(&storage, source, policy, None)
76 .map_err(|e| format!("Installation failed: {e}"))?;
77
78 println!(
79 "Successfully installed '{}' v{} (content hash: {})",
80 entry.package_id, entry.active_version, entry.content_hash
81 );
82 println!(
83 "Active directory: {}",
84 resolve_active_package_dir(&storage, &entry.package_id).display()
85 );
86 Ok(())
87 }
88 AppAction::Uninstall {
89 package_id,
90 storage,
91 purge_data,
92 } => {
93 let storage = resolve_storage_path(storage.clone());
94 println!("Uninstalling QApp '{}'...", package_id);
95
96 uninstall_package(&storage, package_id, *purge_data)
97 .map_err(|e| format!("Uninstall failed: {e}"))?;
98
99 println!("Successfully uninstalled '{}'.", package_id);
100 if *purge_data {
101 println!("Application data directory purged.");
102 }
103 Ok(())
104 }
105 AppAction::Info { target, storage } => {
106 let storage = resolve_storage_path(storage.clone());
107 let manifest_path = if Path::new(target).join(QAPP_PACKAGE_MANIFEST).is_file() {
108 Path::new(target).join(QAPP_PACKAGE_MANIFEST)
109 } else {
110 let active = resolve_active_package_dir(&storage, target);
111 active.join(QAPP_PACKAGE_MANIFEST)
112 };
113
114 if !manifest_path.is_file() {
115 return Err(format!(
116 "QApp manifest not found for '{}' (checked: {})",
117 target,
118 manifest_path.display()
119 )
120 .into());
121 }
122
123 let content = fs::read_to_string(&manifest_path)?;
124 let manifest: QappPackageManifest = serde_json::from_str(&content)
125 .map_err(|e| format!("Failed to parse {}: {e}", manifest_path.display()))?;
126
127 println!("QApp Information: {}", manifest.name);
128 println!(" Version: {}", manifest.version);
129 if let Some(port) = manifest.dev_port {
130 println!(" Dev Port: {}", port);
131 }
132 if !manifest.required_shapes.is_empty() {
133 println!(" Required Shapes: {}", manifest.required_shapes.join(", "));
134 }
135 if let Some(ext) = manifest.x_qualia {
136 println!(" App ID: {}", ext.app_id);
137 println!(" Display Name: {}", ext.display_name);
138 println!(
139 " Category: {}",
140 if ext.category.is_empty() {
141 "standard"
142 } else {
143 &ext.category
144 }
145 );
146 if !ext.launch_modes.is_empty() {
147 println!(" Launch Modes: {}", ext.launch_modes.join(", "));
148 }
149 if !ext.entrypoints.is_empty() {
150 println!(" Entrypoints:");
151 for (k, v) in ext.entrypoints {
152 println!(" - {}: {}", k, v);
153 }
154 }
155 if let Some(wasm) = ext.wasm {
156 println!(" WASM Engine: {}", wasm.engine_package);
157 }
158 }
159 Ok(())
160 }
161 AppAction::Reconcile { storage } => {
162 let storage = resolve_storage_path(storage.clone());
163 println!("Reconciling registry with disk at {}...", storage.display());
164 let registry = reconcile_registry_with_disk(&storage)
165 .map_err(|e| format!("Reconciliation failed: {e}"))?;
166 println!(
167 "Reconciled {} registered package(s).",
168 registry.packages.len()
169 );
170 Ok(())
171 }
172 }
173}