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)] #[derive(Copy, Clone, Debug, Display, PartialEq, Eq, IsVariant)]
pub enum ResponseFormat { pub enum ResponseFormat {
/// Output responses in human-readable format
#[display(fmt = "human")]
Human,
/// Output responses in JSON format /// Output responses in JSON format
#[display(fmt = "json")] #[display(fmt = "json")]
Json, Json,
/// Provides special formatting to stdout & stderr that only /// Provides special formatting to stdout & stderr that only
/// outputs that of the remotely-executed program /// outputs that of the remotely-executed program
#[display(fmt = "program")]
Program,
/// Output responses in human-readable format for shells
#[display(fmt = "shell")] #[display(fmt = "shell")]
Shell, Shell,
} }
@ -109,8 +109,8 @@ impl FromStr for ResponseFormat {
fn from_str(s: &str) -> Result<Self, Self::Err> { fn from_str(s: &str) -> Result<Self, Self::Err> {
match s.trim() { match s.trim() {
"human" => Ok(Self::Human),
"json" => Ok(Self::Json), "json" => Ok(Self::Json),
"program" => Ok(Self::Program),
"shell" => Ok(Self::Shell), "shell" => Ok(Self::Shell),
x => Err(ResponseFormatParseError::InvalidVariant(x.to_string())), x => Err(ResponseFormatParseError::InvalidVariant(x.to_string())),
} }
@ -130,9 +130,9 @@ pub struct SendSubcommand {
#[structopt( #[structopt(
short, short,
long, long,
value_name = "json|program|shell", value_name = "human|json|shell",
default_value = "shell", default_value = "human",
possible_values = &["json", "program", "shell"] possible_values = &["human", "json", "shell"]
)] )]
pub format: ResponseFormat, 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<()> { 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 // 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_stderr = res.payload.is_proc_stderr();
let is_type_stdout = res.payload.is_proc_stdout(); 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)?; let out = format_response(fmt, res)?;
// Print out our response if flagged to do so // Print out our response if flagged to do so
if do_print { if do_print {
// If we are program format and got stderr, write it to stderr without altering content // If we are shell format and got stderr, write it to stderr without altering content
if is_fmt_program && is_type_stderr { if is_fmt_shell && is_type_stderr {
eprint!("{}", out); eprint!("{}", out);
// Else, if we are program format and got stdout, write it to stdout without altering content // Else, if we are shell format and got stdout, write it to stdout without altering content
} else if is_fmt_program && is_type_stdout { } else if is_fmt_shell && is_type_stdout {
print!("{}", out); print!("{}", out);
// Otherwise, always go to stdout with traditional println // 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> { fn format_response(fmt: ResponseFormat, res: Response) -> io::Result<String> {
Ok(match fmt { Ok(match fmt {
ResponseFormat::Human => format_human(res),
ResponseFormat::Json => serde_json::to_string(&res) ResponseFormat::Json => serde_json::to_string(&res)
.map_err(|x| io::Error::new(io::ErrorKind::InvalidData, x))?, .map_err(|x| io::Error::new(io::ErrorKind::InvalidData, x))?,
ResponseFormat::Program => format_program(res), ResponseFormat::Shell => format_shell(res),
ResponseFormat::Shell => format_human(res),
}) })
} }
fn format_program(res: Response) -> String { fn format_shell(res: Response) -> String {
match res.payload { match res.payload {
ResponsePayload::ProcStdout { data, .. } => String::from_utf8_lossy(&data).to_string(), ResponsePayload::ProcStdout { data, .. } => String::from_utf8_lossy(&data).to_string(),
ResponsePayload::ProcStderr { data, .. } => String::from_utf8_lossy(&data).to_string(), ResponsePayload::ProcStderr { data, .. } => String::from_utf8_lossy(&data).to_string(),

Loading…
Cancel
Save