Refactor clear-session into session clear

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

@ -49,12 +49,16 @@ pub struct CommonOpt {
#[derive(Debug, StructOpt)]
pub enum Subcommand {
/// Clears the global session file
ClearSession,
/// Performs some task related to the current session
Session(SessionSubcommand),
#[structopt(visible_aliases = &["exec", "x"])]
Execute(ExecuteSubcommand),
/// Sends some operation to be performed on a remote machine
Send(SendSubcommand),
/// Launches the server-portion of the binary on a remote machine
Launch(LaunchSubcommand),
/// Begins listening for incoming requests
Listen(ListenSubcommand),
}
@ -62,8 +66,8 @@ impl Subcommand {
/// Runs the subcommand, returning the result
pub fn run(self, opt: CommonOpt) -> Result<(), Box<dyn std::error::Error>> {
match self {
Self::ClearSession => subcommand::clear_session::run()?,
Self::Execute(cmd) => subcommand::execute::run(cmd, opt)?,
Self::Session(cmd) => subcommand::session::run(cmd, opt)?,
Self::Send(cmd) => subcommand::send::run(cmd, opt)?,
Self::Launch(cmd) => subcommand::launch::run(cmd, opt)?,
Self::Listen(cmd) => subcommand::listen::run(cmd, opt)?,
}
@ -72,8 +76,15 @@ impl Subcommand {
}
}
/// Represents subcommand to operate on a session
#[derive(Debug, StructOpt)]
pub enum SessionSubcommand {
/// Clears the current session
Clear,
}
#[derive(Copy, Clone, Debug, Display, PartialEq, Eq, IsVariant)]
pub enum ExecuteFormat {
pub enum ResponseFormat {
/// Output responses in JSON format
#[display(fmt = "json")]
Json,
@ -89,19 +100,19 @@ pub enum ExecuteFormat {
}
#[derive(Clone, Debug, Display, From, Error, PartialEq, Eq)]
pub enum ExecuteFormatParseError {
pub enum ResponseFormatParseError {
InvalidVariant(#[error(not(source))] String),
}
impl FromStr for ExecuteFormat {
type Err = ExecuteFormatParseError;
impl FromStr for ResponseFormat {
type Err = ResponseFormatParseError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s.trim() {
"json" => Ok(Self::Json),
"program" => Ok(Self::Program),
"shell" => Ok(Self::Shell),
x => Err(ExecuteFormatParseError::InvalidVariant(x.to_string())),
x => Err(ResponseFormatParseError::InvalidVariant(x.to_string())),
}
}
}
@ -109,7 +120,7 @@ impl FromStr for ExecuteFormat {
/// Represents subcommand to execute some operation remotely
#[derive(Debug, StructOpt)]
#[structopt(verbatim_doc_comment)]
pub struct ExecuteSubcommand {
pub struct SendSubcommand {
/// Represents the format that results should be returned
///
/// Currently, there are two possible formats:
@ -123,7 +134,7 @@ pub struct ExecuteSubcommand {
default_value = "shell",
possible_values = &["json", "program", "shell"]
)]
pub format: ExecuteFormat,
pub format: ResponseFormat,
#[structopt(subcommand)]
pub operation: RequestPayload,

@ -1,7 +0,0 @@
use crate::utils::Session;
use tokio::io;
pub fn run() -> Result<(), io::Error> {
let rt = tokio::runtime::Runtime::new()?;
rt.block_on(async { Session::clear().await })
}

@ -1,4 +1,4 @@
pub mod clear_session;
pub mod execute;
pub mod launch;
pub mod listen;
pub mod send;
pub mod session;

@ -1,7 +1,7 @@
use crate::{
data::{Request, RequestPayload, Response, ResponsePayload},
net::{Client, TransportError},
opt::{CommonOpt, ExecuteFormat, ExecuteSubcommand},
opt::{CommonOpt, ResponseFormat, SendSubcommand},
utils::{Session, SessionError},
};
use derive_more::{Display, Error, From};
@ -15,13 +15,13 @@ pub enum Error {
TransportError(TransportError),
}
pub fn run(cmd: ExecuteSubcommand, opt: CommonOpt) -> Result<(), Error> {
pub fn run(cmd: SendSubcommand, opt: CommonOpt) -> Result<(), Error> {
let rt = tokio::runtime::Runtime::new()?;
rt.block_on(async { run_async(cmd, opt).await })
}
async fn run_async(cmd: ExecuteSubcommand, _opt: CommonOpt) -> Result<(), Error> {
async fn run_async(cmd: SendSubcommand, _opt: CommonOpt) -> Result<(), Error> {
let session = Session::load().await?;
let client = Client::connect(session).await?;
@ -55,7 +55,7 @@ async fn run_async(cmd: ExecuteSubcommand, _opt: CommonOpt) -> Result<(), Error>
Ok(())
}
fn print_response(fmt: ExecuteFormat, 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
// to print out the results
let is_fmt_program = fmt.is_program();
@ -79,12 +79,12 @@ fn print_response(fmt: ExecuteFormat, res: Response) -> io::Result<()> {
Ok(())
}
fn format_response(fmt: ExecuteFormat, res: Response) -> io::Result<String> {
fn format_response(fmt: ResponseFormat, res: Response) -> io::Result<String> {
Ok(match fmt {
ExecuteFormat::Json => serde_json::to_string(&res)
ResponseFormat::Json => serde_json::to_string(&res)
.map_err(|x| io::Error::new(io::ErrorKind::InvalidData, x))?,
ExecuteFormat::Program => format_program(res),
ExecuteFormat::Shell => format_human(res),
ResponseFormat::Program => format_program(res),
ResponseFormat::Shell => format_human(res),
})
}

@ -0,0 +1,14 @@
use crate::{
opt::{CommonOpt, SessionSubcommand},
utils::Session,
};
use tokio::io;
pub fn run(cmd: SessionSubcommand, _opt: CommonOpt) -> Result<(), io::Error> {
let rt = tokio::runtime::Runtime::new()?;
rt.block_on(async {
match cmd {
SessionSubcommand::Clear => Session::clear().await,
}
})
}
Loading…
Cancel
Save