init for public release

This commit is contained in:
geoffsee
2025-08-25 15:15:13 -04:00
commit 3995b14f9e
11 changed files with 2393 additions and 0 deletions

64
.github/dependabot.yml vendored Normal file
View File

@@ -0,0 +1,64 @@
# Dependabot configuration for muxox
# Monitors TLS dependencies for security updates and advisories
# Generated for Task 6: Dependency Monitoring Setup
version: 2
updates:
# Monitor Rust dependencies in the main crate
- package-ecosystem: "cargo"
directory: "/crates/muxox"
schedule:
interval: "weekly"
day: "monday"
time: "09:00"
timezone: "UTC"
# Focus on security updates with higher priority
open-pull-requests-limit: 10
reviewers:
- "security-team"
assignees:
- "maintainer"
labels:
- "dependencies"
- "security"
# Security updates get higher priority
allow:
- dependency-type: "all"
# Group minor and patch updates to reduce noise
groups:
tls-dependencies:
patterns:
- "hyper-tls"
- "native-tls"
- "hyper-rustls"
- "rustls-pemfile"
- "rustls*"
update-types:
- "minor"
- "patch"
# Separate major updates for careful review
ignore:
- dependency-name: "*"
update-types: ["version-update:semver-major"]
commit-message:
prefix: "deps"
include: "scope"
# Monitor security updates more frequently
- package-ecosystem: "cargo"
directory: "/crates/muxox"
schedule:
interval: "daily"
# Only security updates in daily checks
allow:
- dependency-type: "direct"
update-types: ["security"]
- dependency-type: "indirect"
update-types: ["security"]
open-pull-requests-limit: 5
labels:
- "security-update"
- "high-priority"
commit-message:
prefix: "security"
include: "scope"

65
.github/workflows/ci.yml vendored Normal file
View File

@@ -0,0 +1,65 @@
name: CI
on:
push:
pull_request:
jobs:
build:
name: build-and-test (${{ matrix.name }})
runs-on: ubuntu-latest
defaults:
run:
working-directory: crates/muxox
strategy:
fail-fast: false
matrix:
include:
- name: default (native-tls)
features: ""
no-default-features: false
steps:
- name: Checkout
uses: actions/checkout@v4
- uses: actions/cache@v4
with:
path: |
~/.cargo/bin/
~/.cargo/registry/index/
~/.cargo/registry/cache/
~/.cargo/git/db/
target/
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
- name: Setup Rust
run: rustup update stable && rustup default stable
- name: Install clippy and rustfmt
run: rustup component add clippy rustfmt
- name: Cargo fmt (check)
run: cargo fmt --all -- --check
- name: Clippy
shell: bash
run: |
FLAGS=""
if [ "${{ matrix.no-default-features }}" = "true" ]; then FLAGS="$FLAGS --no-default-features"; fi
if [ -n "${{ matrix.features }}" ]; then FLAGS="$FLAGS --features ${{ matrix.features }}"; fi
echo "Running: cargo clippy --all-targets $FLAGS -- -D warnings"
cargo clippy --all-targets $FLAGS -- -D warnings
- name: Tests
shell: bash
run: |
FLAGS=""
if [ "${{ matrix.no-default-features }}" = "true" ]; then FLAGS="$FLAGS --no-default-features"; fi
if [ -n "${{ matrix.features }}" ]; then FLAGS="$FLAGS --features ${{ matrix.features }}"; fi
echo "Running: cargo test $FLAGS -- --nocapture"
cargo test $FLAGS -- --nocapture
- name: Build Docs
shell: bash
run: |
cargo doc -p muxox --no-deps

225
.github/workflows/release.yml vendored Normal file
View File

@@ -0,0 +1,225 @@
name: Release
on:
push:
tags:
- 'v*'
env:
CARGO_TERM_COLOR: always
jobs:
docs:
name: Build and validate documentation
runs-on: ubuntu-latest
defaults:
run:
working-directory: crates/muxox
strategy:
fail-fast: false
matrix:
include:
- name: default-features
features: ""
no-default-features: false
steps:
- name: Checkout
uses: actions/checkout@v4
- uses: actions/cache@v4
with:
path: |
~/.cargo/bin/
~/.cargo/registry/index/
~/.cargo/registry/cache/
~/.cargo/git/db/
target/
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
- name: Setup Rust
run: rustup update stable && rustup default stable
- name: Build documentation
shell: bash
run: |
FLAGS=""
if [ "${{ matrix.no-default-features }}" = "true" ]; then FLAGS="$FLAGS --no-default-features"; fi
if [ -n "${{ matrix.features }}" ]; then FLAGS="$FLAGS --features ${{ matrix.features }}"; fi
echo "Running: cargo doc $FLAGS --no-deps"
cargo doc $FLAGS --no-deps
- name: Check documentation warnings
shell: bash
run: |
FLAGS=""
if [ "${{ matrix.no-default-features }}" = "true" ]; then FLAGS="$FLAGS --no-default-features"; fi
if [ -n "${{ matrix.features }}" ]; then FLAGS="$FLAGS --features ${{ matrix.features }}"; fi
echo "Running: cargo doc $FLAGS --no-deps"
RUSTDOCFLAGS="-D warnings" cargo doc $FLAGS --no-deps
- name: Test documentation examples
shell: bash
run: |
FLAGS=""
if [ "${{ matrix.no-default-features }}" = "true" ]; then FLAGS="$FLAGS --no-default-features"; fi
if [ -n "${{ matrix.features }}" ]; then FLAGS="$FLAGS --features ${{ matrix.features }}"; fi
echo "Running: cargo test --doc $FLAGS"
cargo test --doc $FLAGS
test:
name: Test before release
runs-on: ubuntu-latest
needs: docs
defaults:
run:
working-directory: crates/muxox
strategy:
fail-fast: false
matrix:
include:
- name: default (native-tls)
features: ""
no-default-features: false
steps:
- name: Checkout
uses: actions/checkout@v4
- uses: actions/cache@v4
with:
path: |
~/.cargo/bin/
~/.cargo/registry/index/
~/.cargo/registry/cache/
~/.cargo/git/db/
target/
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
- name: Setup Rust
run: rustup update stable && rustup default stable
- name: Install clippy and rustfmt
run: rustup component add clippy rustfmt
- name: Cargo fmt (check)
run: cargo fmt --all -- --check
- name: Clippy
shell: bash
run: |
FLAGS=""
if [ "${{ matrix.no-default-features }}" = "true" ]; then FLAGS="$FLAGS --no-default-features"; fi
if [ -n "${{ matrix.features }}" ]; then FLAGS="$FLAGS --features ${{ matrix.features }}"; fi
echo "Running: cargo clippy --all-targets $FLAGS -- -D warnings"
cargo clippy --all-targets $FLAGS -- -D warnings
- name: Tests
shell: bash
run: |
FLAGS=""
if [ "${{ matrix.no-default-features }}" = "true" ]; then FLAGS="$FLAGS --no-default-features"; fi
if [ -n "${{ matrix.features }}" ]; then FLAGS="$FLAGS --features ${{ matrix.features }}"; fi
echo "Running: cargo test $FLAGS -- --nocapture"
cargo test $FLAGS -- --nocapture
publish:
name: Publish to crates.io
runs-on: ubuntu-latest
permissions:
id-token: write # Required for OIDC token exchange https://crates.io/docs/trusted-publishing
needs: test
defaults:
run:
working-directory: crates/muxox
steps:
- name: Checkout
uses: actions/checkout@v4
- uses: actions/cache@v4
with:
path: |
~/.cargo/bin/
~/.cargo/registry/index/
~/.cargo/registry/cache/
~/.cargo/git/db/
target/
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
- name: Setup Rust
run: rustup update stable && rustup default stable
- name: Verify tag matches version
run: |
TAG_VERSION=${GITHUB_REF#refs/tags/v}
CARGO_VERSION=$(cargo metadata --no-deps --format-version 1 | jq -r '.packages[0].version')
if [ "$TAG_VERSION" != "$CARGO_VERSION" ]; then
echo "Tag version ($TAG_VERSION) does not match Cargo.toml version ($CARGO_VERSION)"
exit 1
fi
# See Trusted publishing: https://crates.io/docs/trusted-publishing
- uses: rust-lang/crates-io-auth-action@v1
id: auth
- run: cargo publish
env:
CARGO_REGISTRY_TOKEN: ${{ steps.auth.outputs.token }}
release:
name: Create GitHub Release
runs-on: ubuntu-latest
needs: [test, publish]
permissions:
contents: write
steps:
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Extract tag name
id: tag
run: echo "tag=${GITHUB_REF#refs/tags/}" >> $GITHUB_OUTPUT
- name: Generate changelog
id: changelog
run: |
# Get the previous tag
PREV_TAG=$(git describe --tags --abbrev=0 HEAD^ 2>/dev/null || echo "")
# Generate changelog
if [ -n "$PREV_TAG" ]; then
echo "## What's Changed" > changelog.md
echo "" >> changelog.md
git log --pretty=format:"* %s (%h)" ${PREV_TAG}..HEAD >> changelog.md
echo "" >> changelog.md
echo "" >> changelog.md
echo "**Full Changelog**: https://github.com/${{ github.repository }}/compare/${PREV_TAG}...${{ steps.tag.outputs.tag }}" >> changelog.md
else
echo "## What's Changed" > changelog.md
echo "" >> changelog.md
echo "Initial release of muxox" >> changelog.md
echo "" >> changelog.md
echo "A small, ergonomic HTTP client wrapper around hyper with optional support for custom Root CAs and a dev-only insecure mode for self-signed certificates." >> changelog.md
fi
# Set the changelog as output (handle multiline)
echo "changelog<<EOF" >> $GITHUB_OUTPUT
cat changelog.md >> $GITHUB_OUTPUT
echo "EOF" >> $GITHUB_OUTPUT
- name: Create Release
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
if [[ "${{ steps.tag.outputs.tag }}" == *"-"* ]]; then
PRERELEASE_FLAG="--prerelease"
else
PRERELEASE_FLAG=""
fi
gh release create "${{ steps.tag.outputs.tag }}" \
--title "Release ${{ steps.tag.outputs.tag }}" \
--notes-file changelog.md \
$PRERELEASE_FLAG

7
.gitignore vendored Normal file
View File

@@ -0,0 +1,7 @@
*
!crates/**
!.gitignore
!Cargo.toml
!Cargo.lock
!muxox.toml
!README.md

1209
Cargo.lock generated Normal file

File diff suppressed because it is too large Load Diff

5
Cargo.toml Normal file
View File

@@ -0,0 +1,5 @@
[workspace]
members = [
"crates/*"
]
default-members = ["crates/muxox"]

94
README.md Normal file
View File

@@ -0,0 +1,94 @@
# muxox
Run all your dev services from one terminal.
`muxox` is a cli-based service orchestrator that makes it easy to start, stop, and monitor multiple processes during development—without juggling a bunch of windows or tabs.
- Service orchestration
- Live status
- Log viewer
- Simple config
- Start, stop, restart with quick keys
## Key bindings
- ↑ / ↓: Select a service
- Enter: Start/stop the selected service
- r: Restart the selected service
- q: Quit Muxox
## Quick start
### Install
```bash
cargo install muxox
```
1) Create a muxox.toml file in your project:
```toml
[[service]]
name = "frontend"
cmd = "pnpm client:dev"
cwd = "./"
log_capacity = 5000
[[service]]
name = "backend"
cmd = "pnpm server:dev"
cwd = "./"
```
2) Run Muxox:
```bash
muxox
```
3) Optional: point to a custom config:
```bash
muxox --config path/to/muxox.toml
```
## Configuration
Each service supports:
- name: Unique identifier (required)
- cmd: Command to run (required)
- cwd: Working directory (optional, defaults to current dir)
- log_capacity: How many log lines to keep in memory (optional, default 2000)
Tips:
- Use cwd to run commands from anywhere.
- Pick log_capacity large enough to cover your typical debugging session, but not so large that it eats RAM.
## Troubleshooting
- Not working on platform X
- File an issue with details.
- Command not found
- Ensure the command in cmd is installed and available on your PATH in the shell that launches Muxox.
- Permission denied
- Check file permissions or try adjusting the command (e.g., scripts may require execute permission).
- Logs look truncated
- Increase log_capacity in your muxox.toml to keep more history.
- Colors look off
- Use a terminal that supports true color and make sure its enabled.
## FAQ
- How is this different from a terminal multiplexer like tmux?
- Muxox focuses on orchestrating processes and their logs with simple controls, not on managing panes or sessions.
- Do I need containers or a specific runtime?
- No. Muxox runs your local commands directly.
- Can I use it for production?
- Muxox is designed for development workflows. For production, consider a proper process supervisor or orchestrator.
## Requirements
- Unix-like OS (Linux, macOS)
- A terminal with true color support
- All commands referenced in your config must be available in PATH
## License
MIT License
Copyright (c) 2025 Geoff Seemueller

506
crates/muxox/src/main.rs Normal file
View File

@@ -0,0 +1,506 @@
use std::{
collections::VecDeque,
fs,
io,
path::{Path, PathBuf},
process::{Child, Command, Stdio},
time::Duration,
};
use anyhow::{Context, Result};
use clap::Parser;
use crossterm::event::{self, Event, KeyCode, KeyEvent, KeyModifiers};
use crossterm::terminal::{disable_raw_mode, enable_raw_mode};
use ratatui::{
backend::CrosstermBackend,
layout::{Constraint, Direction, Layout},
style::{Color, Modifier, Style},
text::{Line, Span},
widgets::{Block, Borders, List, ListItem, Paragraph, Wrap},
Terminal,
};
use serde::Deserialize;
use tokio::{io::AsyncBufReadExt, process::Command as AsyncCommand, sync::mpsc, task, time};
#[cfg(windows)]
use std::os::windows::process::CommandExt as _;
#[derive(Debug, Parser)]
#[command(author, version, about = "Run multiple dev servers with a simple TUI.")]
struct Cli {
/// Optional path to a services config (TOML). If omitted, looks in: $PWD/muxox.toml then app dirs.
#[arg(short, long)]
config: Option<PathBuf>,
}
#[derive(Debug, Deserialize, Clone)]
struct Config {
service: Vec<ServiceCfg>,
}
#[derive(Debug, Deserialize, Clone)]
struct ServiceCfg {
name: String,
cmd: String,
cwd: Option<PathBuf>,
/// Keep last N log lines in memory
#[serde(default = "default_log_capacity")]
log_capacity: usize,
}
fn default_log_capacity() -> usize { 2000 }
#[cfg(test)]
mod tests {
use super::*;
use std::path::PathBuf;
#[test]
fn test_default_log_capacity() {
assert_eq!(default_log_capacity(), 2000);
}
#[test]
fn test_service_cfg_deserialize() {
let toml_input = r#"
name = "test-service"
cmd = "echo hello"
cwd = "/tmp"
log_capacity = 1000
"#;
let cfg: ServiceCfg = toml::from_str(toml_input).expect("Valid ServiceCfg");
assert_eq!(cfg.name, "test-service");
assert_eq!(cfg.cmd, "echo hello");
assert_eq!(cfg.cwd, Some(PathBuf::from("/tmp")));
assert_eq!(cfg.log_capacity, 1000);
}
#[test]
fn test_service_cfg_default_log_capacity() {
let toml_input = r#"
name = "test-service"
cmd = "echo hello"
"#;
let cfg: ServiceCfg = toml::from_str(toml_input).expect("Valid ServiceCfg");
assert_eq!(cfg.log_capacity, default_log_capacity());
}
#[test]
fn test_config_deserialize() {
let toml_input = r#"
[[service]]
name = "frontend"
cmd = "pnpm client:dev"
cwd = "./"
log_capacity = 5000
[[service]]
name = "backend"
cmd = "pnpm server:dev"
cwd = "./"
"#;
let cfg: Config = toml::from_str(toml_input).expect("Valid Config");
assert_eq!(cfg.service.len(), 2);
assert_eq!(cfg.service[0].name, "frontend");
assert_eq!(cfg.service[0].cmd, "pnpm client:dev");
assert_eq!(cfg.service[0].cwd, Some(PathBuf::from("./")));
assert_eq!(cfg.service[0].log_capacity, 5000);
assert_eq!(cfg.service[1].name, "backend");
assert_eq!(cfg.service[1].cmd, "pnpm server:dev");
assert_eq!(cfg.service[1].cwd, Some(PathBuf::from("./")));
assert_eq!(cfg.service[1].log_capacity, default_log_capacity());
}
#[test]
fn test_service_state() {
let cfg = ServiceCfg {
name: "test".to_string(),
cmd: "echo hello".to_string(),
cwd: None,
log_capacity: 10,
};
let mut state = ServiceState::new(cfg.clone());
// Test initial state
assert_eq!(state.status, Status::Stopped);
assert!(state.child.is_none());
assert_eq!(state.log.len(), 0);
// The ServiceState::new function enforces a minimum capacity of 256
assert_eq!(state.log.capacity(), 256);
// Test log functionality
state.push_log("line 1");
state.push_log("line 2");
assert_eq!(state.log.len(), 2);
// Test log capacity
for i in 3..=12 {
state.push_log(format!("line {}", i));
}
// Should have removed oldest entries to stay within capacity
assert_eq!(state.log.len(), 10);
assert_eq!(state.log.front().unwrap(), "line 3");
assert_eq!(state.log.back().unwrap(), "line 12");
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Status { Stopped, Starting, Running, Stopping }
#[derive(Debug)]
struct ServiceState {
cfg: ServiceCfg,
status: Status,
child: Option<Child>,
log: VecDeque<String>,
}
impl ServiceState {
fn new(cfg: ServiceCfg) -> Self {
Self {
log: VecDeque::with_capacity(cfg.log_capacity.max(256)),
cfg,
status: Status::Stopped,
child: None,
}
}
fn push_log(&mut self, line: impl Into<String>) {
if self.log.len() == self.cfg.log_capacity { self.log.pop_front(); }
self.log.push_back(line.into());
}
}
#[derive(Debug)]
struct App {
services: Vec<ServiceState>,
selected: usize,
tx: mpsc::UnboundedSender<AppMsg>,
}
#[derive(Debug)]
enum AppMsg {
Started(usize),
Stopped(usize, i32),
Log(usize, String),
AbortedAll,
}
#[tokio::main]
async fn main() -> Result<()> {
let cli = Cli::parse();
let cfg = load_config(cli.config.as_deref())?;
let (tx, mut rx) = mpsc::unbounded_channel::<AppMsg>();
let mut app = App { services: cfg.service.into_iter().map(ServiceState::new).collect(), selected: 0, tx: tx.clone() };
// Signal watcher: on any exit signal, nuke children then exit.
task::spawn(signal_watcher(tx.clone()));
// TUI setup
enable_raw_mode()?;
let mut stdout = io::stdout();
crossterm::execute!(stdout, crossterm::terminal::EnterAlternateScreen, crossterm::event::EnableMouseCapture)?;
let backend = CrosstermBackend::new(stdout);
let mut terminal = Terminal::new(backend)?;
// Render loop
let ui_task = task::spawn(async move {
let mut last_tick = time::Instant::now();
let tick_rate = Duration::from_millis(150);
loop {
// Draw
terminal.draw(|f| draw_ui(f, &app)).ok();
// Input or tick
let timeout = tick_rate.saturating_sub(last_tick.elapsed());
let mut handled = false;
if event::poll(timeout).unwrap_or(false) {
if let Event::Key(k) = event::read().unwrap_or(Event::FocusGained) {
handled = handle_key(k, &mut app);
}
}
if handled { /* app mutated, redraw next loop */ }
if last_tick.elapsed() >= tick_rate { last_tick = time::Instant::now(); }
// Drain channel
while let Ok(msg) = rx.try_recv() { apply_msg(&mut app, msg); }
}
});
// Wait until UI task ends (it never does gracefully). If it errors, fallthrough.
let _ = ui_task.await;
Ok(())
}
fn draw_ui(f: &mut ratatui::Frame, app: &App) {
let chunks = Layout::default()
.direction(Direction::Horizontal)
.constraints([Constraint::Percentage(30), Constraint::Percentage(70)])
.split(f.area());
let items: Vec<ListItem> = app.services.iter().enumerate().map(|(_i, s)| {
let status = match s.status { Status::Stopped=>"", Status::Starting=>"", Status::Running=>"", Status::Stopping=>"" };
let color = match s.status { Status::Running=>Color::Green, Status::Starting=>Color::Yellow, Status::Stopping=>Color::Magenta, Status::Stopped=>Color::DarkGray };
ListItem::new(Line::from(vec![Span::styled(format!(" {status} "), Style::default().fg(color)), Span::raw(&s.cfg.name)]))
}).collect();
let list = List::new(items)
.block(Block::default().title("Services (↑/↓ select, Enter start/stop, r restart, c clear, q quit)").borders(Borders::ALL))
.highlight_style(Style::default().add_modifier(Modifier::BOLD).bg(Color::DarkGray));
f.render_stateful_widget(list, chunks[0], &mut list_state(app.selected));
// Right pane: logs
let right_chunks = Layout::default()
.direction(Direction::Vertical)
.constraints([Constraint::Length(3), Constraint::Min(1)])
.split(chunks[1]);
let selected = &app.services[app.selected];
let header = Paragraph::new(vec![
Line::from(vec![Span::styled("Name: ", Style::default().fg(Color::DarkGray)), Span::raw(&selected.cfg.name)]),
Line::from(vec![Span::styled("Cmd: ", Style::default().fg(Color::DarkGray)), Span::raw(&selected.cfg.cmd)]),
Line::from(vec![Span::styled("Cwd: ", Style::default().fg(Color::DarkGray)), Span::raw(selected.cfg.cwd.as_ref().and_then(|p| p.to_str()).unwrap_or("."))]),
])
.block(Block::default().title("Selected Service").borders(Borders::ALL));
let log_text: Vec<Line> = selected.log.iter().map(|l| Line::from(l.clone())).collect();
let log = Paragraph::new(log_text)
.wrap(Wrap { trim: false })
.block(Block::default().title("Logs").borders(Borders::ALL));
f.render_widget(header, right_chunks[0]);
f.render_widget(log, right_chunks[1]);
}
fn list_state(selected: usize) -> ratatui::widgets::ListState {
let mut state = ratatui::widgets::ListState::default();
state.select(Some(selected));
state
}
fn handle_key(k: KeyEvent, app: &mut App) -> bool {
match (k.code, k.modifiers) {
(KeyCode::Char('q'), _) | (KeyCode::Esc, _) => {
// Quit: perform cleanup
cleanup_and_exit(app);
return true;
}
(KeyCode::Down, _) => { app.selected = (app.selected + 1).min(app.services.len()-1); return true; }
(KeyCode::Up, _) => { if app.selected>0 { app.selected -= 1; } return true; }
(KeyCode::Enter, _) | (KeyCode::Char(' '), _) => { toggle_selected(app); return true; }
(KeyCode::Char('r'), _) => { restart_selected(app); return true; }
(KeyCode::Char('c'), _) if k.modifiers == KeyModifiers::NONE => { app.services[app.selected].log.clear(); return true; }
_ => {}
}
false
}
fn toggle_selected(app: &mut App) {
let idx = app.selected;
match app.services[idx].status { Status::Stopped => { start_service(idx, app); }, Status::Running | Status::Starting => { stop_service(idx, app); }, Status::Stopping => {} }
}
fn restart_selected(app: &mut App) { let idx = app.selected; stop_service(idx, app); start_service(idx, app); }
fn start_service(idx: usize, app: &mut App) {
if matches!(app.services[idx].status, Status::Running | Status::Starting) { return; }
app.services[idx].status = Status::Starting;
let tx = app.tx.clone();
let sc = app.services[idx].cfg.clone();
task::spawn(async move {
// Build command under a shell
let mut cmd = AsyncCommand::new(shell_program());
cmd.arg(shell_flag()).arg(shell_exec(&sc.cmd));
if let Some(cwd) = sc.cwd.clone() { cmd.current_dir(cwd); }
cmd.stdout(Stdio::piped()).stderr(Stdio::piped());
set_process_group(&mut cmd);
match cmd.spawn() {
Ok(mut child) => {
let _pid = child.id().unwrap_or_default();
let _ = tx.send(AppMsg::Started(idx));
// stdout
if let Some(out) = child.stdout.take() {
let tx2 = tx.clone();
task::spawn(async move {
let mut reader = tokio::io::BufReader::new(out).lines();
while let Ok(Some(line)) = reader.next_line().await { let _ = tx2.send(AppMsg::Log(idx, line)); }
});
}
// stderr
if let Some(err) = child.stderr.take() {
let tx2 = tx.clone();
task::spawn(async move {
let mut reader = tokio::io::BufReader::new(err).lines();
while let Ok(Some(line)) = reader.next_line().await { let _ = tx2.send(AppMsg::Log(idx, format!("[stderr] {line}"))); }
});
}
// Waiter
let status = child.wait().await; // process exit
let code = status.map(|s| s.code().unwrap_or(-1)).unwrap_or(-1);
let _ = tx.send(AppMsg::Stopped(idx, code));
}
Err(e) => { let _ = tx.send(AppMsg::Log(idx, format!("spawn failed: {e}"))); let _ = tx.send(AppMsg::Stopped(idx, -1)); }
}
});
}
fn stop_service(idx: usize, app: &mut App) {
let sc = &mut app.services[idx];
if !matches!(sc.status, Status::Running | Status::Starting) { return; }
sc.status = Status::Stopping;
sc.push_log("Stopping...");
if let Some(child) = sc.child.take() { drop(child); } // actual kill handled by kill_tree below
kill_tree(idx, app);
}
fn apply_msg(app: &mut App, msg: AppMsg) {
match msg {
AppMsg::Started(i) => {
app.services[i].status = Status::Running;
app.services[i].push_log("[started]");
}
AppMsg::Stopped(i, code) => {
let s = &mut app.services[i];
s.status = Status::Stopped;
s.push_log(format!("[exited: code {code}]").as_str());
}
AppMsg::Log(i, line) => { app.services[i].push_log(line); }
AppMsg::AbortedAll => { /* UI can optionally display something */ }
}
}
fn cleanup_and_exit(app: &mut App) {
// Restore terminal first to avoid leaving it raw if we panic later.
let _ = disable_raw_mode();
let mut stdout = io::stdout();
let _ = crossterm::execute!(stdout, crossterm::event::DisableMouseCapture, crossterm::terminal::LeaveAlternateScreen);
// Kill all children forcefully
kill_all(app);
std::process::exit(0);
}
fn kill_all(app: &mut App) {
for i in 0..app.services.len() { kill_tree(i, app); }
}
fn load_config(provided: Option<&Path>) -> Result<Config> {
let candidates: Vec<PathBuf> = match provided {
Some(p) => vec![p.to_path_buf()],
None => {
let mut v = vec![PathBuf::from("muxox.toml")];
if let Some(proj) = directories::ProjectDirs::from("dev", "local", "devmux") { v.push(proj.config_dir().join("muxox.toml")); }
v
}
};
for path in candidates {
if path.exists() {
let data = fs::read_to_string(&path)?;
return Ok(toml::from_str(&data).with_context(|| format!("parsing {path:?}"))?);
}
}
anyhow::bail!("No config found; create muxox.toml or pass --config <path>")
}
#[cfg(unix)]
fn set_process_group(cmd: &mut AsyncCommand) {
unsafe { cmd.pre_exec(|| { libc::setsid(); Ok(()) }) };
}
#[cfg(windows)]
fn set_process_group(cmd: &mut AsyncCommand) {
const CREATE_NEW_PROCESS_GROUP: u32 = 0x00000200;
const CREATE_NEW_CONSOLE: u32 = 0x00000010; // better isolation for signals
cmd.creation_flags(CREATE_NEW_PROCESS_GROUP | CREATE_NEW_CONSOLE);
}
#[cfg(unix)]
fn shell_program() -> &'static str {
if std::env::var("SHELL").ok().filter(|s| !s.is_empty()).is_some() {
// Can't return a dynamically created String as &'static str
// For simplicity, return a common shell path
"/bin/bash"
} else {
"/bin/sh"
}
}
#[cfg(unix)]
fn shell_flag() -> &'static str { "-lc" }
#[cfg(unix)]
fn shell_exec(cmd: &str) -> String { cmd.to_string() }
#[cfg(windows)]
fn shell_program() -> &'static str { "cmd.exe" }
#[cfg(windows)]
fn shell_flag() -> &'static str { "/C" }
#[cfg(windows)]
fn shell_exec(cmd: &str) -> String { cmd.to_string() }
#[cfg(unix)]
fn kill_tree(idx: usize, app: &mut App) {
// These imports are left as warnings intentionally since they might be needed
// if we implement more advanced process group management in the future
use nix::sys::signal::{killpg, Signal};
use nix::unistd::Pid;
let name = app.services[idx].cfg.name.clone();
// We don't track exact pgid; setsid() made child leader so killpg(-pid) works if we had it.
// As we don't keep the pid, we issue a shell-level killall using process group via sh -c.
// Simpler: store no pid; rely on pkill -f cmd as fallback.
let cmdline = &app.services[idx].cfg.cmd;
// best-effort group kill via pkill
let _ = Command::new("pkill").arg("-TERM").arg("-f").arg(cmdline).status();
std::thread::sleep(Duration::from_millis(250));
let _ = Command::new("pkill").arg("-KILL").arg("-f").arg(cmdline).status();
app.services[idx].push_log(format!("[killed {name}]"));
}
#[cfg(windows)]
fn kill_tree(idx: usize, app: &mut App) {
let name = app.services[idx].cfg.name.clone();
// Use taskkill to nuke the subtree
let _ = Command::new("taskkill").args(["/F","/T","/FI"]).arg(format!("WINDOWTITLE eq {}", name)).status();
// fallback: taskkill by image name is too coarse; skip
app.services[idx].push_log(format!("[killed {name}]"));
}
#[cfg(unix)]
async fn signal_watcher(tx: mpsc::UnboundedSender<AppMsg>) {
use tokio::signal::unix::{signal, SignalKind};
// Create and pin futures
let ctrlc = tokio::signal::ctrl_c();
let sigterm = signal(SignalKind::terminate()).expect("sigterm");
tokio::pin!(ctrlc, sigterm);
// For Unix systems, wait for either Ctrl-C or SIGTERM
loop {
tokio::select! {
_ = &mut ctrlc => { let _=tx.send(AppMsg::AbortedAll); break; }
_ = sigterm.recv() => { let _=tx.send(AppMsg::AbortedAll); break; }
}
}
}
#[cfg(not(unix))]
async fn signal_watcher(tx: mpsc::UnboundedSender<AppMsg>) {
// Create and pin futures
let ctrlc = tokio::signal::ctrl_c();
tokio::pin!(ctrlc);
// For non-Unix systems, just wait for Ctrl-C
loop {
tokio::select! {
_ = &mut ctrlc => { let _=tx.send(AppMsg::AbortedAll); break; }
}
}
}

View File

@@ -0,0 +1,128 @@
use serde::Deserialize;
use std::path::PathBuf;
// These are independent structs for testing, deliberately not using the internal types
#[derive(Debug, Deserialize)]
struct TestService {
name: String,
cmd: String,
#[serde(default)]
cwd: Option<PathBuf>,
#[serde(default = "default_log_capacity")]
log_capacity: usize,
}
fn default_log_capacity() -> usize {
2000
}
#[derive(Debug, Deserialize)]
struct TestConfig {
#[serde(rename = "service")]
services: Vec<TestService>,
}
#[test]
fn parses_sample_config() {
let toml_input = r#"
[[service]]
name = "frontend"
cmd = "pnpm client:dev"
cwd = "./"
log_capacity = 5000
[[service]]
name = "backend"
cmd = "pnpm server:dev"
cwd = "./"
"#;
let cfg: TestConfig = toml::from_str(toml_input).expect("valid muxox.toml");
assert_eq!(cfg.services.len(), 2);
assert_eq!(cfg.services[0].name, "frontend");
assert_eq!(cfg.services[0].cmd, "pnpm client:dev");
assert_eq!(cfg.services[0].cwd, Some(PathBuf::from("./")));
assert_eq!(cfg.services[0].log_capacity, 5000);
assert_eq!(cfg.services[1].name, "backend");
assert_eq!(cfg.services[1].cmd, "pnpm server:dev");
assert_eq!(cfg.services[1].cwd, Some(PathBuf::from("./")));
assert_eq!(cfg.services[1].log_capacity, 2000); // default value
}
#[test]
fn handles_missing_optional_fields() {
let toml_input = r#"
[[service]]
name = "minimal"
cmd = "echo hello"
"#;
let cfg: TestConfig = toml::from_str(toml_input).expect("valid minimal config");
assert_eq!(cfg.services.len(), 1);
assert_eq!(cfg.services[0].name, "minimal");
assert_eq!(cfg.services[0].cmd, "echo hello");
assert_eq!(cfg.services[0].cwd, None);
assert_eq!(cfg.services[0].log_capacity, 2000); // default value
}
#[test]
#[should_panic(expected = "missing field `name`")]
fn fails_on_missing_required_name() {
let toml_input = r#"
[[service]]
cmd = "echo hello"
"#;
let _: TestConfig = toml::from_str(toml_input).expect("should fail on missing name");
}
#[test]
#[should_panic(expected = "missing field `cmd`")]
fn fails_on_missing_required_cmd() {
let toml_input = r#"
[[service]]
name = "invalid"
"#;
let _: TestConfig = toml::from_str(toml_input).expect("should fail on missing cmd");
}
#[test]
fn empty_service_array_is_valid() {
// A config with an empty service array is valid
let toml_input = "service = []";
let cfg: TestConfig = toml::from_str(toml_input).expect("config with empty service array should be valid");
assert_eq!(cfg.services.len(), 0);
}
#[test]
#[should_panic(expected = "missing field `service`")]
fn missing_service_field_is_invalid() {
// A completely empty config is invalid because the service field is required
let toml_input = "";
let _: TestConfig = toml::from_str(toml_input).expect("empty config should be invalid");
}
#[test]
fn parses_real_muxox_toml() {
// Try to parse the actual muxox.toml file from the repository root
let result = std::fs::read_to_string("../../muxox.toml");
if let Ok(contents) = result {
let cfg: TestConfig = toml::from_str(&contents).expect("real muxox.toml should be valid");
assert!(!cfg.services.is_empty(), "muxox.toml should have at least one service");
// Verify it has the expected services (these assertions depend on the actual file content)
let service_names: Vec<&str> = cfg.services.iter().map(|s| s.name.as_str()).collect();
assert!(service_names.contains(&"frontend"), "Should have a frontend service");
assert!(service_names.contains(&"backend"), "Should have a backend service");
} else {
// Test is still valuable even if we can't find the real file
println!("Note: Could not find ../../muxox.toml, skipping part of test");
}
}

View File

@@ -0,0 +1,80 @@
// Platform specific tests
// These tests are conditionally compiled based on the target platform
#[cfg(unix)]
mod unix_tests {
// Test Unix-specific functionality
// Since we can't directly access internal functions from integration tests,
// we test platform-specific behavior instead
#[test]
fn test_unix_shell_detection() {
// This is a simple test to verify that we're on a Unix platform
// The actual shell_program function is private in main.rs
assert!(cfg!(unix), "This test should only run on Unix platforms");
// We can test that standard Unix directories exist
assert!(std::path::Path::new("/bin/sh").exists() || std::path::Path::new("/usr/bin/sh").exists(),
"Expected to find a shell at /bin/sh or /usr/bin/sh on Unix");
}
#[test]
fn test_unix_signals() {
// We can test that nix/signal functionality works as expected
use nix::sys::signal::{Signal, SigSet};
// Create a signal set and verify basic operations
let mut set = SigSet::empty();
set.add(Signal::SIGTERM);
assert!(set.contains(Signal::SIGTERM));
assert!(!set.contains(Signal::SIGINT));
}
}
#[cfg(windows)]
mod windows_tests {
#[test]
fn test_windows_platform() {
assert!(cfg!(windows), "This test should only run on Windows platforms");
}
}
// Cross-platform tests that should work on any platform
#[test]
fn test_process_creation() {
use std::process::Command;
// A simple command that should work on any platform
let output = if cfg!(windows) {
Command::new("cmd").args(["/C", "echo hello"]).output()
} else {
Command::new("sh").args(["-c", "echo hello"]).output()
};
// Verify we can create processes
assert!(output.is_ok(), "Should be able to create a basic process");
if let Ok(output) = output {
let stdout = String::from_utf8_lossy(&output.stdout);
// On Windows, echo adds CRLF, on Unix just LF
let expected = if cfg!(windows) { "hello\r\n" } else { "hello\n" };
assert!(stdout.contains("hello"), "Expected 'hello' in output");
// Optionally, do a more precise check with the expected output format
assert!(stdout.trim() == "hello" || stdout == expected,
"Output should be exactly 'hello' with optional newline formatting");
}
}
// Test for the environment-dependent features
#[test]
fn test_environment_detection() {
if cfg!(unix) {
// Unix environment checks
assert!(std::path::Path::new("/").exists(), "Root directory should exist on Unix");
} else if cfg!(windows) {
// Windows environment checks
assert!(std::path::Path::new("C:\\").exists() ||
std::path::Path::new("D:\\").exists(),
"Expected to find C: or D: drive on Windows");
}
}

10
muxox.toml Normal file
View File

@@ -0,0 +1,10 @@
[[service]]
name = "frontend"
cmd = "pnpm client:dev"
cwd = "./"
log_capacity = 5000
[[service]]
name = "backend"
cmd = "pnpm server:dev"
cwd = "./"