Deleted ScpProtocol; use FileTransferProtocol

This commit is contained in:
ChristianVisintin 2020-11-24 22:33:35 +01:00
parent 067f5e710a
commit 39bd881468
5 changed files with 59 additions and 43 deletions

View file

@ -26,9 +26,10 @@
use std::path::PathBuf; use std::path::PathBuf;
// Deps // Deps
use crate::filetransfer::FileTransferProtocol;
use crate::host::Localhost; use crate::host::Localhost;
use crate::ui::activities::{ use crate::ui::activities::{
auth_activity::AuthActivity, auth_activity::ScpProtocol, auth_activity::AuthActivity,
filetransfer_activity::FileTransferActivity, filetransfer_activity::FileTransferParams, filetransfer_activity::FileTransferActivity, filetransfer_activity::FileTransferParams,
Activity, Activity,
}; };
@ -83,7 +84,7 @@ impl ActivityManager {
&mut self, &mut self,
address: String, address: String,
port: u16, port: u16,
protocol: ScpProtocol, protocol: FileTransferProtocol,
username: Option<String>, username: Option<String>,
password: Option<String>, password: Option<String>,
) { ) {

View file

@ -38,10 +38,10 @@ type ProgressCallback = fn(bytes_written: usize, size: usize);
/// ///
/// This enum defines the different transfer protocol available in TermSCP /// This enum defines the different transfer protocol available in TermSCP
#[derive(PartialEq, Clone)] #[derive(std::cmp::PartialEq, std::fmt::Debug, std::clone::Clone)]
pub enum FileTransferProtocol { pub enum FileTransferProtocol {
Sftp, Sftp,
Ftps, Ftp,
} }
/// ## FileTransferError /// ## FileTransferError
@ -62,6 +62,29 @@ pub enum FileTransferError {
UnknownError, UnknownError,
} }
impl FileTransferError {
/// ### msg
///
/// Get error message
pub fn msg(&self) -> String {
match self {
FileTransferError::AuthenticationFailed => String::from("Authentication failed: bad credentials"),
FileTransferError::BadAddress => String::from("Bad address syntax"),
FileTransferError::ConnectionError => String::from("Connection error"),
FileTransferError::DirStatFailed => String::from("Could not stat directory"),
FileTransferError::FileCreateDenied => String::from("Failed to create file"),
FileTransferError::FileReadonly => String::from("File is readonly"),
FileTransferError::IoErr(err) => format!("IO Error: {}", err),
FileTransferError::NoSuchFileOrDirectory => String::from("No such file or directory"),
FileTransferError::ProtocolError => String::from("Protocol error"),
FileTransferError::UninitializedSession => String::from("Uninitialized session"),
FileTransferError::UnknownError => String::from("Unknown error"),
}
}
}
/// ## FileTransfer /// ## FileTransfer
/// ///
/// File transfer trait must be implemented by all the file transfers and defines the method used by a generic file transfer /// File transfer trait must be implemented by all the file transfers and defines the method used by a generic file transfer

View file

@ -42,7 +42,7 @@ mod utils;
// namespaces // namespaces
use activity_manager::{ActivityManager, NextActivity}; use activity_manager::{ActivityManager, NextActivity};
use ui::activities::auth_activity::ScpProtocol; use filetransfer::FileTransferProtocol;
/// ### print_usage /// ### print_usage
/// ///
@ -60,7 +60,7 @@ fn main() {
let mut port: u16 = 22; // Default port let mut port: u16 = 22; // Default port
let mut username: Option<String> = None; // Default username let mut username: Option<String> = None; // Default username
let password: Option<String>; // Default password let password: Option<String>; // Default password
let mut protocol: ScpProtocol = ScpProtocol::Sftp; // Default protocol let mut protocol: FileTransferProtocol = FileTransferProtocol::Sftp; // Default protocol
let mut ticks: Duration = Duration::from_micros(50); let mut ticks: Duration = Duration::from_micros(50);
//Process options //Process options
// FIXME: there is no way to provide password from CLI atm // FIXME: there is no way to provide password from CLI atm

View file

@ -30,6 +30,7 @@ extern crate unicode_width;
// locals // locals
use super::{Activity, Context}; use super::{Activity, Context};
use crate::filetransfer::FileTransferProtocol;
// Includes // Includes
use crossterm::event::Event as InputEvent; use crossterm::event::Event as InputEvent;
@ -65,22 +66,13 @@ enum InputMode {
Popup, Popup,
} }
/// ### ScpProtocol
///
/// ScpProtocol describes the communication protocol selected by the user to communicate with the remote
#[derive(std::cmp::PartialEq, std::fmt::Debug, std::clone::Clone)]
pub enum ScpProtocol {
Sftp,
Ftp,
}
/// ### AuthActivity /// ### AuthActivity
/// ///
/// AuthActivity is the data holder for the authentication activity /// AuthActivity is the data holder for the authentication activity
pub struct AuthActivity { pub struct AuthActivity {
pub address: String, pub address: String,
pub port: String, pub port: String,
pub protocol: ScpProtocol, pub protocol: FileTransferProtocol,
pub username: String, pub username: String,
pub password: String, pub password: String,
pub submit: bool, // becomes true after user has submitted fields pub submit: bool, // becomes true after user has submitted fields
@ -99,7 +91,7 @@ impl AuthActivity {
AuthActivity { AuthActivity {
address: String::new(), address: String::new(),
port: String::from("22"), port: String::from("22"),
protocol: ScpProtocol::Sftp, protocol: FileTransferProtocol::Sftp,
username: String::new(), username: String::new(),
password: String::new(), password: String::new(),
submit: false, submit: false,
@ -233,8 +225,8 @@ impl AuthActivity {
// If current field is Protocol handle event... (move element left) // If current field is Protocol handle event... (move element left)
if self.selected_field == InputField::Protocol { if self.selected_field == InputField::Protocol {
self.protocol = match self.protocol { self.protocol = match self.protocol {
ScpProtocol::Sftp => ScpProtocol::Sftp, FileTransferProtocol::Sftp => FileTransferProtocol::Sftp,
ScpProtocol::Ftp => ScpProtocol::Sftp, // End of list FileTransferProtocol::Ftp => FileTransferProtocol::Sftp, // End of list
} }
} }
} }
@ -242,8 +234,8 @@ impl AuthActivity {
// If current field is Protocol handle event... ( move element right ) // If current field is Protocol handle event... ( move element right )
if self.selected_field == InputField::Protocol { if self.selected_field == InputField::Protocol {
self.protocol = match self.protocol { self.protocol = match self.protocol {
ScpProtocol::Sftp => ScpProtocol::Ftp, FileTransferProtocol::Sftp => FileTransferProtocol::Ftp,
ScpProtocol::Ftp => ScpProtocol::Ftp, // End of list FileTransferProtocol::Ftp => FileTransferProtocol::Ftp, // End of list
} }
} }
} }
@ -306,8 +298,8 @@ impl AuthActivity {
fn draw_protocol_select(&self) -> Tabs { fn draw_protocol_select(&self) -> Tabs {
let protocols: Vec<Spans> = vec![Spans::from("SFTP"), Spans::from("FTP")]; let protocols: Vec<Spans> = vec![Spans::from("SFTP"), Spans::from("FTP")];
let index: usize = match self.protocol { let index: usize = match self.protocol {
ScpProtocol::Sftp => 0, FileTransferProtocol::Sftp => 0,
ScpProtocol::Ftp => 1, FileTransferProtocol::Ftp => 1,
}; };
Tabs::new(protocols) Tabs::new(protocols)
.block(Block::default().borders(Borders::ALL).title("Protocol")) .block(Block::default().borders(Borders::ALL).title("Protocol"))

View file

@ -26,7 +26,7 @@
// Dependencies // Dependencies
extern crate whoami; extern crate whoami;
use crate::ui::activities::auth_activity::ScpProtocol; use crate::filetransfer::FileTransferProtocol;
/// ### parse_remote_opt /// ### parse_remote_opt
/// ///
@ -49,11 +49,11 @@ use crate::ui::activities::auth_activity::ScpProtocol;
/// ///
pub fn parse_remote_opt( pub fn parse_remote_opt(
remote: &String, remote: &String,
) -> Result<(String, u16, ScpProtocol, Option<String>), String> { ) -> Result<(String, u16, FileTransferProtocol, Option<String>), String> {
let mut wrkstr: String = remote.clone(); let mut wrkstr: String = remote.clone();
let address: String; let address: String;
let mut port: u16 = 22; let mut port: u16 = 22;
let mut protocol: ScpProtocol = ScpProtocol::Sftp; let mut protocol: FileTransferProtocol = FileTransferProtocol::Sftp;
let mut username: Option<String> = None; let mut username: Option<String> = None;
// Split string by '://' // Split string by '://'
let tokens: Vec<&str> = wrkstr.split("://").collect(); let tokens: Vec<&str> = wrkstr.split("://").collect();
@ -65,13 +65,13 @@ pub fn parse_remote_opt(
match tokens[0] { match tokens[0] {
"sftp" => { "sftp" => {
// Set protocol to sftp // Set protocol to sftp
protocol = ScpProtocol::Sftp; protocol = FileTransferProtocol::Sftp;
// Set port to default (22) // Set port to default (22)
port = 22; port = 22;
} }
"ftp" | "ftps" => { "ftp" | "ftps" => {
// Set protocol to fpt // Set protocol to fpt
protocol = ScpProtocol::Ftp; protocol = FileTransferProtocol::Ftp;
// Set port to default (21) // Set port to default (21)
port = 21; port = 21;
} }
@ -82,7 +82,7 @@ pub fn parse_remote_opt(
_ => return Err(String::from("Bad syntax")), // Too many tokens... _ => return Err(String::from("Bad syntax")), // Too many tokens...
} }
// Set username to default if sftp // Set username to default if sftp
if protocol == ScpProtocol::Sftp { if protocol == FileTransferProtocol::Sftp {
// Set username to current username // Set username to current username
username = Some(whoami::username()); username = Some(whoami::username());
} }
@ -127,46 +127,46 @@ mod tests {
#[test] #[test]
fn test_utils_parse_remote_opt() { fn test_utils_parse_remote_opt() {
// Base case // Base case
let result: (String, u16, ScpProtocol, Option<String>) = parse_remote_opt(&String::from("172.26.104.1")).ok().unwrap(); let result: (String, u16, FileTransferProtocol, Option<String>) = parse_remote_opt(&String::from("172.26.104.1")).ok().unwrap();
assert_eq!(result.0, String::from("172.26.104.1")); assert_eq!(result.0, String::from("172.26.104.1"));
assert_eq!(result.1, 22); assert_eq!(result.1, 22);
assert_eq!(result.2, ScpProtocol::Sftp); assert_eq!(result.2, FileTransferProtocol::Sftp);
assert!(result.3.is_some()); assert!(result.3.is_some());
// User case // User case
let result: (String, u16, ScpProtocol, Option<String>) = parse_remote_opt(&String::from("root@172.26.104.1")).ok().unwrap(); let result: (String, u16, FileTransferProtocol, Option<String>) = parse_remote_opt(&String::from("root@172.26.104.1")).ok().unwrap();
assert_eq!(result.0, String::from("172.26.104.1")); assert_eq!(result.0, String::from("172.26.104.1"));
assert_eq!(result.1, 22); assert_eq!(result.1, 22);
assert_eq!(result.2, ScpProtocol::Sftp); assert_eq!(result.2, FileTransferProtocol::Sftp);
assert_eq!(result.3.unwrap(), String::from("root")); assert_eq!(result.3.unwrap(), String::from("root"));
// User + port // User + port
let result: (String, u16, ScpProtocol, Option<String>) = parse_remote_opt(&String::from("root@172.26.104.1:8022")).ok().unwrap(); let result: (String, u16, FileTransferProtocol, Option<String>) = parse_remote_opt(&String::from("root@172.26.104.1:8022")).ok().unwrap();
assert_eq!(result.0, String::from("172.26.104.1")); assert_eq!(result.0, String::from("172.26.104.1"));
assert_eq!(result.1, 8022); assert_eq!(result.1, 8022);
assert_eq!(result.2, ScpProtocol::Sftp); assert_eq!(result.2, FileTransferProtocol::Sftp);
assert_eq!(result.3.unwrap(), String::from("root")); assert_eq!(result.3.unwrap(), String::from("root"));
// Port only // Port only
let result: (String, u16, ScpProtocol, Option<String>) = parse_remote_opt(&String::from("172.26.104.1:4022")).ok().unwrap(); let result: (String, u16, FileTransferProtocol, Option<String>) = parse_remote_opt(&String::from("172.26.104.1:4022")).ok().unwrap();
assert_eq!(result.0, String::from("172.26.104.1")); assert_eq!(result.0, String::from("172.26.104.1"));
assert_eq!(result.1, 4022); assert_eq!(result.1, 4022);
assert_eq!(result.2, ScpProtocol::Sftp); assert_eq!(result.2, FileTransferProtocol::Sftp);
assert!(result.3.is_some()); assert!(result.3.is_some());
// Protocol // Protocol
let result: (String, u16, ScpProtocol, Option<String>) = parse_remote_opt(&String::from("ftp://172.26.104.1")).ok().unwrap(); let result: (String, u16, FileTransferProtocol, Option<String>) = parse_remote_opt(&String::from("ftp://172.26.104.1")).ok().unwrap();
assert_eq!(result.0, String::from("172.26.104.1")); assert_eq!(result.0, String::from("172.26.104.1"));
assert_eq!(result.1, 21); // Fallback to ftp default assert_eq!(result.1, 21); // Fallback to ftp default
assert_eq!(result.2, ScpProtocol::Ftp); assert_eq!(result.2, FileTransferProtocol::Ftp);
assert!(result.3.is_none()); // Doesn't fall back assert!(result.3.is_none()); // Doesn't fall back
// Protocol + user // Protocol + user
let result: (String, u16, ScpProtocol, Option<String>) = parse_remote_opt(&String::from("ftp://anon@172.26.104.1")).ok().unwrap(); let result: (String, u16, FileTransferProtocol, Option<String>) = parse_remote_opt(&String::from("ftp://anon@172.26.104.1")).ok().unwrap();
assert_eq!(result.0, String::from("172.26.104.1")); assert_eq!(result.0, String::from("172.26.104.1"));
assert_eq!(result.1, 21); // Fallback to ftp default assert_eq!(result.1, 21); // Fallback to ftp default
assert_eq!(result.2, ScpProtocol::Ftp); assert_eq!(result.2, FileTransferProtocol::Ftp);
assert_eq!(result.3.unwrap(), String::from("anon")); assert_eq!(result.3.unwrap(), String::from("anon"));
// All together now // All together now
let result: (String, u16, ScpProtocol, Option<String>) = parse_remote_opt(&String::from("ftp://anon@172.26.104.1:8021")).ok().unwrap(); let result: (String, u16, FileTransferProtocol, Option<String>) = parse_remote_opt(&String::from("ftp://anon@172.26.104.1:8021")).ok().unwrap();
assert_eq!(result.0, String::from("172.26.104.1")); assert_eq!(result.0, String::from("172.26.104.1"));
assert_eq!(result.1, 8021); // Fallback to ftp default assert_eq!(result.1, 8021); // Fallback to ftp default
assert_eq!(result.2, ScpProtocol::Ftp); assert_eq!(result.2, FileTransferProtocol::Ftp);
assert_eq!(result.3.unwrap(), String::from("anon")); assert_eq!(result.3.unwrap(), String::from("anon"));
// bad syntax // bad syntax