From f05be4cce0f3ffe22371b1809284a051ecc94028 Mon Sep 17 00:00:00 2001 From: steven-omaha <35634100+steven-omaha@users.noreply.github.com> Date: Thu, 12 Jan 2023 11:23:45 +0100 Subject: [PATCH] add 'new' argument --- src/action.rs | 1 + src/args.rs | 6 ++++++ src/core.rs | 19 ++++++++++++++++++- 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/src/action.rs b/src/action.rs index 177d09e..131a15f 100644 --- a/src/action.rs +++ b/src/action.rs @@ -2,6 +2,7 @@ pub(crate) const CLEAN: &str = "clean"; pub(crate) const EDIT: &str = "edit"; pub(crate) const GROUPS: &str = "groups"; pub(crate) const IMPORT: &str = "import"; +pub(crate) const NEW: &str = "new"; pub(crate) const REMOVE: &str = "remove"; pub(crate) const SHOW: &str = "show"; pub(crate) const SYNC: &str = "sync"; diff --git a/src/args.rs b/src/args.rs index 5c76a99..0ef2403 100644 --- a/src/args.rs +++ b/src/args.rs @@ -25,6 +25,12 @@ fn get_arg_parser() -> Command<'static> { .arg_required_else_help(true) .arg(Arg::new("files").multiple_values(true)), ) + .subcommand( + Command::new(NEW) + .about("create new group files") + .arg_required_else_help(true) + .arg(Arg::new("groups").multiple_values(true)), + ) .subcommand( Command::new(REMOVE) .about("remove one or more previously imported groups") diff --git a/src/core.rs b/src/core.rs index 5cda5c9..c4a357c 100644 --- a/src/core.rs +++ b/src/core.rs @@ -1,5 +1,5 @@ use std::collections::HashSet; -use std::fs::remove_file; +use std::fs::{remove_file, File}; use std::os::unix::fs::symlink; use std::path::PathBuf; @@ -38,6 +38,9 @@ impl Pacdef { } Some((action::GROUPS, _)) => Ok(self.show_groups()), Some((action::IMPORT, files)) => self.import_groups(files).context("importing groups"), + Some((action::NEW, files)) => { + self.new_groups(files).context("creating new group files") + } Some((action::REMOVE, groups)) => self.remove_groups(groups).context("removing groups"), Some((action::SHOW, groups)) => { self.show_group_content(groups).context("showing groups") @@ -211,6 +214,20 @@ impl Pacdef { Ok(()) } + + fn new_groups(&self, files: &ArgMatches) -> Result<()> { + let paths = get_assumed_group_file_names(files)?; + + for file in &paths { + ensure!(!file.exists(), "group already exists under {file:?}"); + } + + for file in paths { + File::create(file)?; + } + + Ok(()) + } } fn get_assumed_group_file_names(arg_match: &ArgMatches) -> Result> {