use crate::files;
use crate::files::view::View;
use anyhow::{Context, Result};
use clap::{crate_name, crate_version, App, Arg};
fn build_matches() -> clap::ArgMatches<'static> {
let app = App::new(crate_name!())
.version(crate_version!())
.author("Shift <me@shift-d.tk>")
.about("")
.arg(
Arg::with_name("init")
.help("Init important files")
.long("init"),
)
.subcommand(
App::new("create")
.about("Create new password")
.arg(
Arg::with_name("NAME")
.help("Password's name")
.required(true),
)
.arg(
Arg::with_name("category")
.help("Password's category")
.required(false)
.short("c")
.long("category")
.takes_value(true),
),
)
.subcommand(
App::new("view").about("View passwords").arg(
Arg::with_name("name")
.help("Passwords'name")
.required(false),
),
);
app.get_matches()
}
pub fn matches_interaction() -> Result<()> {
let matches = build_matches();
if let Some(create_subcommand) = matches.subcommand_matches("create") {
if let Some(name_value) = create_subcommand.value_of("NAME") {
let content =
files::edit::FileContent::default().context("Failed to open necessary files")?;
let password_name = name_value.to_string();
let category_name = create_subcommand.value_of("category");
content
.save_passwords(category_name, password_name)
.context("Failed to save new 'passwords.toml' content")?;
}
} else if matches.is_present("init") {
files::FilePaths::init().unwrap();
files::edit::FileContent::default()
.unwrap()
.init_config()
.unwrap();
}
if let Some(view_subcommand) = matches.subcommand_matches("view") {
if let Some(name_value) = view_subcommand.value_of("name") {
View::view_password(name_value.to_string()).context("Failed to view password")?;
} else {
View::view_all().context("Failed to view file")?;
}
}
Ok(())
}