mielin-cli 0.1.0-rc.1

Command-line interface and control plane for MielinOS distributed agent mesh
Documentation
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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
//! Plugin management commands

use crate::output::{render_output, MultiFormatDisplay, OutputFormat};
use crate::plugin::PluginManager;
use anyhow::Result;
use clap::Subcommand;
use comfy_table::{presets::UTF8_FULL, Cell, Color, ContentArrangement, Table};
use serde::Serialize;
use std::collections::HashMap;
use std::path::{Path, PathBuf};

#[derive(Subcommand)]
pub enum PluginCommands {
    /// List all installed plugins
    #[command(visible_aliases = &["ls"])]
    List,

    /// Show plugin information
    #[command(visible_aliases = &["show", "details"])]
    Info {
        /// Plugin name
        name: String,
    },

    /// Install a plugin from a directory
    #[command(visible_aliases = &["add"])]
    Install {
        /// Path to plugin directory
        path: PathBuf,
    },

    /// Uninstall a plugin
    #[command(visible_aliases = &["remove", "rm"])]
    Uninstall {
        /// Plugin name
        name: String,

        /// Skip confirmation
        #[arg(short = 'y', long)]
        yes: bool,
    },

    /// Enable a plugin
    Enable {
        /// Plugin name
        name: String,
    },

    /// Disable a plugin
    Disable {
        /// Plugin name
        name: String,
    },

    /// Execute a plugin command
    #[command(visible_aliases = &["run", "exec"])]
    Execute {
        /// Plugin name
        plugin: String,

        /// Command name
        command: String,

        /// Arguments as KEY=VALUE pairs
        #[arg(trailing_var_arg = true, allow_hyphen_values = true)]
        args: Vec<String>,
    },

    /// Reload all plugins
    Reload,

    /// Show plugin directory path
    #[command(visible_aliases = &["dir"])]
    Directory,
}

pub async fn handle_plugin_command(cmd: PluginCommands, format: OutputFormat) -> Result<()> {
    match cmd {
        PluginCommands::List => list_plugins(format).await,
        PluginCommands::Info { name } => show_plugin_info(&name, format).await,
        PluginCommands::Install { path } => install_plugin(&path, format).await,
        PluginCommands::Uninstall { name, yes } => uninstall_plugin(&name, yes, format).await,
        PluginCommands::Enable { name } => enable_plugin(&name, format).await,
        PluginCommands::Disable { name } => disable_plugin(&name, format).await,
        PluginCommands::Execute {
            plugin,
            command,
            args,
        } => execute_plugin_command(&plugin, &command, args, format).await,
        PluginCommands::Reload => reload_plugins(format).await,
        PluginCommands::Directory => show_plugin_directory(format).await,
    }
}

#[derive(Debug, Serialize)]
struct PluginListEntry {
    name: String,
    version: String,
    description: String,
    enabled: String,
    commands: usize,
}

impl MultiFormatDisplay for Vec<PluginListEntry> {
    fn to_table(&self) -> Table {
        let mut table = Table::new();
        table
            .load_preset(UTF8_FULL)
            .set_content_arrangement(ContentArrangement::Dynamic);

        table.set_header(vec![
            Cell::new("Name").fg(Color::Cyan),
            Cell::new("Version").fg(Color::Cyan),
            Cell::new("Description").fg(Color::Cyan),
            Cell::new("Enabled").fg(Color::Cyan),
            Cell::new("Commands").fg(Color::Cyan),
        ]);

        for entry in self {
            let enabled_cell = if entry.enabled == "Yes" {
                Cell::new(&entry.enabled).fg(Color::Green)
            } else {
                Cell::new(&entry.enabled).fg(Color::Red)
            };

            table.add_row(vec![
                Cell::new(&entry.name),
                Cell::new(&entry.version),
                Cell::new(&entry.description),
                enabled_cell,
                Cell::new(entry.commands.to_string()),
            ]);
        }

        table
    }

    fn to_quiet(&self) -> String {
        self.iter()
            .map(|e| e.name.clone())
            .collect::<Vec<_>>()
            .join("\n")
    }
}

async fn list_plugins(format: OutputFormat) -> Result<()> {
    let mut manager = PluginManager::new()?;
    manager.discover_plugins()?;

    let plugins = manager.list_plugins();
    let entries: Vec<PluginListEntry> = plugins
        .iter()
        .map(|p| PluginListEntry {
            name: p.metadata.name.clone(),
            version: p.metadata.version.clone(),
            description: p.metadata.description.clone(),
            enabled: if p.enabled { "Yes" } else { "No" }.to_string(),
            commands: p.metadata.commands.len(),
        })
        .collect();

    println!("{}", render_output(&entries, format)?);
    Ok(())
}

#[derive(Debug, Serialize)]
struct PluginInfoDisplay {
    name: String,
    version: String,
    description: String,
    author: String,
    license: String,
    enabled: String,
    commands: Vec<CommandInfo>,
    dependencies: Vec<String>,
    min_version: String,
}

#[derive(Debug, Serialize)]
struct CommandInfo {
    name: String,
    description: String,
    aliases: String,
    arguments: usize,
}

impl MultiFormatDisplay for PluginInfoDisplay {
    fn to_table(&self) -> Table {
        let mut table = Table::new();
        table
            .load_preset(UTF8_FULL)
            .set_content_arrangement(ContentArrangement::Dynamic);

        table.add_row(vec![
            Cell::new("Name").fg(Color::Cyan),
            Cell::new(&self.name),
        ]);
        table.add_row(vec![
            Cell::new("Version").fg(Color::Cyan),
            Cell::new(&self.version),
        ]);
        table.add_row(vec![
            Cell::new("Description").fg(Color::Cyan),
            Cell::new(&self.description),
        ]);
        table.add_row(vec![
            Cell::new("Author").fg(Color::Cyan),
            Cell::new(&self.author),
        ]);
        table.add_row(vec![
            Cell::new("License").fg(Color::Cyan),
            Cell::new(&self.license),
        ]);
        table.add_row(vec![
            Cell::new("Enabled").fg(Color::Cyan),
            Cell::new(&self.enabled),
        ]);
        table.add_row(vec![
            Cell::new("Commands").fg(Color::Cyan),
            Cell::new(self.commands.len().to_string()),
        ]);

        if !self.commands.is_empty() {
            table.add_row(vec![Cell::new("").fg(Color::Cyan), Cell::new("")]);
            table.add_row(vec![
                Cell::new("Available Commands")
                    .fg(Color::Yellow)
                    .add_attribute(comfy_table::Attribute::Bold),
                Cell::new(""),
            ]);
            for cmd in &self.commands {
                table.add_row(vec![
                    Cell::new(format!("  {}", cmd.name)),
                    Cell::new(&cmd.description),
                ]);
            }
        }

        table
    }

    fn to_quiet(&self) -> String {
        format!("{} v{}", self.name, self.version)
    }
}

async fn show_plugin_info(name: &str, format: OutputFormat) -> Result<()> {
    let mut manager = PluginManager::new()?;
    manager.discover_plugins()?;

    let plugin = manager
        .get_plugin(name)
        .ok_or_else(|| anyhow::anyhow!("Plugin not found: {}", name))?;

    let info = PluginInfoDisplay {
        name: plugin.metadata.name.clone(),
        version: plugin.metadata.version.clone(),
        description: plugin.metadata.description.clone(),
        author: plugin.metadata.author.clone(),
        license: plugin.metadata.license.clone(),
        enabled: if plugin.enabled { "Yes" } else { "No" }.to_string(),
        commands: plugin
            .metadata
            .commands
            .iter()
            .map(|c| CommandInfo {
                name: c.name.clone(),
                description: c.description.clone(),
                aliases: c.aliases.join(", "),
                arguments: c.arguments.len(),
            })
            .collect(),
        dependencies: plugin.metadata.dependencies.clone(),
        min_version: plugin
            .metadata
            .min_version
            .clone()
            .unwrap_or_else(|| "None".to_string()),
    };

    println!("{}", render_output(&info, format)?);
    Ok(())
}

async fn install_plugin(path: &Path, format: OutputFormat) -> Result<()> {
    if !path.exists() {
        anyhow::bail!("Plugin directory not found: {:?}", path);
    }

    if !path.is_dir() {
        anyhow::bail!("Path is not a directory: {:?}", path);
    }

    let mut manager = PluginManager::new()?;
    manager.install_plugin(path)?;

    if format != OutputFormat::Quiet {
        println!("✓ Plugin installed successfully");
    }

    Ok(())
}

async fn uninstall_plugin(name: &str, yes: bool, format: OutputFormat) -> Result<()> {
    let mut manager = PluginManager::new()?;
    manager.discover_plugins()?;

    // Check if plugin exists
    if manager.get_plugin(name).is_none() {
        anyhow::bail!("Plugin not found: {}", name);
    }

    // Confirm uninstall
    if !yes && format != OutputFormat::Quiet {
        println!(
            "Are you sure you want to uninstall plugin '{}'? [y/N]",
            name
        );
        let mut input = String::new();
        std::io::stdin().read_line(&mut input)?;
        if !input.trim().eq_ignore_ascii_case("y") {
            println!("Uninstall cancelled");
            return Ok(());
        }
    }

    manager.uninstall_plugin(name)?;

    if format != OutputFormat::Quiet {
        println!("✓ Plugin uninstalled successfully");
    }

    Ok(())
}

async fn enable_plugin(name: &str, format: OutputFormat) -> Result<()> {
    let mut manager = PluginManager::new()?;
    manager.discover_plugins()?;

    manager.enable_plugin(name)?;

    if format != OutputFormat::Quiet {
        println!("✓ Plugin enabled: {}", name);
    }

    Ok(())
}

async fn disable_plugin(name: &str, format: OutputFormat) -> Result<()> {
    let mut manager = PluginManager::new()?;
    manager.discover_plugins()?;

    manager.disable_plugin(name)?;

    if format != OutputFormat::Quiet {
        println!("✓ Plugin disabled: {}", name);
    }

    Ok(())
}

async fn execute_plugin_command(
    plugin: &str,
    command: &str,
    args: Vec<String>,
    format: OutputFormat,
) -> Result<()> {
    let mut manager = PluginManager::new()?;
    manager.discover_plugins()?;

    // Parse arguments (KEY=VALUE format)
    let mut arguments = HashMap::new();
    for arg in args {
        let parts: Vec<&str> = arg.splitn(2, '=').collect();
        if parts.len() == 2 {
            arguments.insert(parts[0].to_string(), parts[1].to_string());
        } else {
            anyhow::bail!("Invalid argument format: {}. Expected KEY=VALUE", arg);
        }
    }

    let result = manager.execute_command(plugin, command, arguments).await?;

    if result.exit_code != 0 {
        if !result.stderr.is_empty() && format != OutputFormat::Quiet {
            eprintln!("{}", result.stderr);
        }
        anyhow::bail!("Plugin command failed with exit code: {}", result.exit_code);
    }

    if !result.stdout.is_empty() && format != OutputFormat::Quiet {
        println!("{}", result.stdout);
    }

    if let Some(data) = result.data {
        if format == OutputFormat::Json {
            println!("{}", serde_json::to_string_pretty(&data)?);
        } else if format == OutputFormat::Yaml {
            println!("{}", serde_yaml::to_string(&data)?);
        }
    }

    Ok(())
}

async fn reload_plugins(format: OutputFormat) -> Result<()> {
    let mut manager = PluginManager::new()?;
    let count = manager.discover_plugins()?;

    if format != OutputFormat::Quiet {
        println!("✓ Reloaded {} plugin(s)", count);
    }

    Ok(())
}

#[derive(Debug, Serialize)]
struct PluginDirectoryInfo {
    path: String,
    exists: bool,
}

impl MultiFormatDisplay for PluginDirectoryInfo {
    fn to_table(&self) -> Table {
        let mut table = Table::new();
        table
            .load_preset(UTF8_FULL)
            .set_content_arrangement(ContentArrangement::Dynamic);

        table.add_row(vec![
            Cell::new("Plugin Directory").fg(Color::Cyan),
            Cell::new(&self.path),
        ]);

        table.add_row(vec![
            Cell::new("Exists").fg(Color::Cyan),
            if self.exists {
                Cell::new("Yes").fg(Color::Green)
            } else {
                Cell::new("No").fg(Color::Red)
            },
        ]);

        table
    }

    fn to_quiet(&self) -> String {
        self.path.clone()
    }
}

async fn show_plugin_directory(format: OutputFormat) -> Result<()> {
    let plugin_dir = PluginManager::get_plugin_dir()?;

    let info = PluginDirectoryInfo {
        path: plugin_dir.to_string_lossy().to_string(),
        exists: plugin_dir.exists(),
    };

    println!("{}", render_output(&info, format)?);
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_plugin_list_entry_serialization() {
        let entry = PluginListEntry {
            name: "test-plugin".to_string(),
            version: "1.0.0".to_string(),
            description: "Test plugin".to_string(),
            enabled: "Yes".to_string(),
            commands: 2,
        };

        let json = serde_json::to_string(&entry).unwrap();
        assert!(json.contains("test-plugin"));
        assert!(json.contains("1.0.0"));
    }

    #[test]
    fn test_plugin_info_display() {
        let info = PluginInfoDisplay {
            name: "test-plugin".to_string(),
            version: "1.0.0".to_string(),
            description: "Test plugin".to_string(),
            author: "Test Author".to_string(),
            license: "MIT".to_string(),
            enabled: "Yes".to_string(),
            commands: vec![],
            dependencies: vec![],
            min_version: "0.1.0".to_string(),
        };

        let quiet = info.to_quiet();
        assert_eq!(quiet, "test-plugin v1.0.0");
    }

    #[test]
    fn test_plugin_directory_info() {
        let info = PluginDirectoryInfo {
            path: "/tmp/plugins".to_string(),
            exists: false,
        };

        let quiet = info.to_quiet();
        assert_eq!(quiet, "/tmp/plugins");
    }
}