From 587742d09e7bd6db70e39952a389e6138076ba1d Mon Sep 17 00:00:00 2001 From: Edward Shen Date: Mon, 13 Apr 2020 00:47:51 -0400 Subject: [PATCH] optimize structopt configs --- src/cli.rs | 27 ++++++++++++++++----------- src/main.rs | 15 ++++++++++----- 2 files changed, 26 insertions(+), 16 deletions(-) diff --git a/src/cli.rs b/src/cli.rs index 1d5aa83..fee6106 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -20,15 +20,15 @@ pub struct KeyInit { pub target: String, #[clap(short = "t", long = "type", default_value = "ed25519")] pub key_type: KeyType, - #[clap(short = "c", long = "comment")] + #[clap(short, long)] /// The comment for the SSH key. Generally, this should be /// `username@hostname` of the computer that generated the key. pub comment: Option, - #[clap(short = "p", long = "port", default_value = "22")] + #[clap(short, default_value = "22")] pub port: u16, - #[clap(short = "P", long = "passphrase")] - pub password: Option, - #[clap(short = "f", long = "--force")] + #[clap(short = "P", long)] + pub passphrase: Option, + #[clap(short, long)] pub force: bool, } @@ -39,7 +39,7 @@ impl From for KeyInit { key_type: key_renew.key_type, comment: key_renew.comment, port: key_renew.port, - password: key_renew.password, + passphrase: key_renew.password, force: key_renew.force, } } @@ -49,8 +49,10 @@ impl From for KeyInit { pub struct KeyRevoke { pub target: String, pub identity_file_path: Option, - #[clap(short = "p", long = "port", default_value = "22")] + #[clap(short, long, default_value = "22")] pub port: u16, + #[clap(short, long)] + pub delete_identity_file: bool, } impl From for KeyRevoke { @@ -59,6 +61,7 @@ impl From for KeyRevoke { target: key_renew.target, identity_file_path: key_renew.identity_file_path, port: key_renew.port, + delete_identity_file: key_renew.delete_identity_file, } } } @@ -68,15 +71,17 @@ pub struct KeyRenew { pub target: String, #[clap(short = "t", long = "type", default_value = "ed25519")] pub key_type: KeyType, - #[clap(short = "c", long = "comment")] + #[clap(short, long)] pub comment: Option, - #[clap(short = "p", long = "port", default_value = "22")] + #[clap(short, long, default_value = "22")] pub port: u16, - #[clap(short = "P", long = "passphrase")] + #[clap(short = "P", long)] pub password: Option, - #[clap(short = "f", long = "--force")] + #[clap(short, long)] pub force: bool, pub identity_file_path: Option, + #[clap(short, long)] + pub delete_identity_file: bool, } #[derive(Debug, Clone)] diff --git a/src/main.rs b/src/main.rs index a48125d..8b9028f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,7 +3,6 @@ use cli::{KeyInit, KeyRevoke, Opts, SubCommands}; use osshkeys::{cipher::Cipher, KeyPair}; use std::fs::read_to_string; use std::fs::OpenOptions; -use std::os::unix::fs::PermissionsExt; use std::{io::Write, process::Command}; mod cli; @@ -67,13 +66,18 @@ fn init(args: &KeyInit) -> Result<(), SshKeyCtlError> { } let mut priv_key_file = priv_key_file.write(true).open(&priv_key_path)?; - let mut perms = priv_key_file.metadata()?.permissions(); - perms.set_mode(0o600); - priv_key_file.set_permissions(perms)?; + #[cfg(unix)] + { + use std::os::unix::fs::PermissionsExt; + let mut perms = priv_key_file.metadata()?.permissions(); + perms.set_mode(0o600); + priv_key_file.set_permissions(perms)?; + } + priv_key_file.write( key_pair .serialize_openssh( - args.password.as_ref().map(String::as_bytes), + args.passphrase.as_ref().map(String::as_bytes), Cipher::Aes256_Ctr, )? .as_bytes(), @@ -124,6 +128,7 @@ fn revoke(args: &KeyRevoke) -> Result<(), SshKeyCtlError> { .args(&[ target, "-C", + // todo: make gnu sed independent &format!("sed -i '/{}/d' .ssh/authorized_keys", key_data), ]) .spawn()