added flag to delete file fter revoke

This commit is contained in:
Edward Shen 2020-04-13 01:00:13 -04:00
parent 587742d09e
commit b6c5bac475
Signed by: edward
GPG key ID: 19182661E818369F

View file

@ -2,7 +2,7 @@ use clap::derive::Clap;
use cli::{KeyInit, KeyRevoke, Opts, SubCommands}; 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::{remove_file, OpenOptions};
use std::{io::Write, process::Command}; use std::{io::Write, process::Command};
mod cli; mod cli;
@ -38,7 +38,7 @@ fn main() -> Result<(), SshKeyCtlError> {
} }
fn init(args: &KeyInit) -> Result<(), SshKeyCtlError> { fn init(args: &KeyInit) -> Result<(), SshKeyCtlError> {
let mut key_pair = KeyPair::generate(args.key_type.0, 0).unwrap(); let mut key_pair = KeyPair::generate(args.key_type.0, 0)?;
*key_pair.comment_mut() = args.comment.clone().unwrap(); *key_pair.comment_mut() = args.comment.clone().unwrap();
let mut ssh_folder = dirs::home_dir().unwrap(); let mut ssh_folder = dirs::home_dir().unwrap();
ssh_folder.push(".ssh"); ssh_folder.push(".ssh");
@ -98,10 +98,8 @@ fn init(args: &KeyInit) -> Result<(), SshKeyCtlError> {
.arg("-p") .arg("-p")
.arg(args.port.to_string()) .arg(args.port.to_string())
.arg(&args.target) .arg(&args.target)
.spawn() .spawn()?
.unwrap() .wait()?;
.wait()
.unwrap();
// todo: edit .ssh/config file // todo: edit .ssh/config file
Ok(()) Ok(())
@ -122,7 +120,7 @@ fn revoke(args: &KeyRevoke) -> Result<(), SshKeyCtlError> {
Some(path) => path, Some(path) => path,
}; };
key_file_path.push(format!("{}.pub", key_file_name)); key_file_path.push(format!("{}.pub", key_file_name));
let key_data = read_to_string(key_file_path).unwrap(); let key_data = read_to_string(&key_file_path)?;
let key_data = key_data.trim().replace("/", "\\/"); let key_data = key_data.trim().replace("/", "\\/");
Command::new("ssh") Command::new("ssh")
.args(&[ .args(&[
@ -131,9 +129,13 @@ fn revoke(args: &KeyRevoke) -> Result<(), SshKeyCtlError> {
// todo: make gnu sed independent // 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()?
.unwrap() .wait()?;
.wait()
.unwrap(); if args.delete_identity_file {
remove_file(&key_file_path)?;
key_file_path.set_extension("");
remove_file(&key_file_path)?;
}
Ok(()) Ok(())
} }