Rust

Console

Console helpers: coloured log lines, prompts, process titles, shell-safe commands, and running commands with input, output, progress and a timeout.
Rust 1,056 lines (547 code)split
PartCodeCommentsBlankTotalFiles
Rust source547418911,0564
Rust tests3102331

Comments include doc comments. Rust tests are tests/ and #[cfg(test)] modules.

Install#

The crate lives in the Appwrite repository; depend on it from Git.

Cargo.toml
[dependencies]
utopia-console = { git = "https://github.com/appwrite/appwrite" }

Getting started#

Common tasks with Console, each a complete program you can copy and run.

Command-line programs log progress, ask questions, and run other programs. This crate writes coloured log lines, builds command lines that cannot be injected into, and runs them with an input, a timeout and live output.

Log lines

Plain, green and blue lines go to standard output, red and yellow ones to standard error. Each call returns the bytes it wrote.

Rust
use utopia_console::Console;

let mut console = Console::new(Vec::new(), Vec::new());
console.log("starting")?;
console.info("12 files to upload")?;
assert_eq!(console.out(), b"starting\n\x1b[34m12 files to upload\x1b[0m\n");

console.warning("disk almost full")?;
assert_eq!(console.err(), b"\x1b[1;33mdisk almost full\x1b[0m\n");

Build a command safely

Each flag, option and argument is one word, checked as it is added; a shell string quotes every word, so a value can never run as code.

Rust
use utopia_console::Command;

let command = Command::new("php")?.option("-r", "echo 'hello'; rm -rf /")?;
assert_eq!(command.to_shell()?, "'php' '-r' 'echo '\\''hello'\\''; rm -rf /'");

let error = Command::new("git")?.flag("verbose").unwrap_err();
assert_eq!(error.to_string(), "Invalid command flag: verbose");

Run a command

execute passes the input, collects both outputs, reports progress, and kills a command that outlives its timeout (status 1).

Rust
use std::time::Duration;
use utopia_console::{execute, Command};

let pipeline = Command::pipe(vec![
    Command::new("printf")?.argument("alpha\nbeta\n")?,
    Command::new("grep")?.argument("beta")?,
])?;
let mut seen = Vec::new();
let mut progress = |chunk: &[u8]| seen.extend_from_slice(chunk);
let output = execute(&pipeline, b"", Some(Duration::from_secs(10)), Some(&mut progress)).await?;
assert_eq!(output.status, 0);
assert_eq!(output.stdout, b"beta\n");
assert_eq!(seen, b"beta\n");

let slow = execute("sleep 5", b"", Some(Duration::from_secs(1)), None).await?;
assert_eq!(slow.status, 1);

Reference#

Console helpers for command-line programs: coloured log lines, prompts, the process title, shell-safe Commands, running commands with execute, and repeating work with repeat.

Command builds a command line word by word, validating each one, and never by concatenating strings: a plain command runs without a shell, and pipelines, &&/|| lists, groups and redirections render with every word quoted. execute runs a command with an input, collects its output (reporting progress as it arrives) and kills it after a timeout.

Rust
use utopia_console::{Command, Console};

let deploy = Command::and(vec![
    Command::group(Command::or(vec![Command::new("build")?, Command::new("build:fallback")?])?),
    Command::new("publish")?,
])?;
assert_eq!(deploy.to_shell()?, "( 'build' || 'build:fallback' ) && 'publish'");

let mut console = Console::new(Vec::new(), Vec::new());
assert_eq!(console.info("deploying")?, 19);

Modules#

Each module groups one concept of the library. Internal ones hold items the crate re-exports at its root.

API#

What utopia_console exports at its root.