Skip to content

Commit

Permalink
Merge branch 'main' into camille/update-format-option (#1403)
Browse files Browse the repository at this point in the history
Co-authored-by: Trevor Blades <[email protected]>
Co-authored-by: Sachin D. Shinde <[email protected]>
Co-authored-by: Jesse Wang <[email protected]>
Co-authored-by: Patrick Arminio <[email protected]>
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
  • Loading branch information
6 people authored and gocamille committed Jan 9, 2023
1 parent 894d81a commit bda3c89
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 10 deletions.
11 changes: 6 additions & 5 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,14 @@ pub struct Rover {
#[serde(serialize_with = "option_from_display")]
log_level: Option<Level>,

/// Specify Rover's output type
#[arg(long = "output", default_value = "plain", global = true)]
output_type: OutputType,

#[clap(long = "format", default_value = "plain", global = true)]
/// Specify Rover's format type
#[arg(long = "format", default_value = "plain", global = true)]
format_type: FormatType,

/// Specify a file to write Rover's output to
#[arg(long = "output", global = true)]
output_type: OutputType,

/// Accept invalid certificates when performing HTTPS requests.
///
/// You should think very carefully before using this flag.
Expand Down
93 changes: 93 additions & 0 deletions src/command/dev/command.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
use std::{
io::{BufRead, BufReader},
process::{Child, Command, Stdio},
};

use anyhow::{anyhow, Context};
use crossbeam_channel::Sender;

use crate::{command::dev::do_dev::log_err_and_continue, RoverError, RoverResult};

#[derive(Debug)]
pub struct BackgroundTask {
child: Child,
}

pub enum BackgroundTaskLog {
Stdout(String),
Stderr(String),
}

impl BackgroundTask {
pub fn new(command: String, log_sender: Sender<BackgroundTaskLog>) -> RoverResult<Self> {
let args: Vec<&str> = command.split(' ').collect();
let (bin, args) = match args.len() {
0 => Err(anyhow!("the command you passed is empty")),
1 => Ok((args[0], Vec::new())),
_ => Ok((args[0], Vec::from_iter(args[1..].iter()))),
}?;
tracing::info!("starting `{}`", &command);
if which::which(bin).is_err() {
return Err(anyhow!("{} is not installed on this machine", &bin).into());
}

let mut command = Command::new(bin);
command.args(args).env("APOLLO_ROVER", "true");

command.stdout(Stdio::piped()).stderr(Stdio::piped());

let mut child = command
.spawn()
.with_context(|| "could not spawn child process")?;

if let Some(stdout) = child.stdout.take() {
let log_sender = log_sender.clone();
rayon::spawn(move || {
let stdout = BufReader::new(stdout);
stdout.lines().for_each(|line| {
if let Ok(line) = line {
log_sender
.send(BackgroundTaskLog::Stdout(line))
.expect("could not update stdout logs for command");
}
});
});
}

if let Some(stderr) = child.stderr.take() {
rayon::spawn(move || {
let stderr = BufReader::new(stderr);
stderr.lines().for_each(|line| {
if let Ok(line) = line {
log_sender
.send(BackgroundTaskLog::Stderr(line))
.expect("could not update stderr logs for command");
}
});
});
}

Ok(Self { child })
}

pub fn kill(&mut self) {
let pid = self.id();
tracing::info!("killing child with pid {}", &pid);
let _ = self.child.kill().map_err(|_| {
log_err_and_continue(RoverError::new(anyhow!(
"could not kill child with pid {}",
&pid
)));
});
}

pub fn id(&self) -> u32 {
self.child.id()
}
}

impl Drop for BackgroundTask {
fn drop(&mut self) {
self.kill()
}
}
7 changes: 4 additions & 3 deletions src/options/output.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use std::str::FromStr;

use saucer::{clap, Parser, Utf8PathBuf};
use camino::Utf8PathBuf;
use clap::Parser;

use crate::{cli::FormatType};
use crate::cli::FormatType;

#[derive(Debug, Parser)]
pub struct Output {
Expand All @@ -18,7 +19,7 @@ pub enum OutputType {
}

impl FromStr for OutputType {
type Err = saucer::Error;
type Err = anyhow::Error;

fn from_str(s: &str) -> Result<Self, Self::Err> {
if let Ok(format_type) = FormatType::from_str(s) {
Expand Down
2 changes: 1 addition & 1 deletion xtask/src/tools/cargo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use camino::Utf8PathBuf;
use crate::commands::version::RoverVersion;
use crate::target::Target;
use crate::tools::{GitRunner, Runner};
use crate::utils::{CommandOutput, PKG_PROJECT_ROOT};
use crate::utils::{self, CommandOutput, PKG_PROJECT_ROOT};

use std::fs;

Expand Down
3 changes: 2 additions & 1 deletion xtask/src/tools/runner.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use anyhow::{anyhow, Context, Result};
use camino::Utf8PathBuf;
use shell_candy::{ShellTask, ShellTaskBehavior, ShellTaskLog, ShellTaskOutput};

use std::collections::HashMap;
use which::which;

use crate::{utils::CommandOutput, Result};

Expand Down

0 comments on commit bda3c89

Please sign in to comment.