From 16a011e81e45ac800407b96bb523e2190ca5e100 Mon Sep 17 00:00:00 2001 From: ChristianVisintin Date: Fri, 25 Dec 2020 19:10:28 +0100 Subject: [PATCH] Use default protocol also in opt parser --- src/utils/parser.rs | 58 +++++++++++++++++++++++---------------------- 1 file changed, 30 insertions(+), 28 deletions(-) diff --git a/src/utils/parser.rs b/src/utils/parser.rs index 1410e5c..72b447f 100644 --- a/src/utils/parser.rs +++ b/src/utils/parser.rs @@ -27,10 +27,15 @@ extern crate chrono; extern crate whoami; +// Locals use crate::filetransfer::FileTransferProtocol; +use crate::system::config_client::ConfigClient; +use crate::system::environment; +// Ext use chrono::format::ParseError; use chrono::prelude::*; +use std::str::FromStr; use std::time::{Duration, SystemTime}; /// ### parse_remote_opt @@ -58,8 +63,22 @@ pub fn parse_remote_opt( let mut wrkstr: String = remote.to_string(); let address: String; let mut port: u16 = 22; - let mut protocol: FileTransferProtocol = FileTransferProtocol::Sftp; let mut username: Option = None; + // Set protocol to default protocol + let mut protocol: FileTransferProtocol = match environment::init_config_dir() { + Ok(p) => match p { + Some(p) => { + // Create config client + let (config_path, ssh_key_path) = environment::get_config_paths(p.as_path()); + match ConfigClient::new(config_path.as_path(), ssh_key_path.as_path()) { + Ok(cli) => cli.get_default_protocol(), + Err(_) => FileTransferProtocol::Sftp, + } + } + None => FileTransferProtocol::Sftp, + }, + Err(_) => FileTransferProtocol::Sftp, + }; // Split string by '://' let tokens: Vec<&str> = wrkstr.split("://").collect(); // If length is > 1, then token[0] is protocol @@ -67,33 +86,16 @@ pub fn parse_remote_opt( 1 => {} 2 => { // Parse protocol - match tokens[0] { - "sftp" => { - // Set protocol to sftp - protocol = FileTransferProtocol::Sftp; - // Set port to default (22) - port = 22; - } - "scp" => { - // Set protocol to scp - protocol = FileTransferProtocol::Scp; - // Set port to default (22) - port = 22; - } - "ftp" => { - // Set protocol to fpt - protocol = FileTransferProtocol::Ftp(false); - // Set port to default (21) - port = 21; - } - "ftps" => { - // Set protocol to fpt - protocol = FileTransferProtocol::Ftp(true); - // Set port to default (21) - port = 21; - } - _ => return Err(format!("Unknown protocol '{}'", tokens[0])), - } + let (m_protocol, m_port) = match FileTransferProtocol::from_str(tokens[0]) { + Ok(proto) => match proto { + FileTransferProtocol::Ftp(_) => (proto, 21), + FileTransferProtocol::Scp => (proto, 22), + FileTransferProtocol::Sftp => (proto, 22), + }, + Err(_) => return Err(format!("Unknown protocol '{}'", tokens[0])), + }; + protocol = m_protocol; + port = m_port; wrkstr = String::from(tokens[1]); // Wrkstr becomes tokens[1] } _ => return Err(String::from("Bad syntax")), // Too many tokens...