diff options
| author | Doan Luu Duc Tai <doan@luuduct.ai> | 2026-10-31 13:08:08 -0500 |
|---|---|---|
| committer | Doan Luu Duc Tai <doan@luuduct.ai> | 2026-10-31 13:08:08 -0500 |
| commit | a80df869a92c330fbad9695057720b11a18af901 (patch) | |
| tree | 5307272821cb3adc3f0348413a3b904290890044 /src/app/process.rs | |
| parent | 9139398d465f5899cc31bc4cdd54daa7392ca550 (diff) | |
Forbid Rust lints and enforce strict typed TypeScript linting
Diffstat (limited to 'src/app/process.rs')
| -rw-r--r-- | src/app/process.rs | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/src/app/process.rs b/src/app/process.rs new file mode 100644 index 0000000..02f6a6b --- /dev/null +++ b/src/app/process.rs @@ -0,0 +1,70 @@ +// Copyright (C) 2026 Doan Luu Duc Tai <doan@luuduct.ai> +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as +// published by the Free Software Foundation, either version 3 of the +// License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see <https://www.gnu.org/licenses/>. + +use std::fmt; +use std::io::{self, Write}; +use std::process::{Command, Output}; + +use anyhow::{Context, Result, bail}; + +pub(super) fn capture(command: &mut Command) -> Result<Output> { + let output = command.output().with_context(|| { + format!( + "Could not start {}", + command.get_program().to_string_lossy() + ) + })?; + if !output.status.success() { + bail!( + "{} failed ({}): {}", + command.get_program().to_string_lossy(), + output.status, + String::from_utf8_lossy(&output.stderr).trim() + ); + } + Ok(output) +} + +pub(super) fn text(command: &mut Command) -> Result<String> { + String::from_utf8(capture(command)?.stdout).context("Command returned non-UTF-8 output") +} + +pub(super) fn run(command: &mut Command) -> Result<()> { + let status = command.status().with_context(|| { + format!( + "Could not start {}", + command.get_program().to_string_lossy() + ) + })?; + if !status.success() { + bail!( + "{} failed ({status})", + command.get_program().to_string_lossy() + ); + } + Ok(()) +} + +pub(super) fn available(command: &mut Command) -> bool { + command.output().is_ok_and(|output| output.status.success()) +} + +pub(super) fn line(message: fmt::Arguments<'_>) -> Result<()> { + writeln!(io::stdout().lock(), "{message}").context("Could not write command output") +} + +pub(super) fn error(message: fmt::Arguments<'_>) -> io::Result<()> { + writeln!(io::stderr().lock(), "{message}") +} |
