Bookmark name as hostname for cli args (#111)

bookmark name as hostname for cli args
This commit is contained in:
Christian Visintin
2022-05-03 11:54:48 +02:00
committed by GitHub
parent f094979ddb
commit e0d8b80cdf
21 changed files with 629 additions and 292 deletions

View File

@@ -28,20 +28,15 @@
// Locals
use super::{AuthActivity, FileTransferParams};
use crate::filetransfer::params::{AwsS3Params, GenericProtocolParams, ProtocolParams};
use crate::system::bookmarks_client::BookmarksClient;
use crate::system::environment;
// Ext
use std::path::PathBuf;
impl AuthActivity {
/// Delete bookmark
pub(super) fn del_bookmark(&mut self, idx: usize) {
if let Some(bookmarks_cli) = self.bookmarks_client.as_mut() {
let name = self.bookmarks_list.get(idx).cloned();
if let Some(bookmarks_cli) = self.bookmarks_client_mut() {
// Iterate over kyes
let name: Option<&String> = self.bookmarks_list.get(idx);
if let Some(name) = name {
bookmarks_cli.del_bookmark(name);
bookmarks_cli.del_bookmark(&name);
// Write bookmarks
self.write_bookmarks();
}
@@ -52,7 +47,7 @@ impl AuthActivity {
/// Load selected bookmark (at index) to input fields
pub(super) fn load_bookmark(&mut self, idx: usize) {
if let Some(bookmarks_cli) = self.bookmarks_client.as_ref() {
if let Some(bookmarks_cli) = self.bookmarks_client() {
// Iterate over bookmarks
if let Some(key) = self.bookmarks_list.get(idx) {
if let Some(bookmark) = bookmarks_cli.get_bookmark(key) {
@@ -72,7 +67,7 @@ impl AuthActivity {
return;
}
};
if let Some(bookmarks_cli) = self.bookmarks_client.as_mut() {
if let Some(bookmarks_cli) = self.bookmarks_client_mut() {
bookmarks_cli.add_bookmark(name.clone(), params, save_password);
// Save bookmarks
self.write_bookmarks();
@@ -85,10 +80,10 @@ impl AuthActivity {
}
/// Delete recent
pub(super) fn del_recent(&mut self, idx: usize) {
if let Some(client) = self.bookmarks_client.as_mut() {
let name: Option<&String> = self.recents_list.get(idx);
let name = self.recents_list.get(idx).cloned();
if let Some(client) = self.bookmarks_client_mut() {
if let Some(name) = name {
client.del_recent(name);
client.del_recent(&name);
// Write bookmarks
self.write_bookmarks();
}
@@ -99,7 +94,7 @@ impl AuthActivity {
/// Load selected recent (at index) to input fields
pub(super) fn load_recent(&mut self, idx: usize) {
if let Some(client) = self.bookmarks_client.as_ref() {
if let Some(client) = self.bookmarks_client() {
// Iterate over bookmarks
if let Some(key) = self.recents_list.get(idx) {
if let Some(bookmark) = client.get_recent(key) {
@@ -119,7 +114,7 @@ impl AuthActivity {
return;
}
};
if let Some(bookmarks_cli) = self.bookmarks_client.as_mut() {
if let Some(bookmarks_cli) = self.bookmarks_client_mut() {
bookmarks_cli.add_recent(params);
// Save bookmarks
self.write_bookmarks();
@@ -128,7 +123,7 @@ impl AuthActivity {
/// Write bookmarks to file
fn write_bookmarks(&mut self) {
if let Some(bookmarks_cli) = self.bookmarks_client.as_ref() {
if let Some(bookmarks_cli) = self.bookmarks_client() {
if let Err(err) = bookmarks_cli.write_bookmarks() {
self.mount_error(format!("Could not write bookmarks: {}", err).as_str());
}
@@ -137,58 +132,22 @@ impl AuthActivity {
/// Initialize bookmarks client
pub(super) fn init_bookmarks_client(&mut self) {
// Get config dir
match environment::init_config_dir() {
Ok(path) => {
// If some configure client, otherwise do nothing; don't bother users telling them that bookmarks are not supported on their system.
if let Some(config_dir_path) = path {
let bookmarks_file: PathBuf =
environment::get_bookmarks_paths(config_dir_path.as_path());
// Initialize client
match BookmarksClient::new(
bookmarks_file.as_path(),
config_dir_path.as_path(),
16,
) {
Ok(cli) => {
// Load bookmarks into list
let mut bookmarks_list: Vec<String> =
Vec::with_capacity(cli.iter_bookmarks().count());
for bookmark in cli.iter_bookmarks() {
bookmarks_list.push(bookmark.clone());
}
// Load recents into list
let mut recents_list: Vec<String> =
Vec::with_capacity(cli.iter_recents().count());
for recent in cli.iter_recents() {
recents_list.push(recent.clone());
}
self.bookmarks_client = Some(cli);
self.bookmarks_list = bookmarks_list;
self.recents_list = recents_list;
// Sort bookmark list
self.sort_bookmarks();
self.sort_recents();
}
Err(err) => {
self.mount_error(
format!(
"Could not initialize bookmarks (at \"{}\", \"{}\"): {}",
bookmarks_file.display(),
config_dir_path.display(),
err
)
.as_str(),
);
}
}
}
if let Some(cli) = self.bookmarks_client_mut() {
// Load bookmarks into list
let mut bookmarks_list: Vec<String> = Vec::with_capacity(cli.iter_bookmarks().count());
for bookmark in cli.iter_bookmarks() {
bookmarks_list.push(bookmark.clone());
}
Err(err) => {
self.mount_error(
format!("Could not initialize configuration directory: {}", err).as_str(),
);
// Load recents into list
let mut recents_list: Vec<String> = Vec::with_capacity(cli.iter_recents().count());
for recent in cli.iter_recents() {
recents_list.push(recent.clone());
}
self.bookmarks_list = bookmarks_list;
self.recents_list = recents_list;
// Sort bookmark list
self.sort_bookmarks();
self.sort_recents();
}
}

View File

@@ -172,7 +172,6 @@ const STORE_KEY_RELEASE_NOTES: &str = "AUTH_RELEASE_NOTES";
/// AuthActivity is the data holder for the authentication activity
pub struct AuthActivity {
app: Application<Id, Msg, NoUserEvent>,
bookmarks_client: Option<BookmarksClient>,
/// List of bookmarks
bookmarks_list: Vec<String>,
/// List of recent hosts
@@ -196,7 +195,6 @@ impl AuthActivity {
.poll_timeout(ticks),
),
context: None,
bookmarks_client: None,
bookmarks_list: Vec::new(),
exit_reason: None,
recents_list: Vec::new(),
@@ -220,6 +218,14 @@ impl AuthActivity {
self.context().config()
}
fn bookmarks_client(&self) -> Option<&BookmarksClient> {
self.context().bookmarks_client()
}
fn bookmarks_client_mut(&mut self) -> Option<&mut BookmarksClient> {
self.context_mut().bookmarks_client_mut()
}
/// Returns a reference to theme
fn theme(&self) -> &Theme {
self.context().theme_provider().theme()
@@ -259,9 +265,8 @@ impl Activity for AuthActivity {
// Initialize view
self.init();
// Init bookmarks client
if self.bookmarks_client.is_none() {
if self.bookmarks_client().is_some() {
self.init_bookmarks_client();
// View bookarmsk
self.view_bookmarks();
self.view_recent_connections();
}

View File

@@ -299,14 +299,7 @@ impl AuthActivity {
.bookmarks_list
.iter()
.map(|x| {
Self::fmt_bookmark(
x,
self.bookmarks_client
.as_ref()
.unwrap()
.get_bookmark(x)
.unwrap(),
)
Self::fmt_bookmark(x, self.bookmarks_client().unwrap().get_bookmark(x).unwrap())
})
.collect();
let bookmarks_color = self.theme().auth_bookmarks;
@@ -325,15 +318,7 @@ impl AuthActivity {
let bookmarks: Vec<String> = self
.recents_list
.iter()
.map(|x| {
Self::fmt_recent(
self.bookmarks_client
.as_ref()
.unwrap()
.get_recent(x)
.unwrap(),
)
})
.map(|x| Self::fmt_recent(self.bookmarks_client().unwrap().get_recent(x).unwrap()))
.collect();
let recents_color = self.theme().auth_recents;
assert!(self

View File

@@ -28,6 +28,7 @@
// Locals
use super::store::Store;
use crate::filetransfer::FileTransferParams;
use crate::system::bookmarks_client::BookmarksClient;
use crate::system::config_client::ConfigClient;
use crate::system::theme_provider::ThemeProvider;
@@ -36,6 +37,7 @@ use tuirealm::terminal::TerminalBridge;
/// Context holds data structures shared by the activities
pub struct Context {
ft_params: Option<FileTransferParams>,
bookmarks_client: Option<BookmarksClient>,
config_client: ConfigClient,
pub(crate) store: Store,
pub(crate) terminal: TerminalBridge,
@@ -46,13 +48,15 @@ pub struct Context {
impl Context {
/// Instantiates a new Context
pub fn new(
bookmarks_client: Option<BookmarksClient>,
config_client: ConfigClient,
theme_provider: ThemeProvider,
error: Option<String>,
) -> Context {
Context {
ft_params: None,
bookmarks_client,
config_client,
ft_params: None,
store: Store::init(),
terminal: TerminalBridge::new().expect("Could not initialize terminal"),
theme_provider,
@@ -66,6 +70,14 @@ impl Context {
self.ft_params.as_ref()
}
pub fn bookmarks_client(&self) -> Option<&BookmarksClient> {
self.bookmarks_client.as_ref()
}
pub fn bookmarks_client_mut(&mut self) -> Option<&mut BookmarksClient> {
self.bookmarks_client.as_mut()
}
pub fn config(&self) -> &ConfigClient {
&self.config_client
}