From 139a95a8c3c33373d534a67cfa85ff0859886b11 Mon Sep 17 00:00:00 2001 From: Reilly Wood Date: Thu, 2 May 2024 18:00:10 -0700 Subject: [PATCH 1/2] Fix duct stderr being printed fix remove commented out line --- Cargo.lock | 1 - Cargo.toml | 1 - src/components/home.rs | 39 ++++++++++++++++++++++++++------------- src/components/logger.rs | 2 +- src/systemd.rs | 16 +++++++++++----- 5 files changed, 38 insertions(+), 21 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index bd6aec0..206bdb3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1796,7 +1796,6 @@ dependencies = [ "colored", "crossterm", "directories", - "duct", "env_logger", "futures", "indexmap", diff --git a/Cargo.toml b/Cargo.toml index a86930f..776e507 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -52,7 +52,6 @@ signal-hook = "0.3.15" tokio-util = "0.7.8" zbus = { version = "4.1.2", default-features = false, features = ["tokio"] } itertools = "0.12.0" -duct = "0.13.6" indexmap = "2.0.0" clipboard-anywhere = "0.2.2" chrono = { version = "0.4.31", default-features = false } diff --git a/src/components/home.rs b/src/components/home.rs index c9cf90f..505cfaa 100644 --- a/src/components/home.rs +++ b/src/components/home.rs @@ -1,5 +1,4 @@ use crossterm::event::{KeyCode, KeyEvent, KeyModifiers}; -use duct::cmd; use futures::Future; use indexmap::IndexMap; use itertools::Itertools; @@ -18,7 +17,10 @@ use tokio_util::sync::CancellationToken; use tracing::{error, info, warn}; use tui_input::{backend::crossterm::EventHandler, Input}; -use std::{process::Stdio, time::Duration}; +use std::{ + process::{Command, Stdio}, + time::Duration, +}; use super::{logger::Logger, Component, Frame}; use crate::{ @@ -365,7 +367,11 @@ impl Component for Home { let _ = tx.send(Action::SetUnitFilePath { unit: unit.clone(), path }); let _ = tx.send(Action::Render); }, - Err(e) => error!("Error getting unit file path for {}: {}", unit.name, e), + Err(e) => { + let _ = tx.send(Action::SetUnitFilePath { unit: unit.clone(), path: "(could not be determined)".into() }); + let _ = tx.send(Action::Render); + error!("Error getting unit file path for {}: {}", unit.name, e); + }, } // First, get the N lines in a batch @@ -380,17 +386,24 @@ impl Component for Home { args.push("--user"); } - match cmd("journalctl", args).read() { - Ok(stdout) => { - info!("Got logs for {} in {:?}", unit.name, start.elapsed()); - - let mut logs = stdout.split('\n').map(String::from).collect_vec(); - - if logs.is_empty() || logs[0].is_empty() { - logs.push(String::from("No logs found/available. Maybe try relaunching with `sudo systemctl-tui`")); + match Command::new("journalctl").args(&args).output() { + Ok(output) => { + if output.status.success() { + info!("Got logs for {} in {:?}", unit.name, start.elapsed()); + if let Ok(stdout) = std::str::from_utf8(&output.stdout) { + let mut logs = stdout.trim().split('\n').map(String::from).collect_vec(); + + if logs.is_empty() || logs[0].is_empty() { + logs.push(String::from("No logs found/available. Maybe try relaunching with `sudo systemctl-tui`")); + } + let _ = tx.send(Action::SetLogs { unit: unit.clone(), logs }); + let _ = tx.send(Action::Render); + } else { + warn!("Error parsing stdout for {}", unit.name); + } + } else { + warn!("Error getting logs for {}: {}", unit.name, String::from_utf8_lossy(&output.stderr)); } - let _ = tx.send(Action::SetLogs { unit: unit.clone(), logs }); - let _ = tx.send(Action::Render); }, Err(e) => warn!("Error getting logs for {}: {}", unit.name, e), } diff --git a/src/components/logger.rs b/src/components/logger.rs index 12a0f72..83318fe 100644 --- a/src/components/logger.rs +++ b/src/components/logger.rs @@ -24,7 +24,7 @@ impl Component for Logger { fn render(&mut self, f: &mut Frame<'_>, rect: Rect) { let w = TuiLoggerWidget::default() - .block(Block::default().title(" 📝 systemctl-tui logs").borders(Borders::ALL)) + .block(Block::default().title(" systemctl-tui logs ").borders(Borders::ALL)) .style_error(Style::default().fg(Color::Red)) .style_debug(Style::default().fg(Color::Green)) .style_warn(Style::default().fg(Color::Yellow)) diff --git a/src/systemd.rs b/src/systemd.rs index 60df69e..de43ae4 100644 --- a/src/systemd.rs +++ b/src/systemd.rs @@ -1,7 +1,8 @@ // File initially taken from https://github.com/servicer-labs/servicer/blob/master/src/utils/systemd.rs, since modified -use anyhow::Result; -use duct::cmd; +use std::process::Command; + +use anyhow::{bail, Result}; use log::error; use tokio_util::sync::CancellationToken; use tracing::info; @@ -152,9 +153,14 @@ pub fn get_unit_file_location(service: &UnitId) -> Result { args.insert(0, "--user"); } - match cmd("systemctl", args).read() { - Ok(output) => Ok(output.trim().to_string()), - Err(e) => anyhow::bail!("Failed to get unit file location: {}", e), + let output = Command::new("systemctl").args(&args).output()?; + + if output.status.success() { + let path = String::from_utf8(output.stdout)?; + Ok(path.trim().to_string()) + } else { + let stderr = String::from_utf8(output.stderr)?; + bail!(stderr); } } From 91d111966d0466c592ba2ae37b58839e48978988 Mon Sep 17 00:00:00 2001 From: Reilly Wood Date: Thu, 2 May 2024 18:03:28 -0700 Subject: [PATCH 2/2] v0.3.5 --- Cargo.lock | 2 +- Cargo.toml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 206bdb3..324143b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1785,7 +1785,7 @@ dependencies = [ [[package]] name = "systemctl-tui" -version = "0.3.4" +version = "0.3.5" dependencies = [ "anyhow", "better-panic", diff --git a/Cargo.toml b/Cargo.toml index 776e507..15299e9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,7 +3,7 @@ name = "systemctl-tui" description = "A simple TUI for interacting with systemd services and their logs" homepage = "https://github.com/rgwood/systemctl-tui" repository = "https://github.com/rgwood/systemctl-tui" -version = "0.3.4" +version = "0.3.5" edition = "2021" authors = ["Reilly Wood"] license = "MIT"