add 'new' argument

This commit is contained in:
steven-omaha
2023-01-12 11:23:45 +01:00
parent b5d6aa9ffb
commit f05be4cce0
3 changed files with 25 additions and 1 deletions
+1
View File
@@ -2,6 +2,7 @@ pub(crate) const CLEAN: &str = "clean";
pub(crate) const EDIT: &str = "edit"; pub(crate) const EDIT: &str = "edit";
pub(crate) const GROUPS: &str = "groups"; pub(crate) const GROUPS: &str = "groups";
pub(crate) const IMPORT: &str = "import"; pub(crate) const IMPORT: &str = "import";
pub(crate) const NEW: &str = "new";
pub(crate) const REMOVE: &str = "remove"; pub(crate) const REMOVE: &str = "remove";
pub(crate) const SHOW: &str = "show"; pub(crate) const SHOW: &str = "show";
pub(crate) const SYNC: &str = "sync"; pub(crate) const SYNC: &str = "sync";
+6
View File
@@ -25,6 +25,12 @@ fn get_arg_parser() -> Command<'static> {
.arg_required_else_help(true) .arg_required_else_help(true)
.arg(Arg::new("files").multiple_values(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( .subcommand(
Command::new(REMOVE) Command::new(REMOVE)
.about("remove one or more previously imported groups") .about("remove one or more previously imported groups")
+18 -1
View File
@@ -1,5 +1,5 @@
use std::collections::HashSet; use std::collections::HashSet;
use std::fs::remove_file; use std::fs::{remove_file, File};
use std::os::unix::fs::symlink; use std::os::unix::fs::symlink;
use std::path::PathBuf; use std::path::PathBuf;
@@ -38,6 +38,9 @@ impl Pacdef {
} }
Some((action::GROUPS, _)) => Ok(self.show_groups()), Some((action::GROUPS, _)) => Ok(self.show_groups()),
Some((action::IMPORT, files)) => self.import_groups(files).context("importing 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::REMOVE, groups)) => self.remove_groups(groups).context("removing groups"),
Some((action::SHOW, groups)) => { Some((action::SHOW, groups)) => {
self.show_group_content(groups).context("showing groups") self.show_group_content(groups).context("showing groups")
@@ -211,6 +214,20 @@ impl Pacdef {
Ok(()) 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<Vec<PathBuf>> { fn get_assumed_group_file_names(arg_match: &ArgMatches) -> Result<Vec<PathBuf>> {