feat: add ssh.rs
This commit is contained in:
414
ssh-rs/Cargo.lock
generated
Normal file
414
ssh-rs/Cargo.lock
generated
Normal file
@@ -0,0 +1,414 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 4
|
||||
|
||||
[[package]]
|
||||
name = "anstream"
|
||||
version = "0.6.21"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "43d5b281e737544384e969a5ccad3f1cdd24b48086a0fc1b2a5262a26b8f4f4a"
|
||||
dependencies = [
|
||||
"anstyle",
|
||||
"anstyle-parse",
|
||||
"anstyle-query",
|
||||
"anstyle-wincon",
|
||||
"colorchoice",
|
||||
"is_terminal_polyfill",
|
||||
"utf8parse",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "anstyle"
|
||||
version = "1.0.13"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5192cca8006f1fd4f7237516f40fa183bb07f8fbdfedaa0036de5ea9b0b45e78"
|
||||
|
||||
[[package]]
|
||||
name = "anstyle-parse"
|
||||
version = "0.2.7"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "4e7644824f0aa2c7b9384579234ef10eb7efb6a0deb83f9630a49594dd9c15c2"
|
||||
dependencies = [
|
||||
"utf8parse",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "anstyle-query"
|
||||
version = "1.1.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "40c48f72fd53cd289104fc64099abca73db4166ad86ea0b4341abe65af83dadc"
|
||||
dependencies = [
|
||||
"windows-sys",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "anstyle-wincon"
|
||||
version = "3.0.11"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "291e6a250ff86cd4a820112fb8898808a366d8f9f58ce16d1f538353ad55747d"
|
||||
dependencies = [
|
||||
"anstyle",
|
||||
"once_cell_polyfill",
|
||||
"windows-sys",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "bitflags"
|
||||
version = "2.10.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "812e12b5285cc515a9c72a5c1d3b6d46a19dac5acfef5265968c166106e31dd3"
|
||||
|
||||
[[package]]
|
||||
name = "cfg-if"
|
||||
version = "1.0.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9330f8b2ff13f34540b44e946ef35111825727b38d33286ef986142615121801"
|
||||
|
||||
[[package]]
|
||||
name = "clap"
|
||||
version = "4.5.53"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c9e340e012a1bf4935f5282ed1436d1489548e8f72308207ea5df0e23d2d03f8"
|
||||
dependencies = [
|
||||
"clap_builder",
|
||||
"clap_derive",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "clap_builder"
|
||||
version = "4.5.53"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d76b5d13eaa18c901fd2f7fca939fefe3a0727a953561fefdf3b2922b8569d00"
|
||||
dependencies = [
|
||||
"anstream",
|
||||
"anstyle",
|
||||
"clap_lex",
|
||||
"strsim",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "clap_derive"
|
||||
version = "4.5.49"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "2a0b5487afeab2deb2ff4e03a807ad1a03ac532ff5a2cee5d86884440c7f7671"
|
||||
dependencies = [
|
||||
"heck",
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "clap_lex"
|
||||
version = "0.7.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a1d728cc89cf3aee9ff92b05e62b19ee65a02b5702cff7d5a377e32c6ae29d8d"
|
||||
|
||||
[[package]]
|
||||
name = "colorchoice"
|
||||
version = "1.0.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b05b61dc5112cbb17e4b6cd61790d9845d13888356391624cbe7e41efeac1e75"
|
||||
|
||||
[[package]]
|
||||
name = "dirs-next"
|
||||
version = "2.0.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b98cf8ebf19c3d1b223e151f99a4f9f0690dca41414773390fc824184ac833e1"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"dirs-sys-next",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "dirs-sys-next"
|
||||
version = "0.1.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "4ebda144c4fe02d1f7ea1a7d9641b6fc6b580adcfa024ae48797ecdeb6825b4d"
|
||||
dependencies = [
|
||||
"libc",
|
||||
"redox_users",
|
||||
"winapi",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "getrandom"
|
||||
version = "0.2.16"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "335ff9f135e4384c8150d6f27c6daed433577f86b4750418338c01a1a2528592"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"libc",
|
||||
"wasi",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "heck"
|
||||
version = "0.5.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea"
|
||||
|
||||
[[package]]
|
||||
name = "is_terminal_polyfill"
|
||||
version = "1.70.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a6cb138bb79a146c1bd460005623e142ef0181e3d0219cb493e02f7d08a35695"
|
||||
|
||||
[[package]]
|
||||
name = "itoa"
|
||||
version = "1.0.17"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "92ecc6618181def0457392ccd0ee51198e065e016d1d527a7ac1b6dc7c1f09d2"
|
||||
|
||||
[[package]]
|
||||
name = "lazy_static"
|
||||
version = "1.5.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe"
|
||||
|
||||
[[package]]
|
||||
name = "libc"
|
||||
version = "0.2.178"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "37c93d8daa9d8a012fd8ab92f088405fb202ea0b6ab73ee2482ae66af4f42091"
|
||||
|
||||
[[package]]
|
||||
name = "libredox"
|
||||
version = "0.1.12"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3d0b95e02c851351f877147b7deea7b1afb1df71b63aa5f8270716e0c5720616"
|
||||
dependencies = [
|
||||
"bitflags",
|
||||
"libc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "memchr"
|
||||
version = "2.7.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f52b00d39961fc5b2736ea853c9cc86238e165017a493d1d5c8eac6bdc4cc273"
|
||||
|
||||
[[package]]
|
||||
name = "once_cell_polyfill"
|
||||
version = "1.70.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "384b8ab6d37215f3c5301a95a4accb5d64aa607f1fcb26a11b5303878451b4fe"
|
||||
|
||||
[[package]]
|
||||
name = "proc-macro2"
|
||||
version = "1.0.104"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9695f8df41bb4f3d222c95a67532365f569318332d03d5f3f67f37b20e6ebdf0"
|
||||
dependencies = [
|
||||
"unicode-ident",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "quote"
|
||||
version = "1.0.42"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a338cc41d27e6cc6dce6cefc13a0729dfbb81c262b1f519331575dd80ef3067f"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "redox_users"
|
||||
version = "0.4.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ba009ff324d1fc1b900bd1fdb31564febe58a8ccc8a6fdbb93b543d33b13ca43"
|
||||
dependencies = [
|
||||
"getrandom",
|
||||
"libredox",
|
||||
"thiserror",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rust_util"
|
||||
version = "0.6.50"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "01736dfb1c97fec6b874d4939aac6db3b748c61cfd62183c7b80c4f1db003635"
|
||||
dependencies = [
|
||||
"lazy_static",
|
||||
"libc",
|
||||
"term",
|
||||
"term_size",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rustversion"
|
||||
version = "1.0.22"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b39cdef0fa800fc44525c84ccb54a029961a8215f9619753635a9c0d2538d46d"
|
||||
|
||||
[[package]]
|
||||
name = "serde"
|
||||
version = "1.0.228"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9a8e94ea7f378bd32cbbd37198a4a91436180c5bb472411e48b5ec2e2124ae9e"
|
||||
dependencies = [
|
||||
"serde_core",
|
||||
"serde_derive",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serde_core"
|
||||
version = "1.0.228"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "41d385c7d4ca58e59fc732af25c3983b67ac852c1a25000afe1175de458b67ad"
|
||||
dependencies = [
|
||||
"serde_derive",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serde_derive"
|
||||
version = "1.0.228"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d540f220d3187173da220f885ab66608367b6574e925011a9353e4badda91d79"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serde_json"
|
||||
version = "1.0.148"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3084b546a1dd6289475996f182a22aba973866ea8e8b02c51d9f46b1336a22da"
|
||||
dependencies = [
|
||||
"itoa",
|
||||
"memchr",
|
||||
"serde",
|
||||
"serde_core",
|
||||
"zmij",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "ssh-rs"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"clap",
|
||||
"rust_util",
|
||||
"serde",
|
||||
"serde_json",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "strsim"
|
||||
version = "0.11.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f"
|
||||
|
||||
[[package]]
|
||||
name = "syn"
|
||||
version = "2.0.112"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "21f182278bf2d2bcb3c88b1b08a37df029d71ce3d3ae26168e3c653b213b99d4"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"unicode-ident",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "term"
|
||||
version = "0.7.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c59df8ac95d96ff9bede18eb7300b0fda5e5d8d90960e76f8e14ae765eedbf1f"
|
||||
dependencies = [
|
||||
"dirs-next",
|
||||
"rustversion",
|
||||
"winapi",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "term_size"
|
||||
version = "0.3.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1e4129646ca0ed8f45d09b929036bafad5377103edd06e50bf574b353d2b08d9"
|
||||
dependencies = [
|
||||
"libc",
|
||||
"winapi",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "thiserror"
|
||||
version = "1.0.69"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b6aaf5339b578ea85b50e080feb250a3e8ae8cfcdff9a461c9ec2904bc923f52"
|
||||
dependencies = [
|
||||
"thiserror-impl",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "thiserror-impl"
|
||||
version = "1.0.69"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "4fee6c4efc90059e10f81e6d42c60a18f76588c3d74cb83a0b242a2b6c7504c1"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "unicode-ident"
|
||||
version = "1.0.22"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9312f7c4f6ff9069b165498234ce8be658059c6728633667c526e27dc2cf1df5"
|
||||
|
||||
[[package]]
|
||||
name = "utf8parse"
|
||||
version = "0.2.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821"
|
||||
|
||||
[[package]]
|
||||
name = "wasi"
|
||||
version = "0.11.1+wasi-snapshot-preview1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ccf3ec651a847eb01de73ccad15eb7d99f80485de043efb2f370cd654f4ea44b"
|
||||
|
||||
[[package]]
|
||||
name = "winapi"
|
||||
version = "0.3.9"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419"
|
||||
dependencies = [
|
||||
"winapi-i686-pc-windows-gnu",
|
||||
"winapi-x86_64-pc-windows-gnu",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "winapi-i686-pc-windows-gnu"
|
||||
version = "0.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
|
||||
|
||||
[[package]]
|
||||
name = "winapi-x86_64-pc-windows-gnu"
|
||||
version = "0.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
|
||||
|
||||
[[package]]
|
||||
name = "windows-link"
|
||||
version = "0.2.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5"
|
||||
|
||||
[[package]]
|
||||
name = "windows-sys"
|
||||
version = "0.61.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ae137229bcbd6cdf0f7b80a31df61766145077ddf49416a728b02cb3921ff3fc"
|
||||
dependencies = [
|
||||
"windows-link",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "zmij"
|
||||
version = "1.0.7"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "de9211a9f64b825911bdf0240f58b7a8dac217fe260fc61f080a07f61372fbd5"
|
||||
10
ssh-rs/Cargo.toml
Normal file
10
ssh-rs/Cargo.toml
Normal file
@@ -0,0 +1,10 @@
|
||||
[package]
|
||||
name = "ssh-rs"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
rust_util = "0.6.50"
|
||||
clap = { version = "4.5.49", features = ["derive"] }
|
||||
serde = { version = "1.0.228", features = ["derive"] }
|
||||
serde_json = "1.0.145"
|
||||
233
ssh-rs/src/main.rs
Normal file
233
ssh-rs/src/main.rs
Normal file
@@ -0,0 +1,233 @@
|
||||
use clap::Parser;
|
||||
use rust_util::{
|
||||
debugging, failure_and_exit, information, opt_result, simple_error, success, util_cmd, util_env,
|
||||
util_file, util_term, XResult,
|
||||
};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::collections::BTreeMap;
|
||||
use std::process::Command;
|
||||
|
||||
#[derive(Debug, Parser)]
|
||||
#[command(name = "ssh-rs", bin_name = "ssh.rs")]
|
||||
#[command(about = "SSH util", long_about = None)]
|
||||
struct SshRsArgs {
|
||||
/// Forward agent
|
||||
#[arg(long, short = 'f')]
|
||||
pub forward_agent: Option<bool>,
|
||||
/// Proxy
|
||||
#[arg(long, short = 'p')]
|
||||
pub proxy: Option<bool>,
|
||||
/// [username@]host
|
||||
pub username_and_host: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
struct SshConfig {
|
||||
pub default_forward_agent: Option<bool>,
|
||||
pub default_proxy: Option<bool>,
|
||||
pub default_username: Option<String>,
|
||||
pub profiles: BTreeMap<String, SshProfile>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
struct SshProfile {
|
||||
pub default_username: Option<String>,
|
||||
pub alias: Option<Vec<String>>,
|
||||
pub host: String,
|
||||
pub proxy: Option<bool>,
|
||||
pub forward_agent: Option<bool>,
|
||||
pub comment: Option<String>,
|
||||
}
|
||||
|
||||
impl SshConfig {
|
||||
fn find_profiles(&self, profile: &str) -> Vec<(&String, &SshProfile)> {
|
||||
let mut found = vec![];
|
||||
for (k, v) in &self.profiles {
|
||||
if k == profile {
|
||||
found.push((k, v));
|
||||
} else {
|
||||
if let Some(alias) = &v.alias {
|
||||
if alias.contains(&profile.to_string()) {
|
||||
found.push((k, v));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
found
|
||||
}
|
||||
}
|
||||
|
||||
const ENV_SSH_RS_CONFIG_FILE: &str = "SSH_RS_CONFIG_FILE";
|
||||
const SSH_RS_CONFIG_FILE: &str = "~/.config/ssh-rs-config.json";
|
||||
const CMD_SSH: &str = "ssh";
|
||||
|
||||
fn main() -> XResult<()> {
|
||||
let args = SshRsArgs::parse();
|
||||
let ssh_rs_config = load_ssh_rs_config()?;
|
||||
|
||||
let username_and_host = match args.username_and_host {
|
||||
None => {
|
||||
success!("Total {} server(s):", ssh_rs_config.profiles.len());
|
||||
let mut max_profile_id_len = 0_usize;
|
||||
let mut max_host_len = 0_usize;
|
||||
for (profile_id, profile) in &ssh_rs_config.profiles {
|
||||
if profile_id.len() > max_profile_id_len {
|
||||
max_profile_id_len = profile_id.len();
|
||||
}
|
||||
if profile.host.len() > max_host_len {
|
||||
max_host_len = profile.host.len();
|
||||
}
|
||||
}
|
||||
for (profile_id, profile) in &ssh_rs_config.profiles {
|
||||
let mut features = vec![];
|
||||
if let Some(true) = profile.forward_agent {
|
||||
features.push("forward_agent");
|
||||
}
|
||||
if let Some(true) = profile.proxy {
|
||||
features.push("proxy");
|
||||
}
|
||||
println!(
|
||||
"- {} : {}{} {}{}{} # {}{}",
|
||||
pad(profile_id, max_profile_id_len),
|
||||
util_term::GREEN,
|
||||
pad(&profile.host, max_host_len),
|
||||
util_term::YELLOW,
|
||||
match &profile.alias {
|
||||
None => {
|
||||
"".to_string()
|
||||
}
|
||||
Some(alias) => {
|
||||
format!("alias: [{}]", alias.join(", "))
|
||||
}
|
||||
},
|
||||
util_term::END,
|
||||
profile.comment.clone().unwrap_or_else(|| "-".to_string()),
|
||||
if features.is_empty() {
|
||||
"".to_string()
|
||||
} else {
|
||||
format!(" ;[{}]", features.join(", "))
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
let mut features = vec![];
|
||||
if let Some(true) = ssh_rs_config.default_forward_agent {
|
||||
features.push("forward_agent");
|
||||
}
|
||||
if let Some(true) = ssh_rs_config.default_proxy {
|
||||
features.push("proxy");
|
||||
}
|
||||
if !features.is_empty() {
|
||||
println!();
|
||||
information!("Global default features: [{}]", features.join(", "));
|
||||
}
|
||||
|
||||
return Ok(());
|
||||
}
|
||||
Some(username_and_host) => username_and_host,
|
||||
};
|
||||
|
||||
let (username, host) = parse_username_and_host(&username_and_host)?;
|
||||
|
||||
let profiles = ssh_rs_config.find_profiles(&host);
|
||||
if profiles.is_empty() {
|
||||
return simple_error!("Profile not found");
|
||||
}
|
||||
if profiles.len() > 1 {
|
||||
let profile_names = profiles
|
||||
.iter()
|
||||
.map(|p| p.0.to_string())
|
||||
.collect::<Vec<_>>()
|
||||
.join(", ");
|
||||
return simple_error!("Multiple profiles found: {}", profile_names);
|
||||
}
|
||||
let profile = profiles[0].1;
|
||||
|
||||
debugging!("Found profile: {:#?}", profile);
|
||||
|
||||
let ssh_forward_agent = args.forward_agent.clone().unwrap_or_else(|| {
|
||||
profile.forward_agent.unwrap_or_else(|| //-
|
||||
ssh_rs_config.default_forward_agent.unwrap_or(false))
|
||||
});
|
||||
let ssh_proxy = args.proxy.clone().unwrap_or_else(|| {
|
||||
profile.proxy.unwrap_or_else(|| //-
|
||||
ssh_rs_config.default_proxy.unwrap_or(false))
|
||||
});
|
||||
let ssh_username = username.unwrap_or_else(|| {
|
||||
profile.default_username.clone().unwrap_or_else(|| {
|
||||
ssh_rs_config
|
||||
.default_username
|
||||
.clone()
|
||||
.unwrap_or_else(|| "root".to_string())
|
||||
})
|
||||
});
|
||||
let ssh_host = profile.host.clone();
|
||||
|
||||
let mut cmd = Command::new(CMD_SSH);
|
||||
if ssh_forward_agent {
|
||||
cmd.args(&["-o", "ForwardAgent=yes"]);
|
||||
}
|
||||
if ssh_proxy {
|
||||
cmd.args(&["-o", "ProxyCommand=nc -X 5 -x 127.0.0.1:1080 %h %p"]);
|
||||
}
|
||||
cmd.arg(&format!("{}@{}", ssh_username, ssh_host));
|
||||
|
||||
success!(
|
||||
"{} {}",
|
||||
CMD_SSH,
|
||||
cmd.get_args()
|
||||
.map(|s| s.to_string_lossy().to_string())
|
||||
.collect::<Vec<_>>()
|
||||
.join(" ")
|
||||
);
|
||||
match util_cmd::run_command_and_wait(&mut cmd) {
|
||||
Ok(exit_status) => {
|
||||
debugging!("Command exited with status: {}", exit_status);
|
||||
if !exit_status.success() {
|
||||
failure_and_exit!("Exit with error: {}", exit_status);
|
||||
}
|
||||
}
|
||||
Err(err) => {
|
||||
failure_and_exit!("SSH command failed: {}", err);
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn pad(str: &str, width: usize) -> String {
|
||||
if str.len() >= width {
|
||||
return str.to_string();
|
||||
}
|
||||
format!("{}{}", str, " ".repeat(width - str.len()))
|
||||
}
|
||||
|
||||
fn parse_username_and_host(username_and_host: &str) -> XResult<(Option<String>, String)> {
|
||||
if username_and_host.is_empty() {
|
||||
return simple_error!("Empty username@host");
|
||||
}
|
||||
let username_and_host_parts = username_and_host.split("@").collect::<Vec<_>>();
|
||||
if username_and_host_parts.len() == 1 {
|
||||
return Ok((None, username_and_host_parts[0].to_string()));
|
||||
}
|
||||
if username_and_host_parts.len() > 2 {
|
||||
return simple_error!("Bad username@host: {}", username_and_host);
|
||||
}
|
||||
Ok((
|
||||
Some(username_and_host_parts[0].to_string()),
|
||||
username_and_host_parts[1].to_string(),
|
||||
))
|
||||
}
|
||||
|
||||
fn load_ssh_rs_config() -> XResult<SshConfig> {
|
||||
let config_file = get_ssh_rs_config_file();
|
||||
let config_content = util_file::read_file_content(&config_file)?;
|
||||
let config: SshConfig = opt_result!(
|
||||
serde_json::from_str(config_content.as_str()),
|
||||
"Parse config failed: {}"
|
||||
);
|
||||
Ok(config)
|
||||
}
|
||||
|
||||
fn get_ssh_rs_config_file() -> String {
|
||||
util_env::env_var(ENV_SSH_RS_CONFIG_FILE).unwrap_or_else(|| SSH_RS_CONFIG_FILE.to_string())
|
||||
}
|
||||
Reference in New Issue
Block a user