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 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<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 '://'
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...