Use default protocol also in opt parser

This commit is contained in:
ChristianVisintin
2020-12-25 19:10:28 +01:00
parent 90f28d9f27
commit 16a011e81e

View File

@@ -27,10 +27,15 @@
extern crate chrono; extern crate chrono;
extern crate whoami; extern crate whoami;
// Locals
use crate::filetransfer::FileTransferProtocol; use crate::filetransfer::FileTransferProtocol;
use crate::system::config_client::ConfigClient;
use crate::system::environment;
// Ext
use chrono::format::ParseError; use chrono::format::ParseError;
use chrono::prelude::*; use chrono::prelude::*;
use std::str::FromStr;
use std::time::{Duration, SystemTime}; use std::time::{Duration, SystemTime};
/// ### parse_remote_opt /// ### parse_remote_opt
@@ -58,8 +63,22 @@ pub fn parse_remote_opt(
let mut wrkstr: String = remote.to_string(); let mut wrkstr: String = remote.to_string();
let address: String; let address: String;
let mut port: u16 = 22; let mut port: u16 = 22;
let mut protocol: FileTransferProtocol = FileTransferProtocol::Sftp;
let mut username: Option<String> = None; let mut username: Option<String> = 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 '://' // Split string by '://'
let tokens: Vec<&str> = wrkstr.split("://").collect(); let tokens: Vec<&str> = wrkstr.split("://").collect();
// If length is > 1, then token[0] is protocol // If length is > 1, then token[0] is protocol
@@ -67,33 +86,16 @@ pub fn parse_remote_opt(
1 => {} 1 => {}
2 => { 2 => {
// Parse protocol // Parse protocol
match tokens[0] { let (m_protocol, m_port) = match FileTransferProtocol::from_str(tokens[0]) {
"sftp" => { Ok(proto) => match proto {
// Set protocol to sftp FileTransferProtocol::Ftp(_) => (proto, 21),
protocol = FileTransferProtocol::Sftp; FileTransferProtocol::Scp => (proto, 22),
// Set port to default (22) FileTransferProtocol::Sftp => (proto, 22),
port = 22; },
} Err(_) => return Err(format!("Unknown protocol '{}'", tokens[0])),
"scp" => { };
// Set protocol to scp protocol = m_protocol;
protocol = FileTransferProtocol::Scp; port = m_port;
// 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])),
}
wrkstr = String::from(tokens[1]); // Wrkstr becomes tokens[1] wrkstr = String::from(tokens[1]); // Wrkstr becomes tokens[1]
} }
_ => return Err(String::from("Bad syntax")), // Too many tokens... _ => return Err(String::from("Bad syntax")), // Too many tokens...