zinit 0.3.6

Process supervisor with dependency management
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
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
//! Interactive REPL for zinit.
//!
//! Provides a readline-style interface with:
//! - Tab completion for commands and service names
//! - Command history
//! - Syntax highlighting
//! - Help hints
//!
//! Future: Rhai scripting support (Phase 5)

use std::borrow::Cow;
use std::path::Path;

use colored::Colorize;
use rustyline::completion::{Completer, Pair};
use rustyline::error::ReadlineError;
use rustyline::highlight::Highlighter;
use rustyline::hint::{Hinter, HistoryHinter};
use rustyline::history::DefaultHistory;
use rustyline::validate::Validator;
use rustyline::{Config, Context, Editor, Helper};

use crate::sdk::{State, ZinitClient};

/// Run the REPL interface.
pub fn run(socket_path: &Path) -> Result<(), String> {
    run_repl(socket_path).map_err(|e| e.to_string())
}

/// REPL commands
const REPL_COMMANDS: &[(&str, &str)] = &[
    ("help", "Show this help"),
    ("list", "List all services"),
    ("status <name>", "Show service status"),
    ("start <name>", "Start a service"),
    ("stop <name>", "Stop a service"),
    ("restart <name>", "Restart a service"),
    ("kill <name> [signal]", "Send signal to service"),
    ("why <name>", "Show why service is blocked"),
    ("tree", "Show dependency tree"),
    ("logs <name> [lines]", "Show service logs"),
    ("reload", "Reload service configs from disk"),
    ("remove <name>", "Remove a service"),
    ("ping", "Ping the daemon"),
    ("xinet list", "List xinet proxies"),
    ("xinet status [name]", "Show xinet proxy status"),
    ("clear", "Clear screen"),
    ("quit", "Exit REPL (or Ctrl+D)"),
];

/// CLI commands for completion (without arguments)
const CLI_COMMANDS: &[&str] = &[
    "help", "list", "status", "start", "stop", "restart", "kill", "why", "tree", "logs", "reload",
    "remove", "ping", "xinet", "clear", "quit", "exit",
];

/// Helper struct for rustyline
struct ReplHelper {
    hinter: HistoryHinter,
    service_names: Vec<String>,
}

impl ReplHelper {
    fn new() -> Self {
        Self {
            hinter: HistoryHinter::new(),
            service_names: Vec::new(),
        }
    }

    fn update_services(&mut self, names: Vec<String>) {
        self.service_names = names;
    }
}

impl Helper for ReplHelper {}

impl Completer for ReplHelper {
    type Candidate = Pair;

    fn complete(
        &self,
        line: &str,
        pos: usize,
        _ctx: &Context<'_>,
    ) -> rustyline::Result<(usize, Vec<Pair>)> {
        let mut completions = Vec::new();

        let line_to_cursor = &line[..pos];
        let parts: Vec<&str> = line_to_cursor.split_whitespace().collect();

        // If at start of line or completing first word, complete commands
        if parts.is_empty() || (parts.len() == 1 && !line_to_cursor.ends_with(' ')) {
            let word = parts.first().copied().unwrap_or("");
            let word_start = line_to_cursor.rfind(' ').map(|i| i + 1).unwrap_or(0);

            for cmd in CLI_COMMANDS {
                if cmd.starts_with(word) {
                    completions.push(Pair {
                        display: cmd.to_string(),
                        replacement: cmd.to_string(),
                    });
                }
            }
            return Ok((word_start, completions));
        }

        // If completing second word for commands that take service names
        let cmd = parts[0];
        let needs_service = matches!(
            cmd,
            "status" | "start" | "stop" | "restart" | "kill" | "why" | "logs" | "remove"
        );

        if needs_service
            && (parts.len() == 1 || (parts.len() == 2 && !line_to_cursor.ends_with(' ')))
        {
            let word = parts.get(1).copied().unwrap_or("");
            let word_start = line_to_cursor.rfind(' ').map(|i| i + 1).unwrap_or(0);

            for name in &self.service_names {
                if name.starts_with(word) {
                    completions.push(Pair {
                        display: name.clone(),
                        replacement: name.clone(),
                    });
                }
            }
            return Ok((word_start, completions));
        }

        Ok((pos, completions))
    }
}

impl Hinter for ReplHelper {
    type Hint = String;

    fn hint(&self, line: &str, pos: usize, ctx: &Context<'_>) -> Option<String> {
        // First try history hints
        if let Some(hint) = self.hinter.hint(line, pos, ctx) {
            return Some(hint);
        }

        // Show command hints
        let line_to_cursor = &line[..pos];
        let parts: Vec<&str> = line_to_cursor.split_whitespace().collect();

        if parts.len() == 1 && !line_to_cursor.ends_with(' ') {
            let word = parts[0];
            for (cmd, desc) in REPL_COMMANDS {
                let cmd_name = cmd.split_whitespace().next().unwrap_or(cmd);
                if cmd_name.starts_with(word) && cmd_name != word {
                    let suffix = &cmd_name[word.len()..];
                    let args = cmd.strip_prefix(cmd_name).unwrap_or("");
                    return Some(format!("{}{} - {}", suffix, args.dimmed(), desc.dimmed()));
                }
            }
        }

        None
    }
}

impl Highlighter for ReplHelper {
    fn highlight<'l>(&self, line: &'l str, _pos: usize) -> Cow<'l, str> {
        let parts: Vec<&str> = line.split_whitespace().collect();
        if parts.is_empty() {
            return Cow::Borrowed(line);
        }

        let cmd = parts[0];
        let rest = line.strip_prefix(cmd).unwrap_or("");

        let highlighted_cmd = if CLI_COMMANDS.contains(&cmd) {
            format!("{}", cmd.cyan().bold())
        } else {
            format!("{}", cmd.red())
        };

        // Highlight service name argument
        let highlighted_rest = if parts.len() > 1 {
            let name = parts[1];
            let after_name = rest
                .strip_prefix(char::is_whitespace)
                .and_then(|s| s.strip_prefix(name))
                .unwrap_or("");

            let name_color = if self.service_names.contains(&name.to_string()) {
                format!("{}", name.green())
            } else {
                format!("{}", name.yellow())
            };

            format!(" {}{}", name_color, after_name)
        } else {
            rest.to_string()
        };

        Cow::Owned(format!("{}{}", highlighted_cmd, highlighted_rest))
    }

    fn highlight_prompt<'b, 's: 'b, 'p: 'b>(
        &'s self,
        prompt: &'p str,
        _default: bool,
    ) -> Cow<'b, str> {
        Cow::Owned(format!("{}", prompt.cyan().bold()))
    }

    fn highlight_hint<'h>(&self, hint: &'h str) -> Cow<'h, str> {
        Cow::Owned(format!("{}", hint.bright_black()))
    }

    fn highlight_char(
        &self,
        _line: &str,
        _pos: usize,
        _kind: rustyline::highlight::CmdKind,
    ) -> bool {
        true
    }
}

impl Validator for ReplHelper {}

/// Print the welcome banner
fn print_banner() {
    println!(
        "{}",
        "╔═══════════════════════════════════════════════════════════╗".cyan()
    );
    println!(
        "{}",
        "║              zinit Interactive Shell                      ║".cyan()
    );
    println!(
        "{}",
        "╠═══════════════════════════════════════════════════════════╣".cyan()
    );
    println!(
        "{}  {}",
        "".cyan(),
        format!(
            "{:<56} {}",
            "Tab: completion | Up/Down: history | Ctrl+D: exit", ""
        )
        .cyan()
    );
    println!(
        "{}  {}",
        "".cyan(),
        format!("{:<56} {}", "Type 'help' for commands", "").cyan()
    );
    println!(
        "{}",
        "╚═══════════════════════════════════════════════════════════╝".cyan()
    );
    println!();
}

/// Print help
fn print_help() {
    println!("{}", "Commands:".yellow().bold());
    for (cmd, desc) in REPL_COMMANDS {
        println!("  {:25} {}", cmd.cyan(), desc);
    }
    println!();
    println!("{}", "Tips:".yellow().bold());
    println!(
        "  - Press {} to autocomplete commands and service names",
        "Tab".cyan()
    );
    println!("  - Use {} arrows for command history", "Up/Down".cyan());
    println!("  - Service names are auto-completed from running services");
}

/// Execute a command
fn execute_command(client: &mut ZinitClient, line: &str) -> Result<bool, String> {
    let parts: Vec<&str> = line.split_whitespace().collect();
    if parts.is_empty() {
        return Ok(true);
    }

    let cmd = parts[0];
    let args = &parts[1..];

    match cmd {
        "help" => {
            print_help();
        }
        "quit" | "exit" => {
            return Ok(false);
        }
        "clear" => {
            print!("\x1B[2J\x1B[1;1H");
            print_banner();
        }
        "list" | "ls" => {
            let names = client.list().map_err(|e| e.to_string())?;
            if names.is_empty() {
                println!("No services configured");
            } else {
                let max_len = names.iter().map(|n| n.len()).max().unwrap_or(10);
                for name in names {
                    if let Ok(status) = client.status(&name) {
                        let pid_str = if status.pid > 0 {
                            format!(" (pid: {})", status.pid)
                        } else {
                            String::new()
                        };
                        let symbol = match status.state {
                            State::Running => "".green(),
                            State::Starting | State::Stopping => "".yellow(),
                            State::Blocked | State::Inactive => "".normal(),
                            State::Exited => "".normal(),
                            State::Failed => "".red(),
                        };
                        println!(
                            "{} {:<width$} {:?}{}",
                            symbol,
                            name,
                            status.state,
                            pid_str,
                            width = max_len
                        );
                    } else {
                        println!("? {:<width$} unknown", name, width = max_len);
                    }
                }
            }
        }
        "status" => {
            let name = args.first().ok_or("Usage: status <name>")?;
            let status = client.status(name).map_err(|e| e.to_string())?;
            let symbol = match status.state {
                State::Running => "".green(),
                State::Starting | State::Stopping => "".yellow(),
                State::Blocked | State::Inactive => "".normal(),
                State::Exited => "".normal(),
                State::Failed => "".red(),
            };
            println!("Service: {}", status.name);
            println!("State:   {} {:?}", symbol, status.state);
            if status.pid > 0 {
                println!("PID:     {}", status.pid);
            }
            if let Some(code) = status.exit_code {
                println!("Exit:    {}", code);
            }
            if let Some(ref err) = status.error {
                println!("Error:   {}", err.red());
            }
        }
        "start" => {
            let name = args.first().ok_or("Usage: start <name>")?;
            client.start(name).map_err(|e| e.to_string())?;
            println!("{} Started: {}", "+".green(), name);
        }
        "stop" => {
            let name = args.first().ok_or("Usage: stop <name>")?;
            client.stop(name).map_err(|e| e.to_string())?;
            println!("{} Stopped: {}", "-".yellow(), name);
        }
        "restart" => {
            let name = args.first().ok_or("Usage: restart <name>")?;
            client.restart(name).map_err(|e| e.to_string())?;
            println!("{} Restarted: {}", "*".cyan(), name);
        }
        "kill" => {
            let name = args.first().ok_or("Usage: kill <name> [signal]")?;
            let signal = args.get(1).copied();
            client.kill(name, signal).map_err(|e| e.to_string())?;
            let sig = signal.unwrap_or("SIGTERM");
            println!("{} Sent {} to: {}", "!".red(), sig, name);
        }
        "why" => {
            let name = args.first().ok_or("Usage: why <name>")?;
            let why = client.why(name).map_err(|e| e.to_string())?;
            if !why.blocked {
                println!("{} is not blocked", name.green());
            } else {
                println!("{}", why.ascii);
                if !why.waiting_on.is_empty() {
                    println!("\nWaiting on: {}", why.waiting_on.join(", ").yellow());
                }
                if !why.conflicts_with.is_empty() {
                    println!("Conflicts with: {}", why.conflicts_with.join(", ").red());
                }
            }
        }
        "tree" => {
            let tree = client.tree().map_err(|e| e.to_string())?;
            println!("{}", tree);
        }
        "logs" => {
            let name = args.first().ok_or("Usage: logs <name> [lines]")?;
            let lines: usize = args.get(1).and_then(|s| s.parse().ok()).unwrap_or(50);
            let logs = client.logs(name, Some(lines)).map_err(|e| e.to_string())?;
            if logs.is_empty() {
                println!("No logs available for {}", name);
            } else {
                for log in logs {
                    println!("{}", log);
                }
            }
        }
        "reload" => {
            let result = client.reload().map_err(|e| e.to_string())?;
            if result.added.is_empty() && result.removed.is_empty() && result.changed.is_empty() {
                println!("No changes detected");
            } else {
                if !result.added.is_empty() {
                    println!("{}   {}", "Added:".green(), result.added.join(", "));
                }
                if !result.removed.is_empty() {
                    println!("{} {}", "Removed:".red(), result.removed.join(", "));
                }
                if !result.changed.is_empty() {
                    println!("{} {}", "Changed:".yellow(), result.changed.join(", "));
                }
            }
        }
        "remove" => {
            let name = args.first().ok_or("Usage: remove <name>")?;
            client.remove(name).map_err(|e| e.to_string())?;
            println!("{} Removed: {}", "-".red(), name);
        }
        "ping" => {
            let version = client.ping().map_err(|e| e.to_string())?;
            println!("{} zinit daemon v{} is running", "+".green(), version);
        }
        "xinet" => {
            let subcmd = args.first().ok_or("Usage: xinet <list|status [name]>")?;
            match *subcmd {
                "list" => {
                    let proxies = client.xinet_list().map_err(|e| e.to_string())?;
                    if proxies.is_empty() {
                        println!("No xinet proxies registered");
                    } else {
                        println!("{}", "Registered xinet proxies:".cyan());
                        for name in proxies {
                            println!("  {}", name);
                        }
                    }
                }
                "status" => {
                    let name = args.get(1).copied();
                    match name {
                        Some(n) => {
                            let status = client.xinet_status(n).map_err(|e| e.to_string())?;
                            println!("{}: {}", "Proxy".cyan(), status.name);
                            println!("  Listen:  {}", status.listen);
                            println!("  Backend: {}", status.backend);
                            println!("  Service: {}", status.service);
                            println!(
                                "  Status:  {}",
                                if status.running {
                                    "running".green()
                                } else {
                                    "stopped".yellow()
                                }
                            );
                            println!(
                                "  Connections: {} active, {} total",
                                status.active_connections, status.total_connections
                            );
                        }
                        None => {
                            let statuses = client.xinet_status_all().map_err(|e| e.to_string())?;
                            if statuses.is_empty() {
                                println!("No xinet proxies registered");
                            } else {
                                for (i, status) in statuses.iter().enumerate() {
                                    if i > 0 {
                                        println!();
                                    }
                                    println!("{}: {}", "Proxy".cyan(), status.name);
                                    println!("  Listen:  {}", status.listen);
                                    println!("  Backend: {}", status.backend);
                                    println!("  Service: {}", status.service);
                                    println!(
                                        "  Status:  {}",
                                        if status.running {
                                            "running".green()
                                        } else {
                                            "stopped".yellow()
                                        }
                                    );
                                    println!(
                                        "  Connections: {} active, {} total",
                                        status.active_connections, status.total_connections
                                    );
                                }
                            }
                        }
                    }
                }
                _ => {
                    println!(
                        "{}: Unknown xinet subcommand '{}'. Use: list, status",
                        "Error".red(),
                        subcmd
                    );
                }
            }
        }
        _ => {
            println!(
                "{}: Unknown command '{}'. Type 'help' for commands.",
                "Error".red(),
                cmd
            );
        }
    }

    Ok(true)
}

/// Run the interactive REPL
fn run_repl(socket_path: &Path) -> Result<(), Box<dyn std::error::Error>> {
    print_banner();

    // Connect to zinit
    let mut client = match ZinitClient::connect(socket_path) {
        Ok(c) => c,
        Err(e) => {
            println!(
                "{}: Could not connect to zinit at {}: {}",
                "Warning".yellow(),
                socket_path.display(),
                e
            );
            println!("Some commands will not work until the server is running.");
            println!();
            // Create a dummy connection attempt later
            return Err(Box::new(e));
        }
    };

    // Check if server is running
    match client.ping() {
        Ok(version) => {
            println!("{} Connected to zinit daemon v{}", "+".green(), version);
        }
        Err(e) => {
            println!("{}: Could not ping zinit server: {}", "Warning".yellow(), e);
        }
    }
    println!();

    let config = Config::builder()
        .history_ignore_space(true)
        .completion_type(rustyline::CompletionType::List)
        .edit_mode(rustyline::EditMode::Emacs)
        .build();

    let mut helper = ReplHelper::new();

    // Initial fetch of service names for completion
    if let Ok(names) = client.list() {
        helper.update_services(names);
    }

    let mut rl: Editor<ReplHelper, DefaultHistory> = Editor::with_config(config)?;
    rl.set_helper(Some(helper));

    // Load history
    let history_path = dirs::home_dir()
        .map(|h| h.join(".zinit_history"))
        .unwrap_or_else(|| std::path::PathBuf::from(".zinit_history"));
    let _ = rl.load_history(&history_path);

    loop {
        match rl.readline("zinit> ") {
            Ok(line) => {
                let line = line.trim();
                if line.is_empty() {
                    continue;
                }

                // Add to history
                let _ = rl.add_history_entry(line);

                // Execute command
                match execute_command(&mut client, line) {
                    Ok(true) => {
                        // Update service names for completion after commands that might change them
                        if (line.starts_with("start")
                            || line.starts_with("stop")
                            || line.starts_with("reload")
                            || line.starts_with("remove"))
                            && let Ok(names) = client.list()
                            && let Some(helper) = rl.helper_mut()
                        {
                            helper.update_services(names);
                        }
                    }
                    Ok(false) => {
                        println!("{}", "Goodbye!".cyan());
                        break;
                    }
                    Err(e) => {
                        println!("{}: {}", "Error".red().bold(), e);
                    }
                }
                println!();
            }
            Err(ReadlineError::Interrupted) => {
                println!("{}", "^C (use 'quit' or Ctrl+D to exit)".bright_black());
            }
            Err(ReadlineError::Eof) => {
                println!("{}", "Goodbye!".cyan());
                break;
            }
            Err(err) => {
                println!("{}: {:?}", "Error".red(), err);
                break;
            }
        }
    }

    // Save history
    let _ = rl.save_history(&history_path);

    Ok(())
}