oxproc 0.1.0

A simple process manager for proc.toml/Procfile projects with colored log following, daemon mode, and one-off task runner (including composite tasks).
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
use anyhow::Result;
use clap::{Parser, Subcommand};
use std::path::PathBuf;

mod color;
mod config;
#[cfg(unix)]
mod daemon;
mod dirs;
mod list;
mod manager;
mod state;
mod task;

// config loader is used via config::load_config_from

#[derive(Parser, Debug)]
#[command(author, version, about, long_about = None)]
struct Cli {
    /// Project root containing proc.toml/Procfile (default: current dir)
    #[arg(global = true, long = "root", value_name = "PATH")]
    root: Option<PathBuf>,

    /// Colorize output: auto, always, or never
    #[arg(global = true, long = "color", value_enum)]
    color: Option<ColorChoice>,

    #[command(subcommand)]
    command: Option<Commands>,
}

#[derive(Subcommand, Debug)]
enum Commands {
    /// Start all processes as a background daemon
    Start {
        /// Follow logs after starting (equivalent to: start && logs -f)
        #[arg(short, long)]
        follow: bool,
    },
    /// Show status for the current project's processes
    #[command(alias = "ps")]
    Status {},
    /// Stop all processes for the current project
    Stop {
        /// Grace period in seconds before SIGKILL
        #[arg(long, default_value_t = 5)]
        grace: u64,
    },
    /// Restart all processes (stop then start). Add -f to follow logs.
    Restart {
        /// Grace period in seconds before SIGKILL
        #[arg(long, default_value_t = 5)]
        grace: u64,
        /// Follow logs after restarting
        #[arg(short, long)]
        follow: bool,
    },
    /// View logs. By default shows combined logs. Use --name to filter.
    Logs {
        /// Process name to filter
        #[arg(long)]
        name: Option<String>,
        /// Follow the logs
        #[arg(short, long)]
        follow: bool,
        /// Number of lines from the end
        #[arg(short = 'n', long, default_value_t = 100)]
        lines: usize,
    },
    /// List configured processes and tasks (proc.toml only for tasks)
    #[command(alias = "ls")]
    List {
        /// Output as JSON
        #[arg(long)]
        json: bool,
        /// Print names only, one per line
        #[arg(long = "names-only")]
        names_only: bool,
        /// Show only processes
        #[arg(long = "processes-only")]
        processes_only: bool,
        /// Show only tasks
        #[arg(long = "tasks-only")]
        tasks_only: bool,
    },
    /// Run a one-off task from proc.toml
    Run {
        /// Task name under [tasks.<name>]
        task: String,
        /// Arguments passed to the task command after '--'
        #[arg(trailing_var_arg = true)]
        args: Vec<String>,
    },
    /// Shorthand: if not a known command, treat first token as a task name
    #[command(external_subcommand)]
    External(Vec<String>),
}

#[derive(Clone, Debug, clap::ValueEnum)]
enum ColorChoice {
    Auto,
    Always,
    Never,
}

impl From<ColorChoice> for color::ColorMode {
    fn from(c: ColorChoice) -> Self {
        match c {
            ColorChoice::Auto => color::ColorMode::Auto,
            ColorChoice::Always => color::ColorMode::Always,
            ColorChoice::Never => color::ColorMode::Never,
        }
    }
}

fn main() -> Result<()> {
    let cli = Cli::parse();
    color::init(cli.color.map(|c| c.into()));
    let root = cli.root.unwrap_or_else(|| std::env::current_dir().unwrap());
    match cli.command {
        Some(Commands::Start { follow }) => {
            #[cfg(unix)]
            {
                if follow {
                    start_and_follow(&root)
                } else {
                    daemon::start_daemon(&root)
                }
            }
            #[cfg(not(unix))]
            {
                anyhow::bail!("Daemon mode is only supported on Unix (Linux/macOS)");
            }
        }
        Some(Commands::Status {}) => {
            state::print_status(&root)?;
            Ok(())
        }
        Some(Commands::Stop { grace }) => {
            #[cfg(unix)]
            {
                manager::stop_all(&root, Some(std::time::Duration::from_secs(grace)))?;
                Ok(())
            }
            #[cfg(not(unix))]
            {
                anyhow::bail!("Stop is only supported on Unix in daemon mode");
            }
        }
        Some(Commands::Logs {
            name,
            follow,
            lines,
        }) => {
            manager::print_logs(&root, name, follow, lines)?;
            Ok(())
        }
        Some(Commands::Restart { grace, follow }) => {
            #[cfg(unix)]
            {
                manager::stop_all(&root, Some(std::time::Duration::from_secs(grace)))?;
                if follow {
                    start_and_follow(&root)
                } else {
                    daemon::start_daemon(&root)
                }
            }
            #[cfg(not(unix))]
            {
                anyhow::bail!("Restart is only supported on Unix in daemon mode");
            }
        }
        Some(Commands::List {
            json,
            names_only,
            processes_only,
            tasks_only,
        }) => {
            let info = list::gather_list_info(&root)?;
            if json {
                println!("{}", serde_json::to_string_pretty(&info)?);
                return Ok(());
            }
            if names_only {
                let s = list::format_list_names_only(&info, processes_only, tasks_only);
                if !s.is_empty() {
                    println!("{}", s);
                }
                return Ok(());
            }
            let s = list::format_list_human(&info, processes_only, tasks_only);
            print!("{}", s);
            Ok(())
        }
        Some(Commands::Run { task, args }) => run_task(&root, &task, &args),
        Some(Commands::External(v)) => {
            if v.is_empty() {
                anyhow::bail!("No task name provided")
            } else {
                let task = &v[0];
                let args = v[1..].to_vec();
                run_task(&root, task, &args)
            }
        }
        None => {
            // Default: foreground follow of all processes (dev UX)
            tokio_foreground_follow(&root)
        }
    }
}

#[cfg(unix)]
fn start_and_follow(root: &std::path::Path) -> Result<()> {
    use std::process::Command;
    use std::time::Duration;

    // Spawn a fresh `oxproc start` without --follow to perform the daemonization
    let exe = std::env::current_exe()?;
    let mut args: Vec<String> = Vec::new();
    // forward --root if provided
    args.push("start".to_string());
    // If the user passed --root in the original invocation, `root` will reflect it; we must forward
    // by comparing with current_dir and adding explicit flag only if different.
    if let Ok(cwd) = std::env::current_dir() {
        if cwd != root {
            args.splice(
                0..0,
                vec!["--root".to_string(), root.to_string_lossy().to_string()],
            );
        }
    }

    let status = Command::new(exe)
        .args(&args)
        .stdin(std::process::Stdio::null())
        .stdout(std::process::Stdio::inherit())
        .stderr(std::process::Stdio::inherit())
        .spawn();

    match status {
        Ok(_child) => {
            // Wait for readiness then attach logs
            println!("Waiting for manager to become ready…");
            state::wait_for_manager_ready(root, Duration::from_secs(10))?;
            println!("Attaching to logs (Ctrl+C to detach)…");
            manager::print_logs(root, None, true, 100)?;
            Ok(())
        }
        Err(e) => {
            anyhow::bail!("Failed to spawn start: {}", e);
        }
    }
}

fn tokio_foreground_follow(root: &std::path::Path) -> Result<()> {
    use futures::future::join_all;
    use std::process::Stdio;
    use std::sync::Arc;
    use tokio::io::{AsyncBufReadExt, AsyncRead, BufReader};
    use tokio::process::Command;
    use tokio::runtime::Runtime;
    use tokio::sync::Mutex;

    let rt = Runtime::new()?;
    rt.block_on(async move {
        let configs = config::load_config_from(root)?;

        async fn handle_output<T: AsyncRead + Unpin>(
            child_name: String,
            stream: T,
            _log_path: Option<String>,
            follow: bool,
            prefix: &'static str,
        ) {
            let mut reader = BufReader::new(stream).lines();
            while let Some(line) = reader.next_line().await.unwrap() {
                if follow {
                    let p = color::prefix(&child_name);
                    println!("{}{}{}", p, prefix, line);
                }
            }
        }

        let mut children = Vec::new();
        let mut handles = Vec::new();

        for config in configs {
            let mut cmd = Command::new("sh");
            cmd.arg("-c");
            cmd.arg(&config.command);
            if let Some(cwd) = &config.cwd {
                let abs = if std::path::Path::new(cwd).is_absolute() {
                    std::path::PathBuf::from(cwd)
                } else {
                    root.join(cwd)
                };
                if !abs.exists() {
                    return Err(anyhow::anyhow!(
                        "Process '{}' cwd does not exist: {}",
                        config.name,
                        abs.display()
                    ));
                }
                cmd.current_dir(abs);
            }
            cmd.stdout(Stdio::piped());
            cmd.stderr(Stdio::piped());

            let mut child = cmd.spawn()?;
            let pid = child.id().unwrap();
            println!("Started {} with PID: {}", config.name, pid);

            let stdout = child.stdout.take().unwrap();
            let stderr = child.stderr.take().unwrap();

            let stdout_handle =
                tokio::spawn(handle_output(config.name.clone(), stdout, None, true, ""));

            let stderr_handle = tokio::spawn(handle_output(
                config.name.clone(),
                stderr,
                None,
                true,
                "[ERR] ",
            ));

            children.push(Arc::new(Mutex::new(child)));
            handles.push(stdout_handle);
            handles.push(stderr_handle);
        }

        tokio::select! {
            _ = join_all(handles) => {},
            _ = tokio::signal::ctrl_c() => {
                println!("\nShutting down...");
                for child in children.iter_mut() {
                    let mut child_guard = child.lock().await;
                    child_guard.kill().await?;
                }
            }
        }

        Ok::<(), anyhow::Error>(())
    })?;

    Ok(())
}

fn run_task(root: &std::path::Path, task: &str, args: &[String]) -> Result<()> {
    use tokio::runtime::Runtime;

    // Gate: only available for proc.toml projects
    match config::detect_source(root)? {
        config::ConfigSource::Procfile => {
            anyhow::bail!("Task runner requires proc.toml. Current project uses a Procfile.");
        }
        config::ConfigSource::ProcToml => {}
    }

    let tasks_opt = config::load_tasks_from(root)?;
    let tasks = tasks_opt.unwrap_or_default();

    // Normalize user query: allow frontend:build or frontend.build
    let key = task::normalize_task_query(task);

    let Some(_) = tasks.get(&key) else {
        let mut available: Vec<String> = tasks.keys().map(|k| task::display_task_name(k)).collect();
        available.sort();
        if available.is_empty() {
            anyhow::bail!("Unknown task '{}'. No tasks defined under [tasks].", task);
        } else {
            anyhow::bail!(
                "Unknown task '{}'. Available tasks: {}",
                task,
                available.join(", ")
            );
        }
    };

    // Execute task graph
    let rt = Runtime::new()?;
    let outcome = rt.block_on(async move {
        exec_task(
            root,
            &tasks,
            &key,
            args,
            &mut Vec::new(),
            StdioMode::Inherit,
        )
        .await
    })?;

    match outcome {
        ExecOutcome::Success => Ok(()),
        ExecOutcome::Failed(code) => {
            std::process::exit(code);
        }
    }
}

#[derive(Debug, Clone, Copy)]
enum StdioMode<'a> {
    Inherit,
    Prefixed(&'a str),
}

#[derive(Debug)]
enum ExecOutcome {
    Success,
    Failed(i32),
}

type ExecFut<'a> = futures::future::BoxFuture<'a, Result<ExecOutcome>>;

fn exec_task<'a>(
    root: &'a std::path::Path,
    tasks: &'a std::collections::HashMap<String, config::TaskConfig>,
    name: &'a str,
    args: &'a [String],
    stack: &'a mut Vec<String>,
    stdio: StdioMode<'a>,
) -> ExecFut<'a> {
    Box::pin(async move {
        use crate::config::TaskKind;

        let Some(task_cfg) = tasks.get(name) else {
            let mut available: Vec<String> =
                tasks.keys().map(|k| task::display_task_name(k)).collect();
            available.sort();
            anyhow::bail!(
                "Unknown task '{}'. Available tasks: {}",
                task::display_task_name(name),
                available.join(", ")
            );
        };

        // Cycle detection
        if stack.contains(&name.to_string()) {
            stack.push(name.to_string());
            let pretty = stack
                .iter()
                .map(|s| task::display_task_name(s))
                .collect::<Vec<_>>()
                .join(" -> ");
            anyhow::bail!("Dependency cycle detected: {}", pretty);
        }

        stack.push(name.to_string());

        let result = match &task_cfg.kind {
            TaskKind::Shell { cmd, cwd } => {
                run_shell_task(root, name, cmd, cwd.as_deref(), args, stdio).await?
            }
            TaskKind::Composite { children, parallel } => {
                if *parallel {
                    // Launch all children concurrently, each with prefixed output using the top-level child label.
                    let mut futs = Vec::new();
                    for c in children {
                        let child_abs = task::resolve_child_name(name, c);
                        let display = task::display_task_name(&child_abs);
                        let mut local_stack = stack.clone();
                        let args_vec = args.to_vec();
                        let fut = async move {
                            exec_task(
                                root,
                                tasks,
                                &child_abs,
                                &args_vec,
                                &mut local_stack,
                                StdioMode::Prefixed(&display),
                            )
                            .await
                        };
                        futs.push(fut);
                    }
                    let results = futures::future::join_all(futs).await;
                    // If any child failed, propagate first non-zero code
                    let mut first_failed: Option<i32> = None;
                    for r in results {
                        match r? {
                            ExecOutcome::Success => {}
                            ExecOutcome::Failed(code) => {
                                if first_failed.is_none() {
                                    first_failed = Some(code);
                                }
                            }
                        }
                    }
                    match first_failed {
                        Some(code) => ExecOutcome::Failed(code),
                        None => ExecOutcome::Success,
                    }
                } else {
                    // Sequential: run in order, stop on first failure
                    for c in children {
                        let child_abs = task::resolve_child_name(name, c);
                        println!("▶ running {}…", task::display_task_name(&child_abs));
                        match exec_task(root, tasks, &child_abs, args, stack, stdio).await? {
                            ExecOutcome::Success => {}
                            ExecOutcome::Failed(code) => return Ok(ExecOutcome::Failed(code)),
                        }
                    }
                    ExecOutcome::Success
                }
            }
        };

        stack.pop();
        Ok(result)
    })
}

async fn run_shell_task(
    root: &std::path::Path,
    name: &str,
    cmd_str: &str,
    cwd: Option<&str>,
    args: &[String],
    stdio: StdioMode<'_>,
) -> Result<ExecOutcome> {
    use tokio::io::{AsyncBufReadExt, AsyncRead, BufReader};

    // Build final command string
    let mut final_cmd = cmd_str.to_string();
    if !args.is_empty() {
        let extra = args.join(" ");
        final_cmd.push(' ');
        final_cmd.push_str(&extra);
    }

    let mut cmd = tokio::process::Command::new("sh");
    cmd.arg("-c").arg(&final_cmd);

    // cwd handling
    if let Some(cwd) = cwd {
        let abs = if std::path::Path::new(cwd).is_absolute() {
            std::path::PathBuf::from(cwd)
        } else {
            root.join(cwd)
        };
        if !abs.exists() {
            anyhow::bail!(
                "Task '{}' cwd does not exist: {}",
                task::display_task_name(name),
                abs.display()
            );
        }
        cmd.current_dir(abs);
    } else {
        cmd.current_dir(root);
    }

    match stdio {
        StdioMode::Inherit => {
            use std::process::Stdio;
            cmd.stdin(Stdio::inherit());
            cmd.stdout(Stdio::inherit());
            cmd.stderr(Stdio::inherit());
            let status = cmd.status().await?;
            if !status.success() {
                if let Some(code) = status.code() {
                    return Ok(ExecOutcome::Failed(code));
                } else {
                    anyhow::bail!("Task terminated by signal");
                }
            }
            Ok(ExecOutcome::Success)
        }
        StdioMode::Prefixed(label) => {
            use std::process::Stdio;
            cmd.stdin(Stdio::null());
            cmd.stdout(Stdio::piped());
            cmd.stderr(Stdio::piped());
            let mut child = cmd.spawn()?;
            let prefix = color::prefix(label);

            async fn handle_output<T: AsyncRead + Unpin>(prefix: String, stream: T, err: bool) {
                let mut reader = BufReader::new(stream).lines();
                while let Ok(Some(line)) = reader.next_line().await {
                    if err {
                        println!("{}[ERR] {}", prefix, line);
                    } else {
                        println!("{}{}", prefix, line);
                    }
                }
            }

            let mut handles = Vec::new();
            if let Some(stdout) = child.stdout.take() {
                handles.push(tokio::spawn(handle_output(prefix.clone(), stdout, false)));
            }
            if let Some(stderr) = child.stderr.take() {
                handles.push(tokio::spawn(handle_output(prefix.clone(), stderr, true)));
            }

            let status = child.wait().await?;
            futures::future::join_all(handles).await;
            if !status.success() {
                if let Some(code) = status.code() {
                    return Ok(ExecOutcome::Failed(code));
                } else {
                    anyhow::bail!("Task terminated by signal");
                }
            }
            Ok(ExecOutcome::Success)
        }
    }
}