mirror of
https://github.com/veeso/termscp.git
synced 2025-12-07 09:36:00 -08:00
Config client shared in the context
This commit is contained in:
@@ -25,55 +25,17 @@
|
||||
*/
|
||||
|
||||
// Locals
|
||||
use super::{ConfigClient, Popup, SetupActivity};
|
||||
use crate::system::environment;
|
||||
use super::SetupActivity;
|
||||
// Ext
|
||||
use crossterm::terminal::{disable_raw_mode, enable_raw_mode};
|
||||
use std::env;
|
||||
use std::path::PathBuf;
|
||||
|
||||
impl SetupActivity {
|
||||
/// ### init_config_dir
|
||||
///
|
||||
/// Initialize configuration directory
|
||||
pub(super) fn init_config_client(&mut self) {
|
||||
match environment::init_config_dir() {
|
||||
Ok(config_dir) => match config_dir {
|
||||
Some(config_dir) => {
|
||||
// Get paths
|
||||
let (config_file, ssh_dir): (PathBuf, PathBuf) =
|
||||
environment::get_config_paths(config_dir.as_path());
|
||||
// Create config client
|
||||
match ConfigClient::new(config_file.as_path(), ssh_dir.as_path()) {
|
||||
Ok(cli) => self.config_cli = Some(cli),
|
||||
Err(err) => {
|
||||
self.popup = Some(Popup::Fatal(format!(
|
||||
"Could not initialize configuration client: {}",
|
||||
err
|
||||
)))
|
||||
}
|
||||
}
|
||||
}
|
||||
None => {
|
||||
self.popup = Some(Popup::Fatal(
|
||||
"No configuration directory is available on your system".to_string(),
|
||||
))
|
||||
}
|
||||
},
|
||||
Err(err) => {
|
||||
self.popup = Some(Popup::Fatal(format!(
|
||||
"Could not initialize configuration directory: {}",
|
||||
err
|
||||
)))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// ### save_config
|
||||
///
|
||||
/// Save configuration
|
||||
pub(super) fn save_config(&mut self) -> Result<(), String> {
|
||||
match &self.config_cli {
|
||||
match self.context.as_ref().unwrap().config_client.as_ref() {
|
||||
Some(cli) => match cli.write_config() {
|
||||
Ok(_) => Ok(()),
|
||||
Err(err) => Err(format!("Could not save configuration: {}", err)),
|
||||
@@ -87,7 +49,7 @@ impl SetupActivity {
|
||||
/// Reset configuration changes; pratically read config from file, overwriting any change made
|
||||
/// since last write action
|
||||
pub(super) fn reset_config_changes(&mut self) -> Result<(), String> {
|
||||
match self.config_cli.as_mut() {
|
||||
match self.context.as_mut().unwrap().config_client.as_mut() {
|
||||
Some(cli) => match cli.read_config() {
|
||||
Ok(_) => Ok(()),
|
||||
Err(err) => Err(format!("Could not restore configuration: {}", err)),
|
||||
@@ -100,7 +62,7 @@ impl SetupActivity {
|
||||
///
|
||||
/// Delete ssh key from config cli
|
||||
pub(super) fn delete_ssh_key(&mut self, host: &str, username: &str) -> Result<(), String> {
|
||||
match self.config_cli.as_mut() {
|
||||
match self.context.as_mut().unwrap().config_client.as_mut() {
|
||||
Some(cli) => match cli.del_ssh_key(host, username) {
|
||||
Ok(_) => Ok(()),
|
||||
Err(err) => Err(format!(
|
||||
@@ -116,58 +78,51 @@ impl SetupActivity {
|
||||
///
|
||||
/// Edit selected ssh key
|
||||
pub(super) fn edit_ssh_key(&mut self) -> Result<(), String> {
|
||||
match self.config_cli.as_ref() {
|
||||
Some(cli) => {
|
||||
// Set text editor
|
||||
env::set_var("EDITOR", cli.get_text_editor());
|
||||
match self.context.as_mut() {
|
||||
None => Ok(()),
|
||||
Some(ctx) => {
|
||||
// Set editor if config client exists
|
||||
if let Some(config_cli) = ctx.config_client.as_ref() {
|
||||
env::set_var("EDITOR", config_cli.get_text_editor());
|
||||
}
|
||||
// Prepare terminal
|
||||
let _ = disable_raw_mode();
|
||||
// Leave alternate mode
|
||||
if let Some(ctx) = self.context.as_mut() {
|
||||
ctx.leave_alternate_screen();
|
||||
}
|
||||
// Check if key exists
|
||||
match cli.iter_ssh_keys().nth(self.ssh_key_idx) {
|
||||
Some(key) => {
|
||||
// Get key path
|
||||
match cli.get_ssh_key(key) {
|
||||
Ok(ssh_key) => match ssh_key {
|
||||
None => Ok(()),
|
||||
Some((_, _, key_path)) => match edit::edit_file(key_path.as_path())
|
||||
{
|
||||
Ok(_) => {
|
||||
// Restore terminal
|
||||
if let Some(ctx) = self.context.as_mut() {
|
||||
// Clear screen
|
||||
ctx.clear_screen();
|
||||
// Enter alternate mode
|
||||
ctx.enter_alternate_screen();
|
||||
ctx.leave_alternate_screen();
|
||||
// Get result
|
||||
let result: Result<(), String> = match ctx.config_client.as_ref() {
|
||||
Some(config_cli) => match config_cli.iter_ssh_keys().nth(self.ssh_key_idx) {
|
||||
Some(key) => {
|
||||
// Get key path
|
||||
match config_cli.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))
|
||||
}
|
||||
}
|
||||
// Re-enable raw mode
|
||||
let _ = enable_raw_mode();
|
||||
Ok(())
|
||||
}
|
||||
Err(err) => {
|
||||
// Restore terminal
|
||||
if let Some(ctx) = self.context.as_mut() {
|
||||
// Clear screen
|
||||
ctx.clear_screen();
|
||||
// Enter alternate mode
|
||||
ctx.enter_alternate_screen();
|
||||
}
|
||||
// Re-enable raw mode
|
||||
let _ = enable_raw_mode();
|
||||
Err(format!("Could not edit ssh key: {}", err))
|
||||
}
|
||||
},
|
||||
},
|
||||
Err(err) => Err(format!("Could not read ssh key: {}", err)),
|
||||
Err(err) => Err(format!("Could not read ssh key: {}", err)),
|
||||
}
|
||||
}
|
||||
}
|
||||
None => Ok(()),
|
||||
},
|
||||
None => Ok(()),
|
||||
}
|
||||
};
|
||||
// Restore terminal
|
||||
// Clear screen
|
||||
ctx.clear_screen();
|
||||
// Enter alternate mode
|
||||
ctx.enter_alternate_screen();
|
||||
// Re-enable raw mode
|
||||
let _ = enable_raw_mode();
|
||||
// Return result
|
||||
result
|
||||
}
|
||||
None => Ok(()),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -180,7 +135,7 @@ impl SetupActivity {
|
||||
username: &str,
|
||||
rsa_key: &str,
|
||||
) -> Result<(), String> {
|
||||
match self.config_cli.as_mut() {
|
||||
match self.context.as_mut().unwrap().config_client.as_mut() {
|
||||
Some(cli) => {
|
||||
// Add key to client
|
||||
match cli.add_ssh_key(host, username, rsa_key) {
|
||||
|
||||
Reference in New Issue
Block a user