Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
458 changes: 193 additions & 265 deletions Cargo.lock

Large diffs are not rendered by default.

14 changes: 7 additions & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "vsv"
version = "2.0.0"
version = "2.0.1"
description = "Runit service manager CLI"
edition = "2021"
repository = "https://github.com/bahamas10/rust-vsv"
Expand All @@ -10,12 +10,12 @@ keywords = ["runit", "void", "void-linux"]
categories = ["command-line-utilities"]

[dependencies]
anyhow = "1.0.55"
clap = { version = "3.1.1", features = ["cargo", "derive"] }
dirs = "4.0.0"
libc = "0.2.119"
rayon = "1.5.1"
yansi = "0.5.0"
anyhow = "1.0.100"
clap = { version = "4.5.53", features = ["cargo", "derive"] }
dirs = "6.0.0"
libc = "0.2.178"
rayon = "1.11.0"
yansi = "1.0.1"

[dev-dependencies]
assert_cmd = "2.0.4"
8 changes: 4 additions & 4 deletions src/arguments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

use std::path;

use clap::{Parser, Subcommand};
use clap::{ArgAction, Parser, Subcommand};

#[derive(Debug, Parser)]
#[clap(author, version, about, verbatim_doc_comment, long_about = None)]
Expand Down Expand Up @@ -37,7 +37,7 @@ pub struct Args {
pub color: Option<String>,

/// Directory to look into, defaults to env SVDIR or /var/service if unset.
#[clap(short, long, parse(from_os_str), value_name = "dir")]
#[clap(short, long, value_parser, value_name = "dir")]
pub dir: Option<path::PathBuf>,

/// Show log processes, this is a shortcut for `status -l`.
Expand All @@ -53,8 +53,8 @@ pub struct Args {
pub user: bool,

/// Increase Verbosity.
#[clap(short, long, parse(from_occurrences))]
pub verbose: usize,
#[clap(short, long, action = ArgAction::Count)]
pub verbose: u8,

/// Subcommand.
#[clap(subcommand)]
Expand Down
14 changes: 5 additions & 9 deletions src/commands/enable_disable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
//! `vsv enable` and `vsv disable`.

use anyhow::{ensure, Result};
use yansi::{Color, Style};
use yansi::Paint;

use crate::config;
use crate::config::Config;
Expand All @@ -32,14 +32,10 @@ fn _do_enable_disable(cfg: &Config) -> Result<()> {
for name in &cfg.operands {
let p = cfg.svdir.join(name);
let svc = RunitService::new(name, &p);
print!(
"{} service {}... ",
cfg.mode,
Style::default().bold().paint(name)
);
print!("{} service {}... ", cfg.mode, name.bold(),);

if !svc.valid() {
println!("{}", Color::Red.paint("failed! service not valid"));
println!("{}", "failed! service not valid".red());
had_error = true;
continue;
}
Expand All @@ -53,9 +49,9 @@ fn _do_enable_disable(cfg: &Config) -> Result<()> {
match ret {
Err(err) => {
had_error = true;
println!("{}", Color::Red.paint(format!("failed! {}", err)));
println!("{}", format!("failed! {}", err).red());
}
Ok(()) => println!("{}.", Color::Green.paint("done")),
Ok(()) => println!("{}.", "done".green()),
};
}

Expand Down
9 changes: 5 additions & 4 deletions src/commands/external.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use std::env;

use anyhow::{bail, ensure, Context, Result};
use clap::crate_name;
use yansi::Color;
use yansi::{Color, Paint};

use crate::utils;
use crate::{config, config::Config};
Expand All @@ -37,14 +37,15 @@ pub fn do_external(cfg: &Config) -> Result<()> {
println!(
"[{}] {}",
crate_name!(),
Color::Cyan.paint(format!(
format!(
"Running {} command ({}={:?} {} {})",
sv,
config::ENV_SVDIR,
&cfg.svdir,
sv,
&args_s
))
)
.cyan()
);

// run the actual program
Expand All @@ -62,7 +63,7 @@ pub fn do_external(cfg: &Config) -> Result<()> {
println!(
"[{}] {}",
crate_name!(),
color.paint(format!("[{} {}] exit code {}", sv, &args_s, code))
format!("[{} {}] exit code {}", sv, &args_s, code).paint(color)
);

match code {
Expand Down
6 changes: 3 additions & 3 deletions src/commands/status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

use anyhow::{Context, Result};
use rayon::prelude::*;
use yansi::Style;
use yansi::{Paint, Style};

use crate::config::Config;
use crate::runit;
Expand All @@ -18,7 +18,7 @@ use crate::{utils, utils::verbose};
/// Handle `vsv status` or `vsv` without a subcommand given.
pub fn do_status(cfg: &Config) -> Result<()> {
// may or may not be set (option)
let filter = cfg.operands.get(0);
let filter = cfg.operands.first();

// find all services
let services = runit::get_services(&cfg.svdir, cfg.log, filter)
Expand Down Expand Up @@ -64,7 +64,7 @@ pub fn do_status(cfg: &Config) -> Result<()> {
// print pstree if applicable
if cfg.tree {
let (tree_s, style) = service.format_pstree();
println!("{}", style.paint(tree_s));
println!("{}", tree_s.paint(style));
}

// print any verbose messages/warnings generated by the service
Expand Down
7 changes: 4 additions & 3 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@
use std::env;
use std::ffi::OsString;
use std::fmt;
use std::io;
use std::io::IsTerminal;
use std::path::PathBuf;

use anyhow::{bail, Context, Result};

use crate::arguments::{Args, Commands};
use crate::config;
use crate::utils;

// default values
pub const DEFAULT_SVDIR: &str = "/var/service";
Expand Down Expand Up @@ -80,7 +81,7 @@ pub struct Config {
// CLI options only
pub tree: bool,
pub log: bool,
pub verbose: usize,
pub verbose: u8,
pub operands: Vec<String>,
pub mode: ProgramMode,
}
Expand Down Expand Up @@ -190,7 +191,7 @@ fn should_colorize_output(color_arg: &Option<String>) -> Result<bool> {
}

// lastly check if stdout is a tty
let isatty = utils::isatty(1);
let isatty = io::stdout().is_terminal();

Ok(isatty)
}
Expand Down
8 changes: 4 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#![allow(clippy::uninlined_format_args)]

use anyhow::{Context, Result};
use yansi::{Color, Paint};
use yansi::Paint;

mod arguments;
mod commands;
Expand All @@ -29,7 +29,7 @@ use utils::verbose;

fn do_main() -> Result<()> {
// disable color until we absolutely know we want it
Paint::disable();
yansi::disable();

// parse CLI options + env vars
let args = arguments::parse();
Expand All @@ -38,7 +38,7 @@ fn do_main() -> Result<()> {

// toggle color if the user wants it or the env dictates
if cfg.colorize {
Paint::enable();
yansi::enable();
}

verbose!(
Expand All @@ -62,6 +62,6 @@ fn main() {
let ret = do_main();

if let Err(err) = ret {
die!(1, "{}: {:?}", Color::Red.paint("error"), err);
die!(1, "{}: {:?}", "error".red(), err);
}
}
4 changes: 2 additions & 2 deletions src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ impl Service {
let style = match t.as_secs() {
t if t < 5 => style.fg(Color::Red),
t if t < 30 => style.fg(Color::Yellow),
_ => style.dimmed(),
_ => style.dim(),
};

(s, style)
Expand All @@ -229,7 +229,7 @@ impl Service {
};

let (tree_s, style) = match tree {
Ok(stdout) => (stdout.trim().into(), style.dimmed()),
Ok(stdout) => (stdout.trim().into(), style.dim()),
Err(err) => {
(format!("pstree call failed: {}", err), style.fg(Color::Red))
}
Expand Down
34 changes: 5 additions & 29 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@

//! Contains various util functions for vsv.

use libc::{c_int, pid_t};
use libc::pid_t;
use std::fs;
use std::path::Path;
use std::process::{Command, ExitStatus};
use std::time::Duration;

use anyhow::{anyhow, Context, Result};
use yansi::Style;
use yansi::{Paint, Style};

/**
* A `println!()`-like macro that will only print if `-v` is set.
Expand All @@ -22,7 +22,7 @@ macro_rules! verbose {
($cfg:expr, $fmt:expr $(, $args:expr )* $(,)? ) => {
if $cfg.verbose > 0 {
let s = format!($fmt $(, $args)*);
eprintln!("> {}", ::yansi::Style::default().dimmed().paint(s));
eprintln!("> {}", s.dim());
}
};
}
Expand Down Expand Up @@ -73,10 +73,10 @@ pub fn format_status_line<T: AsRef<str>>(

for ((text, style), max, suffix) in data {
let column = if max == 0 {
format!(" {}", style.paint(text.as_ref()))
format!(" {}", text.as_ref().paint(style))
} else {
let text = trim_long_string(text.as_ref(), max, suffix);
format!(" {0:1$}", style.paint(text), max)
format!(" {0:1$}", text.paint(style), max)
};

line.push_str(&column);
Expand Down Expand Up @@ -279,23 +279,6 @@ pub fn trim_long_string(s: &str, limit: usize, suffix: &str) -> String {
)
}

/**
* Check if the given file descriptor (by number) is a tty.
*
* # Example
*
* Print "hello world" if stdout is a tty:
*
* ```
* if isatty(1) {
* println!("hello world");
* }
* ```
*/
pub fn isatty(fd: c_int) -> bool {
unsafe { libc::isatty(fd) != 0 }
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down Expand Up @@ -369,13 +352,6 @@ mod tests {
Ok(())
}

#[test]
fn test_isatty_bad_fd() {
let b = isatty(-1);

assert!(!b, "fd -1 is not a tty");
}

#[test]
fn test_relative_durations() {
use std::time::Duration;
Expand Down