Skip to content

Commit

Permalink
Fix RUST_LOG parsing and add --silent flag for application too
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesmcm committed Dec 30, 2024
1 parent fcf164f commit 54879a3
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 12 deletions.
4 changes: 4 additions & 0 deletions src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ pub struct App {
#[clap(short = 'v', long = "verbose")]
pub verbose: bool,

/// Suppress all output including application output. Note RUST_LOG=off can be used to suppress only vopono log/error output.
#[clap(long = "silent")]
pub silent: bool,

/// read sudo password from program specified in SUDO_ASKPASS environment variable
#[clap(short = 'A', long = "askpass")]
pub askpass: bool,
Expand Down
2 changes: 0 additions & 2 deletions src/args_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,8 @@ impl ArgsConfig {
let hosts = command_else_config_option!(hosts, command, config);
let open_ports = command_else_config_option!(open_ports, command, config);
let forward = command_else_config_option!(forward, command, config);
dbg!(&command.postup); // TODO
let postup = command_else_config_option!(postup, command, config)
.and_then(|p| shellexpand::full(&p).ok().map(|s| s.into_owned()));
dbg!(&postup);
let predown = command_else_config_option!(predown, command, config)
.and_then(|p| shellexpand::full(&p).ok().map(|s| s.into_owned()));
let group = command_else_config_option!(group, command, config);
Expand Down
11 changes: 9 additions & 2 deletions src/exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,12 @@ use vopono_core::network::sysctl::SysCtl;
use vopono_core::util::{get_config_from_alias, get_existing_namespaces, get_target_subnet};
use vopono_core::util::{parse_command_str, vopono_dir};

pub fn exec(command: ExecCommand, uiclient: &dyn UiClient, verbose: bool) -> anyhow::Result<()> {
pub fn exec(
command: ExecCommand,
uiclient: &dyn UiClient,
verbose: bool,
silent: bool,
) -> anyhow::Result<()> {
// this captures all sigint signals
// ignore for now, they are automatically passed on to the child
let signals = Signals::new([SIGINT])?;
Expand Down Expand Up @@ -234,7 +239,7 @@ pub fn exec(command: ExecCommand, uiclient: &dyn UiClient, verbose: bool) -> any
}

if !parsed_command.create_netns_only {
run_application(&parsed_command, forwarder, &ns, signals)?;
run_application(&parsed_command, forwarder, &ns, signals, silent)?;
} else {
info!(
"Created netns {} - will leave network namespace alive until ctrl+C received",
Expand Down Expand Up @@ -589,6 +594,7 @@ fn run_application(
forwarder: Option<Box<dyn Forwarder>>,
ns: &NetworkNamespace,
signals: SignalsInfo,
silent: bool,
) -> anyhow::Result<()> {
let application = ApplicationWrapper::new(
ns,
Expand All @@ -597,6 +603,7 @@ fn run_application(
parsed_command.group.clone(),
parsed_command.working_directory.clone().map(PathBuf::from),
forwarder,
silent,
)?;

let pid = application.handle.id();
Expand Down
19 changes: 12 additions & 7 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,16 @@ fn main() -> anyhow::Result<()> {
let app = args::App::parse();
// Set up logging
let mut builder = pretty_env_logger::formatted_timed_builder();
let log_level = if app.verbose {
LevelFilter::Debug
} else {
LevelFilter::Info
};
builder.filter_level(log_level);
builder.parse_default_env();
if app.verbose {
builder.filter_level(LevelFilter::Debug);
}
if app.silent {
if app.verbose {
warn!("Verbose and silent flags are mutually exclusive, ignoring verbose flag");
}
builder.filter_level(LevelFilter::Off);
}
builder.init();

let uiclient = CliClient {};
Expand All @@ -51,9 +55,10 @@ fn main() -> anyhow::Result<()> {
} else {
debug!("pactl not found, will not set PULSE_SERVER");
}
let verbose = app.verbose && !app.silent;
elevate_privileges(app.askpass)?;
clean_dead_namespaces()?;
exec::exec(cmd, &uiclient, app.verbose)?
exec::exec(cmd, &uiclient, verbose, app.silent)?
}
args::Command::List(listcmd) => {
clean_dead_locks()?;
Expand Down
3 changes: 2 additions & 1 deletion vopono_core/src/network/application_wrapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ impl ApplicationWrapper {
group: Option<String>,
working_directory: Option<PathBuf>,
port_forwarding: Option<Box<dyn Forwarder>>,
silent: bool,
) -> anyhow::Result<Self> {
let running_processes = get_all_running_process_names();
let app_vec = parse_command_str(application)?;
Expand Down Expand Up @@ -46,7 +47,7 @@ impl ApplicationWrapper {
app_vec_ptrs.as_slice(),
user,
group,
false,
silent,
false,
false,
working_directory,
Expand Down

0 comments on commit 54879a3

Please sign in to comment.