mirror of
https://github.com/veeso/termscp.git
synced 2026-04-04 01:01:33 -07:00
feat: Import bookmarks from ssh config with a CLI command (#364)
* feat: Import bookmarks from ssh config with a CLI command Use import-ssh-hosts to import all the possible hosts by the configured ssh config or the default one on your machine closes #331
This commit is contained in:
committed by
GitHub
parent
4bebec369f
commit
f4156a5059
@@ -30,13 +30,13 @@ impl AuthActivity {
|
||||
pub(super) fn load_bookmark(&mut self, form_tab: FormTab, idx: usize) {
|
||||
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) {
|
||||
// Load parameters into components
|
||||
match form_tab {
|
||||
FormTab::Remote => self.load_remote_bookmark_into_gui(bookmark),
|
||||
FormTab::HostBridge => self.load_host_bridge_bookmark_into_gui(bookmark),
|
||||
}
|
||||
if let Some(key) = self.bookmarks_list.get(idx)
|
||||
&& let Some(bookmark) = bookmarks_cli.get_bookmark(key)
|
||||
{
|
||||
// Load parameters into components
|
||||
match form_tab {
|
||||
FormTab::Remote => self.load_remote_bookmark_into_gui(bookmark),
|
||||
FormTab::HostBridge => self.load_host_bridge_bookmark_into_gui(bookmark),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -99,13 +99,13 @@ impl AuthActivity {
|
||||
pub(super) fn load_recent(&mut self, form_tab: FormTab, idx: usize) {
|
||||
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) {
|
||||
// Load parameters
|
||||
match form_tab {
|
||||
FormTab::Remote => self.load_remote_bookmark_into_gui(bookmark),
|
||||
FormTab::HostBridge => self.load_host_bridge_bookmark_into_gui(bookmark),
|
||||
}
|
||||
if let Some(key) = self.recents_list.get(idx)
|
||||
&& let Some(bookmark) = client.get_recent(key)
|
||||
{
|
||||
// Load parameters
|
||||
match form_tab {
|
||||
FormTab::Remote => self.load_remote_bookmark_into_gui(bookmark),
|
||||
FormTab::HostBridge => self.load_host_bridge_bookmark_into_gui(bookmark),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -129,10 +129,10 @@ impl AuthActivity {
|
||||
|
||||
/// Write bookmarks to file
|
||||
fn write_bookmarks(&mut self) {
|
||||
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());
|
||||
}
|
||||
if let Some(bookmarks_cli) = self.bookmarks_client()
|
||||
&& let Err(err) = bookmarks_cli.write_bookmarks()
|
||||
{
|
||||
self.mount_error(format!("Could not write bookmarks: {err}").as_str());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -126,13 +126,13 @@ impl AuthActivity {
|
||||
self.host_bridge_protocol = protocol;
|
||||
// Update port
|
||||
let port: u16 = self.get_input_port(FormTab::HostBridge);
|
||||
if let HostBridgeProtocol::Remote(remote_protocol) = protocol {
|
||||
if Self::is_port_standard(port) {
|
||||
self.mount_port(
|
||||
FormTab::HostBridge,
|
||||
Self::get_default_port_for_protocol(remote_protocol),
|
||||
);
|
||||
}
|
||||
if let HostBridgeProtocol::Remote(remote_protocol) = protocol
|
||||
&& Self::is_port_standard(port)
|
||||
{
|
||||
self.mount_port(
|
||||
FormTab::HostBridge,
|
||||
Self::get_default_port_for_protocol(remote_protocol),
|
||||
);
|
||||
}
|
||||
}
|
||||
FormMsg::RemoteProtocolChanged(protocol) => {
|
||||
|
||||
@@ -687,30 +687,30 @@ impl AuthActivity {
|
||||
|
||||
/// mount release notes text area
|
||||
pub(super) fn mount_release_notes(&mut self) {
|
||||
if let Some(ctx) = self.context.as_ref() {
|
||||
if let Some(release_notes) = ctx.store().get_string(super::STORE_KEY_RELEASE_NOTES) {
|
||||
// make spans
|
||||
let info_color = self.theme().misc_info_dialog;
|
||||
assert!(
|
||||
self.app
|
||||
.remount(
|
||||
Id::NewVersionChangelog,
|
||||
Box::new(components::ReleaseNotes::new(release_notes, info_color)),
|
||||
vec![]
|
||||
)
|
||||
.is_ok()
|
||||
);
|
||||
assert!(
|
||||
self.app
|
||||
.remount(
|
||||
Id::InstallUpdatePopup,
|
||||
Box::new(components::InstallUpdatePopup::new(info_color)),
|
||||
vec![]
|
||||
)
|
||||
.is_ok()
|
||||
);
|
||||
assert!(self.app.active(&Id::InstallUpdatePopup).is_ok());
|
||||
}
|
||||
if let Some(ctx) = self.context.as_ref()
|
||||
&& let Some(release_notes) = ctx.store().get_string(super::STORE_KEY_RELEASE_NOTES)
|
||||
{
|
||||
// make spans
|
||||
let info_color = self.theme().misc_info_dialog;
|
||||
assert!(
|
||||
self.app
|
||||
.remount(
|
||||
Id::NewVersionChangelog,
|
||||
Box::new(components::ReleaseNotes::new(release_notes, info_color)),
|
||||
vec![]
|
||||
)
|
||||
.is_ok()
|
||||
);
|
||||
assert!(
|
||||
self.app
|
||||
.remount(
|
||||
Id::InstallUpdatePopup,
|
||||
Box::new(components::InstallUpdatePopup::new(info_color)),
|
||||
vec![]
|
||||
)
|
||||
.is_ok()
|
||||
);
|
||||
assert!(self.app.active(&Id::InstallUpdatePopup).is_ok());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
use std::path::PathBuf;
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
use tui_realm_stdlib::Input;
|
||||
use tuirealm::command::{Cmd, CmdResult, Direction, Position};
|
||||
@@ -158,7 +158,7 @@ impl OwnStates {
|
||||
.unwrap_or_else(|| PathBuf::from("/"));
|
||||
|
||||
// if path is `.`, then return None
|
||||
if parent == PathBuf::from(".") {
|
||||
if parent == Path::new(".") {
|
||||
return Suggestion::None;
|
||||
}
|
||||
|
||||
|
||||
@@ -506,10 +506,10 @@ impl Activity for FileTransferActivity {
|
||||
/// This function must be called once before terminating the activity.
|
||||
fn on_destroy(&mut self) -> Option<Context> {
|
||||
// Destroy cache
|
||||
if let Some(cache) = self.cache.take() {
|
||||
if let Err(err) = cache.close() {
|
||||
error!("Failed to delete cache: {}", err);
|
||||
}
|
||||
if let Some(cache) = self.cache.take()
|
||||
&& let Err(err) = cache.close()
|
||||
{
|
||||
error!("Failed to delete cache: {}", err);
|
||||
}
|
||||
// Disable raw mode
|
||||
if let Err(err) = self.context_mut().terminal().disable_raw_mode() {
|
||||
|
||||
@@ -99,27 +99,14 @@ impl SetupActivity {
|
||||
Ok(State::One(StateValue::Usize(idx))) => Some(idx),
|
||||
_ => None,
|
||||
};
|
||||
if let Some(idx) = idx {
|
||||
let key: Option<String> = self.config().iter_ssh_keys().nth(idx).cloned();
|
||||
if let Some(key) = key {
|
||||
match self.config().get_ssh_key(&key) {
|
||||
Ok(opt) => {
|
||||
if let Some((host, username, _)) = opt {
|
||||
if let Err(err) = self.delete_ssh_key(host.as_str(), username.as_str())
|
||||
{
|
||||
// Report error
|
||||
self.mount_error(err.as_str());
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(err) => {
|
||||
// Report error
|
||||
self.mount_error(
|
||||
format!("Could not get ssh key \"{key}\": {err}").as_str(),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
// get ssh key and delete it
|
||||
if let Some(Err(err)) = idx
|
||||
.and_then(|i| self.config().iter_ssh_keys().nth(i).cloned())
|
||||
.and_then(|key| self.config().get_ssh_key(&key))
|
||||
.map(|(host, username, _)| self.delete_ssh_key(host.as_str(), username.as_str()))
|
||||
{
|
||||
// Report error
|
||||
self.mount_error(err.as_str());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -77,16 +77,11 @@ impl SetupActivity {
|
||||
Some(key) => {
|
||||
// Get key path
|
||||
match ctx.config().get_ssh_key(key) {
|
||||
Ok(ssh_key) => match ssh_key {
|
||||
None => Ok(()),
|
||||
Some((_, _, key_path)) => {
|
||||
match edit::edit_file(key_path.as_path()) {
|
||||
Ok(_) => Ok(()),
|
||||
Err(err) => Err(format!("Could not edit ssh key: {err}")),
|
||||
}
|
||||
}
|
||||
None => Ok(()),
|
||||
Some((_, _, key_path)) => match edit::edit_file(key_path.as_path()) {
|
||||
Ok(_) => Ok(()),
|
||||
Err(err) => Err(format!("Could not edit ssh key: {err}")),
|
||||
},
|
||||
Err(err) => Err(format!("Could not read ssh key: {err}")),
|
||||
}
|
||||
}
|
||||
None => Ok(()),
|
||||
|
||||
@@ -126,7 +126,7 @@ impl SetupActivity {
|
||||
.config()
|
||||
.iter_ssh_keys()
|
||||
.map(|x| {
|
||||
let (addr, username, _) = self.config().get_ssh_key(x).ok().unwrap().unwrap();
|
||||
let (addr, username, _) = self.config().get_ssh_key(x).unwrap();
|
||||
format!("{username} at {addr}")
|
||||
})
|
||||
.collect();
|
||||
|
||||
Reference in New Issue
Block a user