Compare commits

..

9 commits

Author SHA1 Message Date
Quantum 2109cd45aa Document cargo install in README.md 2024-10-15 02:32:20 -04:00
Quantum a5e9f7e6fe Fix license tag 2024-10-15 02:27:11 -04:00
Quantum 4ef2eab3db Ignore .github in published crate 2024-10-15 02:22:35 -04:00
Quantum 6faafdd7ae Bump version to 0.1.2 and add cargo metadata 2024-10-15 02:18:34 -04:00
Quantum 58f0ba9952 Clean up use statements 2024-10-15 02:05:39 -04:00
Quantum 7e74fc8888 Forward child process return code if possible 2024-10-15 02:02:39 -04:00
Quantum ffb1c1c8b5 Bump version to 0.1.1 2024-10-14 21:25:22 -04:00
Quantum d59da1aeb9 Shorten section tags and improve formatting 2024-10-14 19:20:09 -04:00
Quantum 5a464aa3b1 Use simpler else clause with tokio::select! 2024-10-14 19:02:29 -04:00
6 changed files with 50 additions and 36 deletions

2
Cargo.lock generated
View file

@ -592,7 +592,7 @@ dependencies = [
[[package]] [[package]]
name = "ntfy-run" name = "ntfy-run"
version = "0.1.0" version = "0.1.2"
dependencies = [ dependencies = [
"clap", "clap",
"itertools", "itertools",

View file

@ -1,7 +1,19 @@
[package] [package]
name = "ntfy-run" name = "ntfy-run"
version = "0.1.0" version = "0.1.2"
edition = "2021" edition = "2021"
authors = ["Quantum <me@quantum5.ca>"]
description = "ntfy-run is a tool to run a command, capture its output, and send it to a ntfy server."
readme = "README.md"
homepage = "https://github.com/quantum5/ntfy-run"
repository = "https://github.com/quantum5/ntfy-run"
license = "GPL-3.0-or-later"
keywords = ["ntfy", "cron", "notifications", "utility"]
categories = ["command-line-interface"]
exclude = [
".github/*"
]
[dependencies] [dependencies]
clap = { version = "4.5.20", features = ["derive", "env"] } clap = { version = "4.5.20", features = ["derive", "env"] }

View file

@ -16,7 +16,13 @@ sudo wget https://github.com/quantum5/ntfy-run/releases/latest/download/ntfy-run
sudo chmod a+x /usr/local/bin/ntfy-run sudo chmod a+x /usr/local/bin/ntfy-run
``` ```
Alternatively, build it yourself: You can also install the latest release with `cargo`:
```bash
cargo install ntfy-run
```
Finally, you can build the latest Git version yourself:
```bash ```bash
git clone https://github.com/quantum5/ntfy-run.git git clone https://github.com/quantum5/ntfy-run.git

View file

@ -1,13 +1,13 @@
use crate::runner::CaptureError; use crate::runner::{CaptureError, CapturedOutput};
use clap::Parser; use clap::Parser;
use runner::CapturedOutput; use std::process::exit;
mod quote; mod quote;
mod runner; mod runner;
mod tap_stream; mod tap_stream;
#[derive(Parser)] #[derive(Parser)]
/// Tool to run a command, capture its output, and send it to ntfy. #[command(version, about)]
struct Cli { struct Cli {
/// URL of the ntfy server and topic, e.g. https://ntfy.sh/topic /// URL of the ntfy server and topic, e.g. https://ntfy.sh/topic
#[arg(short = 'n', long = "ntfy-url", env = "NTFY_URL", alias = "url")] #[arg(short = 'n', long = "ntfy-url", env = "NTFY_URL", alias = "url")]
@ -100,7 +100,8 @@ fn format_post_body(output: CapturedOutput) -> String {
}]; }];
if !output.errors.is_empty() { if !output.errors.is_empty() {
fragments.push("==================== Errors ====================".to_string()); fragments.push("".to_string());
fragments.push("========== Errors ==========".to_string());
for error in &output.errors { for error in &output.errors {
fragments.push(match error { fragments.push(match error {
CaptureError::Spawn(error) => format!("Spawn error: {}", error), CaptureError::Spawn(error) => format!("Spawn error: {}", error),
@ -109,19 +110,18 @@ fn format_post_body(output: CapturedOutput) -> String {
CaptureError::Wait(error) => format!("Error while waiting for process: {}", error), CaptureError::Wait(error) => format!("Error while waiting for process: {}", error),
}); });
} }
fragments.push("\n".to_string());
} }
if !output.stdout.is_empty() { if !output.stdout.is_empty() {
fragments.push("==================== STDOUT ====================".to_string()); fragments.push("".to_string());
fragments.push(String::from_utf8_lossy(&output.stdout).into_owned()); fragments.push("========== STDOUT ==========".to_string());
fragments.push("\n".to_string()); fragments.push(String::from_utf8_lossy(output.stdout.trim_ascii_end()).into_owned());
} }
if !output.stderr.is_empty() { if !output.stderr.is_empty() {
fragments.push("==================== STDERR ====================".to_string()); fragments.push("".to_string());
fragments.push(String::from_utf8_lossy(&output.stderr).into_owned()); fragments.push("========== STDERR ==========".to_string());
fragments.push("\n".to_string()); fragments.push(String::from_utf8_lossy(output.stderr.trim_ascii_end()).into_owned());
} }
fragments.join("\n") fragments.join("\n")
@ -210,7 +210,13 @@ async fn main() {
}; };
match request.send().await.and_then(|r| r.error_for_status()) { match request.send().await.and_then(|r| r.error_for_status()) {
Ok(_) => (), Ok(_) => exit(match status {
Err(error) => eprintln!("Failed to send request to ntfy: {}", error), Some(code) => code.code().unwrap_or(255),
None => 255,
}),
Err(error) => {
eprintln!("Failed to send request to ntfy: {}", error);
exit(37)
}
} }
} }

View file

@ -1,7 +1,6 @@
use crate::tap_stream::{ReadOrWrite, TapStream}; use crate::tap_stream::{ReadOrWrite, TapStream};
use std::process::{ExitStatus, Stdio}; use std::process::{ExitStatus, Stdio};
use tokio::process::Command; use tokio::{io, process::Command, select};
use tokio::{io, select};
pub enum CaptureError { pub enum CaptureError {
Spawn(io::Error), Spawn(io::Error),
@ -61,29 +60,20 @@ pub async fn run_forward_and_capture(cmdline: &Vec<String>) -> CapturedOutput {
result = stdout_tap.step(), if !stdout_eof => match result { result = stdout_tap.step(), if !stdout_eof => match result {
Ok(ReadOrWrite::Read(bytes)) => stdout.extend_from_slice(bytes), Ok(ReadOrWrite::Read(bytes)) => stdout.extend_from_slice(bytes),
Ok(ReadOrWrite::Written) => (), Ok(ReadOrWrite::Written) => (),
Ok(ReadOrWrite::EOF) => match (stderr_eof, maybe_status) { Ok(ReadOrWrite::EOF) => stdout_eof = true,
(true, Some(status)) => break status,
_ => stdout_eof = true,
},
Err(error) => errors.push(CaptureError::Stdout(error)), Err(error) => errors.push(CaptureError::Stdout(error)),
}, },
result = stderr_tap.step(), if !stderr_eof => match result { result = stderr_tap.step(), if !stderr_eof => match result {
Ok(ReadOrWrite::Read(bytes)) => stderr.extend_from_slice(bytes), Ok(ReadOrWrite::Read(bytes)) => stderr.extend_from_slice(bytes),
Ok(ReadOrWrite::Written) => (), Ok(ReadOrWrite::Written) => (),
Ok(ReadOrWrite::EOF) => match (stdout_eof, maybe_status) { Ok(ReadOrWrite::EOF) => stderr_eof = true,
(true, Some(status)) => break status,
_ => stderr_eof = true,
},
Err(error) => errors.push(CaptureError::Stderr(error)), Err(error) => errors.push(CaptureError::Stderr(error)),
}, },
status = child.wait(), if maybe_status.is_none() => match status { status = child.wait(), if maybe_status.is_none() => match status {
Ok(status) => if stdout_eof && stderr_eof { Ok(status) => maybe_status = Some(status),
break status;
} else {
maybe_status = Some(status);
},
Err(error) => errors.push(CaptureError::Wait(error)), Err(error) => errors.push(CaptureError::Wait(error)),
} },
else => break maybe_status.unwrap(),
} }
}; };

View file

@ -34,11 +34,11 @@ impl<R: AsyncRead + Unpin, W: AsyncWrite + Unpin> TapStream<R, W> {
self.buf_start = 0; self.buf_start = 0;
self.buf_end = bytes; self.buf_end = bytes;
Ok(if bytes == 0 { if bytes == 0 {
ReadOrWrite::EOF Ok(ReadOrWrite::EOF)
} else { } else {
ReadOrWrite::Read(&self.buffer[0..bytes]) Ok(ReadOrWrite::Read(&self.buffer[0..bytes]))
}) }
} else { } else {
let bytes = self let bytes = self
.target .target