optimize structopt configs

This commit is contained in:
Edward Shen 2020-04-13 00:47:51 -04:00
parent 7494f74cb2
commit 587742d09e
Signed by: edward
GPG key ID: 19182661E818369F
2 changed files with 26 additions and 16 deletions

View file

@ -20,15 +20,15 @@ pub struct KeyInit {
pub target: String, pub target: String,
#[clap(short = "t", long = "type", default_value = "ed25519")] #[clap(short = "t", long = "type", default_value = "ed25519")]
pub key_type: KeyType, pub key_type: KeyType,
#[clap(short = "c", long = "comment")] #[clap(short, long)]
/// The comment for the SSH key. Generally, this should be /// The comment for the SSH key. Generally, this should be
/// `username@hostname` of the computer that generated the key. /// `username@hostname` of the computer that generated the key.
pub comment: Option<String>, pub comment: Option<String>,
#[clap(short = "p", long = "port", default_value = "22")] #[clap(short, default_value = "22")]
pub port: u16, pub port: u16,
#[clap(short = "P", long = "passphrase")] #[clap(short = "P", long)]
pub password: Option<String>, pub passphrase: Option<String>,
#[clap(short = "f", long = "--force")] #[clap(short, long)]
pub force: bool, pub force: bool,
} }
@ -39,7 +39,7 @@ impl From<KeyRenew> for KeyInit {
key_type: key_renew.key_type, key_type: key_renew.key_type,
comment: key_renew.comment, comment: key_renew.comment,
port: key_renew.port, port: key_renew.port,
password: key_renew.password, passphrase: key_renew.password,
force: key_renew.force, force: key_renew.force,
} }
} }
@ -49,8 +49,10 @@ impl From<KeyRenew> for KeyInit {
pub struct KeyRevoke { pub struct KeyRevoke {
pub target: String, pub target: String,
pub identity_file_path: Option<String>, pub identity_file_path: Option<String>,
#[clap(short = "p", long = "port", default_value = "22")] #[clap(short, long, default_value = "22")]
pub port: u16, pub port: u16,
#[clap(short, long)]
pub delete_identity_file: bool,
} }
impl From<KeyRenew> for KeyRevoke { impl From<KeyRenew> for KeyRevoke {
@ -59,6 +61,7 @@ impl From<KeyRenew> for KeyRevoke {
target: key_renew.target, target: key_renew.target,
identity_file_path: key_renew.identity_file_path, identity_file_path: key_renew.identity_file_path,
port: key_renew.port, port: key_renew.port,
delete_identity_file: key_renew.delete_identity_file,
} }
} }
} }
@ -68,15 +71,17 @@ pub struct KeyRenew {
pub target: String, pub target: String,
#[clap(short = "t", long = "type", default_value = "ed25519")] #[clap(short = "t", long = "type", default_value = "ed25519")]
pub key_type: KeyType, pub key_type: KeyType,
#[clap(short = "c", long = "comment")] #[clap(short, long)]
pub comment: Option<String>, pub comment: Option<String>,
#[clap(short = "p", long = "port", default_value = "22")] #[clap(short, long, default_value = "22")]
pub port: u16, pub port: u16,
#[clap(short = "P", long = "passphrase")] #[clap(short = "P", long)]
pub password: Option<String>, pub password: Option<String>,
#[clap(short = "f", long = "--force")] #[clap(short, long)]
pub force: bool, pub force: bool,
pub identity_file_path: Option<String>, pub identity_file_path: Option<String>,
#[clap(short, long)]
pub delete_identity_file: bool,
} }
#[derive(Debug, Clone)] #[derive(Debug, Clone)]

View file

@ -3,7 +3,6 @@ use cli::{KeyInit, KeyRevoke, Opts, SubCommands};
use osshkeys::{cipher::Cipher, KeyPair}; use osshkeys::{cipher::Cipher, KeyPair};
use std::fs::read_to_string; use std::fs::read_to_string;
use std::fs::OpenOptions; use std::fs::OpenOptions;
use std::os::unix::fs::PermissionsExt;
use std::{io::Write, process::Command}; use std::{io::Write, process::Command};
mod cli; 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 priv_key_file = priv_key_file.write(true).open(&priv_key_path)?;
let mut perms = priv_key_file.metadata()?.permissions(); #[cfg(unix)]
perms.set_mode(0o600); {
priv_key_file.set_permissions(perms)?; 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( priv_key_file.write(
key_pair key_pair
.serialize_openssh( .serialize_openssh(
args.password.as_ref().map(String::as_bytes), args.passphrase.as_ref().map(String::as_bytes),
Cipher::Aes256_Ctr, Cipher::Aes256_Ctr,
)? )?
.as_bytes(), .as_bytes(),
@ -124,6 +128,7 @@ fn revoke(args: &KeyRevoke) -> Result<(), SshKeyCtlError> {
.args(&[ .args(&[
target, target,
"-C", "-C",
// todo: make gnu sed independent
&format!("sed -i '/{}/d' .ssh/authorized_keys", key_data), &format!("sed -i '/{}/d' .ssh/authorized_keys", key_data),
]) ])
.spawn() .spawn()