Rename format type shell -> human and program -> shell

pull/38/head
Chip Senkbeil 3 years ago
parent 79fe86ae15
commit 6ef55d6e38
No known key found for this signature in database
GPG Key ID: 35EF1F8EC72A4131

@ -85,16 +85,16 @@ pub enum SessionSubcommand {
#[derive(Copy, Clone, Debug, Display, PartialEq, Eq, IsVariant)]
pub enum ResponseFormat {
/// Output responses in human-readable format
#[display(fmt = "human")]
Human,
/// Output responses in JSON format
#[display(fmt = "json")]
Json,
/// Provides special formatting to stdout & stderr that only
/// outputs that of the remotely-executed program
#[display(fmt = "program")]
Program,
/// Output responses in human-readable format for shells
#[display(fmt = "shell")]
Shell,
}
@ -109,8 +109,8 @@ impl FromStr for ResponseFormat {
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s.trim() {
"human" => Ok(Self::Human),
"json" => Ok(Self::Json),
"program" => Ok(Self::Program),
"shell" => Ok(Self::Shell),
x => Err(ResponseFormatParseError::InvalidVariant(x.to_string())),
}
@ -130,9 +130,9 @@ pub struct SendSubcommand {
#[structopt(
short,
long,
value_name = "json|program|shell",
default_value = "shell",
possible_values = &["json", "program", "shell"]
value_name = "human|json|shell",
default_value = "human",
possible_values = &["human", "json", "shell"]
)]
pub format: ResponseFormat,

@ -56,23 +56,23 @@ async fn run_async(cmd: SendSubcommand, _opt: CommonOpt) -> Result<(), Error> {
}
fn print_response(fmt: ResponseFormat, res: Response) -> io::Result<()> {
// If we are not program format or we are program format and got stdout/stderr, we want
// If we are not shell format or we are shell format and got stdout/stderr, we want
// to print out the results
let is_fmt_program = fmt.is_program();
let is_fmt_shell = fmt.is_shell();
let is_type_stderr = res.payload.is_proc_stderr();
let is_type_stdout = res.payload.is_proc_stdout();
let do_print = !is_fmt_program || is_type_stderr || is_type_stdout;
let do_print = !is_fmt_shell || is_type_stderr || is_type_stdout;
let out = format_response(fmt, res)?;
// Print out our response if flagged to do so
if do_print {
// If we are program format and got stderr, write it to stderr without altering content
if is_fmt_program && is_type_stderr {
// If we are shell format and got stderr, write it to stderr without altering content
if is_fmt_shell && is_type_stderr {
eprint!("{}", out);
// Else, if we are program format and got stdout, write it to stdout without altering content
} else if is_fmt_program && is_type_stdout {
// Else, if we are shell format and got stdout, write it to stdout without altering content
} else if is_fmt_shell && is_type_stdout {
print!("{}", out);
// Otherwise, always go to stdout with traditional println
@ -86,14 +86,14 @@ fn print_response(fmt: ResponseFormat, res: Response) -> io::Result<()> {
fn format_response(fmt: ResponseFormat, res: Response) -> io::Result<String> {
Ok(match fmt {
ResponseFormat::Human => format_human(res),
ResponseFormat::Json => serde_json::to_string(&res)
.map_err(|x| io::Error::new(io::ErrorKind::InvalidData, x))?,
ResponseFormat::Program => format_program(res),
ResponseFormat::Shell => format_human(res),
ResponseFormat::Shell => format_shell(res),
})
}
fn format_program(res: Response) -> String {
fn format_shell(res: Response) -> String {
match res.payload {
ResponsePayload::ProcStdout { data, .. } => String::from_utf8_lossy(&data).to_string(),
ResponsePayload::ProcStderr { data, .. } => String::from_utf8_lossy(&data).to_string(),

Loading…
Cancel
Save