Skip to content

Commit

Permalink
Merge pull request #18 from rgwood/fix-error-being-printed
Browse files Browse the repository at this point in the history
Fix error being printed
  • Loading branch information
rgwood authored May 3, 2024
2 parents 31fbf7c + 91d1119 commit 2f80971
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 23 deletions.
3 changes: 1 addition & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 }
Expand Down
39 changes: 26 additions & 13 deletions src/components/home.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
use duct::cmd;
use futures::Future;
use indexmap::IndexMap;
use itertools::Itertools;
Expand All @@ -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::{
Expand Down Expand Up @@ -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
Expand All @@ -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),
}
Expand Down
2 changes: 1 addition & 1 deletion src/components/logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
16 changes: 11 additions & 5 deletions src/systemd.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -152,9 +153,14 @@ pub fn get_unit_file_location(service: &UnitId) -> Result<String> {
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);
}
}

Expand Down

0 comments on commit 2f80971

Please sign in to comment.