diff --git a/src/opt.rs b/src/opt.rs index c42b8e0..432b0bd 100644 --- a/src/opt.rs +++ b/src/opt.rs @@ -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> { 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 { 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, diff --git a/src/subcommand/clear_session.rs b/src/subcommand/clear_session.rs deleted file mode 100644 index 81eb4f3..0000000 --- a/src/subcommand/clear_session.rs +++ /dev/null @@ -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 }) -} diff --git a/src/subcommand/mod.rs b/src/subcommand/mod.rs index 2a86c25..ed8cded 100644 --- a/src/subcommand/mod.rs +++ b/src/subcommand/mod.rs @@ -1,4 +1,4 @@ -pub mod clear_session; -pub mod execute; pub mod launch; pub mod listen; +pub mod send; +pub mod session; diff --git a/src/subcommand/execute.rs b/src/subcommand/send.rs similarity index 89% rename from src/subcommand/execute.rs rename to src/subcommand/send.rs index ae95e83..a3726ce 100644 --- a/src/subcommand/execute.rs +++ b/src/subcommand/send.rs @@ -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 { +fn format_response(fmt: ResponseFormat, res: Response) -> io::Result { 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), }) } diff --git a/src/subcommand/session.rs b/src/subcommand/session.rs new file mode 100644 index 0000000..5a40733 --- /dev/null +++ b/src/subcommand/session.rs @@ -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, + } + }) +}