add import

This commit is contained in:
steven-omaha
2023-01-11 14:49:03 +01:00
parent 08b74ce919
commit 686a5cefad
5 changed files with 70 additions and 1 deletions
+1
View File
@@ -1,6 +1,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 SHOW: &str = "show";
pub(crate) const SYNC: &str = "sync";
pub(crate) const UNMANAGED: &str = "unmanaged";
+20 -1
View File
@@ -1,4 +1,7 @@
use clap::{Arg, Command};
use std::path::PathBuf;
use clap::{Arg, ArgMatches, Command};
use path_absolutize::Absolutize;
use crate::action::*;
@@ -16,6 +19,12 @@ fn get_arg_parser() -> Command<'static> {
.arg(Arg::new("group").multiple_values(true)),
)
.subcommand(Command::new(GROUPS).about("show names of imported groups"))
.subcommand(
Command::new(IMPORT)
.about("import one or more group files")
.arg_required_else_help(true)
.arg(Arg::new("files").multiple_values(true)),
)
.subcommand(
Command::new(SHOW)
.about("show packages under an imported group")
@@ -35,3 +44,13 @@ fn get_arg_parser() -> Command<'static> {
pub fn get() -> clap::ArgMatches {
get_arg_parser().get_matches()
}
pub(crate) fn get_file_paths(arg_match: &ArgMatches) -> Vec<PathBuf> {
arg_match
.get_many::<String>("files")
.unwrap()
.cloned()
.map(PathBuf::from)
.map(|path| path.absolutize().unwrap().into_owned())
.collect()
}
+29
View File
@@ -1,12 +1,15 @@
use std::collections::HashSet;
use std::os::unix::fs::symlink;
use anyhow::{ensure, Context, Result};
use clap::ArgMatches;
use crate::action;
use crate::args;
use crate::backend::{Backend, Backends, ToDoPerBackend};
use crate::cmd::run_edit_command;
use crate::env::get_single_var;
use crate::path::get_pacdef_group_dir;
use crate::ui::get_user_confirmation;
use crate::Group;
@@ -34,6 +37,7 @@ impl Pacdef {
self.edit_group_files(groups).context("editing group files")
}
Some((action::GROUPS, _)) => Ok(self.show_groups()),
Some((action::IMPORT, files)) => self.import_groups(files).context("importing groups"),
Some((action::SHOW, groups)) => {
self.show_group_content(groups).context("showing groups")
}
@@ -167,6 +171,31 @@ impl Pacdef {
}
Ok(())
}
fn import_groups(&self, args: &ArgMatches) -> Result<()> {
let files = args::get_file_paths(args);
let groups_dir = get_pacdef_group_dir()?;
for target in files {
let target_name = target.file_name().unwrap().to_str().unwrap();
if !target.exists() {
println!("file {target_name} does not exist, skipping");
continue;
}
let mut link = groups_dir.clone();
link.push(target_name);
if link.exists() {
println!("group {target_name} already exists, skipping");
} else {
symlink(target, link)?;
}
}
Ok(())
}
}
fn show_error(error: anyhow::Error, backend: Box<dyn Backend>) {