mockforge-cli 0.3.108

CLI interface for MockForge
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
//! Template Library Commands
//!
//! Commands for managing the template library, including:
//! - Registering templates
//! - Searching templates
//! - Installing from marketplace
//! - Managing template versions

use clap::Subcommand;
use mockforge_core::{
    template_library::{TemplateLibraryManager, TemplateMarketplace, TemplateMetadata},
    Error, Result,
};
use std::path::PathBuf;
use tracing::info;

#[derive(Subcommand, Debug, Clone)]
pub enum TemplateCommands {
    /// Register a new template in the library
    Register {
        /// Template ID (unique identifier)
        #[arg(long)]
        id: String,
        /// Template name
        #[arg(long)]
        name: String,
        /// Template description
        #[arg(long)]
        description: Option<String>,
        /// Template version (semver format)
        #[arg(long, default_value = "1.0.0")]
        version: String,
        /// Template author
        #[arg(long)]
        author: Option<String>,
        /// Template tags (comma-separated)
        #[arg(long, value_delimiter = ',')]
        tags: Vec<String>,
        /// Template category
        #[arg(long)]
        category: Option<String>,
        /// Template content (the actual template string)
        #[arg(long)]
        content: String,
        /// Example usage
        #[arg(long)]
        example: Option<String>,
        /// Dependencies (comma-separated template IDs)
        #[arg(long, value_delimiter = ',')]
        dependencies: Vec<String>,
        /// Storage directory for templates
        #[arg(long)]
        storage_dir: Option<PathBuf>,
    },
    /// List templates in the library
    List {
        /// Filter by category
        #[arg(long)]
        category: Option<String>,
        /// Search query
        #[arg(long)]
        search: Option<String>,
        /// Storage directory for templates
        #[arg(long)]
        storage_dir: Option<PathBuf>,
    },
    /// Get a template by ID
    Get {
        /// Template ID
        id: String,
        /// Specific version (defaults to latest)
        #[arg(long)]
        version: Option<String>,
        /// Storage directory for templates
        #[arg(long)]
        storage_dir: Option<PathBuf>,
    },
    /// Remove a template or version
    Remove {
        /// Template ID
        id: String,
        /// Specific version to remove (removes entire template if not specified)
        #[arg(long)]
        version: Option<String>,
        /// Storage directory for templates
        #[arg(long)]
        storage_dir: Option<PathBuf>,
    },
    /// Search templates
    Search {
        /// Search query
        query: String,
        /// Storage directory for templates
        #[arg(long)]
        storage_dir: Option<PathBuf>,
    },
    /// Install a template from marketplace
    Install {
        /// Template ID
        id: String,
        /// Specific version to install (defaults to latest)
        #[arg(long)]
        version: Option<String>,
        /// Marketplace registry URL
        #[arg(long)]
        registry_url: String,
        /// Authentication token
        #[arg(long)]
        auth_token: Option<String>,
        /// Storage directory for templates
        #[arg(long)]
        storage_dir: Option<PathBuf>,
    },
    /// Marketplace operations
    Marketplace {
        /// Marketplace registry URL
        #[arg(long)]
        registry_url: String,
        /// Authentication token
        #[arg(long)]
        auth_token: Option<String>,
        #[command(subcommand)]
        command: MarketplaceCommands,
    },
}

#[derive(Subcommand, Debug, Clone)]
pub enum MarketplaceCommands {
    /// Search templates in marketplace
    Search {
        /// Search query
        query: String,
    },
    /// List featured templates
    Featured,
    /// List templates by category
    Category {
        /// Category name
        category: String,
    },
    /// Get a template from marketplace
    Get {
        /// Template ID
        id: String,
        /// Specific version (defaults to latest)
        #[arg(long)]
        version: Option<String>,
    },
}

/// Handle template library commands
pub async fn handle_template_command(command: TemplateCommands) -> Result<()> {
    // Extract storage_dir from any command variant
    let storage_dir = match &command {
        TemplateCommands::Register { storage_dir, .. } => storage_dir.clone(),
        TemplateCommands::List { storage_dir, .. } => storage_dir.clone(),
        TemplateCommands::Get { storage_dir, .. } => storage_dir.clone(),
        TemplateCommands::Remove { storage_dir, .. } => storage_dir.clone(),
        TemplateCommands::Search { storage_dir, .. } => storage_dir.clone(),
        TemplateCommands::Install { storage_dir, .. } => storage_dir.clone(),
        TemplateCommands::Marketplace { .. } => None,
    };

    let storage = storage_dir.unwrap_or_else(|| PathBuf::from("./.mockforge-templates"));
    let mut manager = TemplateLibraryManager::new(&storage)?;

    match command {
        TemplateCommands::Register {
            id,
            name,
            description,
            version,
            author,
            tags,
            category,
            content,
            example,
            dependencies,
            ..
        } => {
            handle_register_template(
                &mut manager,
                id,
                name,
                description,
                version,
                author,
                tags,
                category,
                content,
                example,
                dependencies,
            )
            .await
        }
        TemplateCommands::List {
            category, search, ..
        } => handle_list_templates(&manager, category, search).await,
        TemplateCommands::Get { id, version, .. } => {
            handle_get_template(&manager, id, version).await
        }
        TemplateCommands::Remove { id, version, .. } => {
            handle_remove_template(&mut manager, id, version).await
        }
        TemplateCommands::Search { query, .. } => handle_search_templates(&manager, query).await,
        TemplateCommands::Install {
            id,
            version,
            registry_url,
            auth_token,
            ..
        } => handle_install_template(&mut manager, id, version, registry_url, auth_token).await,
        TemplateCommands::Marketplace {
            registry_url,
            auth_token,
            command: marketplace_cmd,
            ..
        } => handle_marketplace_command(registry_url, auth_token, marketplace_cmd).await,
    }
}

#[allow(clippy::too_many_arguments)]
async fn handle_register_template(
    manager: &mut TemplateLibraryManager,
    id: String,
    name: String,
    description: Option<String>,
    version: String,
    author: Option<String>,
    tags: Vec<String>,
    category: Option<String>,
    content: String,
    example: Option<String>,
    dependencies: Vec<String>,
) -> Result<()> {
    let metadata = TemplateMetadata {
        id,
        name,
        description,
        version,
        author,
        tags,
        category,
        content,
        example,
        dependencies,
        created_at: None,
        updated_at: None,
    };

    manager.library_mut().register_template(metadata)?;
    info!("Template registered successfully");
    Ok(())
}

async fn handle_list_templates(
    manager: &TemplateLibraryManager,
    category: Option<String>,
    search: Option<String>,
) -> Result<()> {
    let templates = if let Some(ref category) = category {
        manager.library().templates_by_category(category)
    } else if let Some(ref query) = search {
        manager.library().search_templates(query)
    } else {
        manager.library().list_templates()
    };

    if templates.is_empty() {
        println!("No templates found.");
        return Ok(());
    }

    println!("Found {} template(s):\n", templates.len());
    for template in templates {
        println!("ID: {}", template.id);
        println!("Name: {}", template.name);
        if let Some(ref desc) = template.description {
            println!("Description: {}", desc);
        }
        println!("Version: {}", template.latest_version);
        if let Some(ref author) = template.author {
            println!("Author: {}", author);
        }
        if !template.tags.is_empty() {
            println!("Tags: {}", template.tags.join(", "));
        }
        if let Some(ref category) = template.category {
            println!("Category: {}", category);
        }
        println!("Versions: {}", template.versions.len());
        println!();
    }

    Ok(())
}

async fn handle_get_template(
    manager: &TemplateLibraryManager,
    id: String,
    version: Option<String>,
) -> Result<()> {
    let version_clone = version.clone();
    let content = if let Some(ref version) = version {
        manager.library().get_template_version(&id, version)
    } else {
        manager.library().get_latest_template(&id)
    };

    match content {
        Some(content) => {
            println!("Template: {}", id);
            if let Some(ref version) = version_clone {
                println!("Version: {}", version);
            }
            println!("\nContent:\n{}", content);
            Ok(())
        }
        None => Err(Error::internal(format!("Template '{}' not found", id))),
    }
}

async fn handle_remove_template(
    manager: &mut TemplateLibraryManager,
    id: String,
    version: Option<String>,
) -> Result<()> {
    if let Some(version) = version {
        manager.library_mut().remove_template_version(&id, &version)?;
        info!("Removed version {} of template {}", version, id);
    } else {
        manager.library_mut().remove_template(&id)?;
        info!("Removed template {}", id);
    }
    Ok(())
}

async fn handle_search_templates(manager: &TemplateLibraryManager, query: String) -> Result<()> {
    let templates = manager.library().search_templates(&query);

    if templates.is_empty() {
        println!("No templates found matching '{}'", query);
        return Ok(());
    }

    println!("Found {} template(s) matching '{}':\n", templates.len(), query);
    for template in templates {
        println!("- {} ({})", template.name, template.id);
        if let Some(ref desc) = template.description {
            println!("  {}", desc);
        }
    }

    Ok(())
}

async fn handle_install_template(
    manager: &mut TemplateLibraryManager,
    id: String,
    version: Option<String>,
    registry_url: String,
    auth_token: Option<String>,
) -> Result<()> {
    // Take ownership, configure marketplace, then put it back
    let mut temp_manager =
        std::mem::replace(manager, TemplateLibraryManager::new(manager.library().storage_dir())?);
    temp_manager = temp_manager.with_marketplace(registry_url, auth_token);
    temp_manager.install_from_marketplace(&id, version.as_deref()).await?;
    *manager = temp_manager;
    info!("Template '{}' installed successfully", id);
    Ok(())
}

async fn handle_marketplace_command(
    registry_url: String,
    auth_token: Option<String>,
    command: MarketplaceCommands,
) -> Result<()> {
    let marketplace = TemplateMarketplace::new(registry_url, auth_token);

    match command {
        MarketplaceCommands::Search { query } => {
            let templates = marketplace.search(&query).await?;
            println!("Found {} template(s) in marketplace:\n", templates.len());
            for template in templates {
                println!("- {} ({}) - {}", template.name, template.id, template.latest_version);
                if let Some(ref desc) = template.description {
                    println!("  {}", desc);
                }
            }
        }
        MarketplaceCommands::Featured => {
            let templates = marketplace.list_featured().await?;
            println!("Featured templates:\n");
            for template in templates {
                println!("- {} ({}) - {}", template.name, template.id, template.latest_version);
                if let Some(ref desc) = template.description {
                    println!("  {}", desc);
                }
            }
        }
        MarketplaceCommands::Category { category } => {
            let templates = marketplace.list_by_category(&category).await?;
            println!("Templates in category '{}':\n", category);
            for template in templates {
                println!("- {} ({}) - {}", template.name, template.id, template.latest_version);
            }
        }
        MarketplaceCommands::Get { id, version } => {
            let template = marketplace.get_template(&id, version.as_deref()).await?;
            println!("Template: {}", template.name);
            println!("ID: {}", template.id);
            println!("Version: {}", template.latest_version);
            if let Some(ref desc) = template.description {
                println!("Description: {}", desc);
            }
            if let Some(ref author) = template.author {
                println!("Author: {}", author);
            }
            println!(
                "\nContent:\n{}",
                template.versions.first().map(|v| &v.content).unwrap_or(&String::new())
            );
        }
    }

    Ok(())
}