optimize structopt configs
This commit is contained in:
parent
7494f74cb2
commit
587742d09e
2 changed files with 26 additions and 16 deletions
27
src/cli.rs
27
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<String>,
|
||||
#[clap(short = "p", long = "port", default_value = "22")]
|
||||
#[clap(short, default_value = "22")]
|
||||
pub port: u16,
|
||||
#[clap(short = "P", long = "passphrase")]
|
||||
pub password: Option<String>,
|
||||
#[clap(short = "f", long = "--force")]
|
||||
#[clap(short = "P", long)]
|
||||
pub passphrase: Option<String>,
|
||||
#[clap(short, long)]
|
||||
pub force: bool,
|
||||
}
|
||||
|
||||
|
@ -39,7 +39,7 @@ impl From<KeyRenew> 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<KeyRenew> for KeyInit {
|
|||
pub struct KeyRevoke {
|
||||
pub target: String,
|
||||
pub identity_file_path: Option<String>,
|
||||
#[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<KeyRenew> for KeyRevoke {
|
||||
|
@ -59,6 +61,7 @@ impl From<KeyRenew> 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<String>,
|
||||
#[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<String>,
|
||||
#[clap(short = "f", long = "--force")]
|
||||
#[clap(short, long)]
|
||||
pub force: bool,
|
||||
pub identity_file_path: Option<String>,
|
||||
#[clap(short, long)]
|
||||
pub delete_identity_file: bool,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
|
|
15
src/main.rs
15
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()
|
||||
|
|
Loading…
Reference in a new issue