add import

This commit is contained in:
timeshifter
2023-01-11 14:49:03 +01:00
parent a4e8ce50f6
commit 951cc7ab66
5 changed files with 70 additions and 1 deletions
Generated
+19
View File
@@ -349,9 +349,28 @@ dependencies = [
"anyhow", "anyhow",
"clap", "clap",
"criterion", "criterion",
"path-absolutize",
"serde_json", "serde_json",
] ]
[[package]]
name = "path-absolutize"
version = "3.0.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0f1d4993b16f7325d90c18c3c6a3327db7808752db8d208cea0acee0abd52c52"
dependencies = [
"path-dedot",
]
[[package]]
name = "path-dedot"
version = "3.0.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9a81540d94551664b72b72829b12bd167c73c9d25fbac0e04fafa8023f7e4901"
dependencies = [
"once_cell",
]
[[package]] [[package]]
name = "pkg-config" name = "pkg-config"
version = "0.3.26" version = "0.3.26"
+1
View File
@@ -8,6 +8,7 @@ alpm = "*"
anyhow = "*" anyhow = "*"
# clap 4 until (at least) 4.0.32 have scrapped actual color support. we stay on 3.x until that's fixed. # clap 4 until (at least) 4.0.32 have scrapped actual color support. we stay on 3.x until that's fixed.
clap = "3.*" clap = "3.*"
path-absolutize = "*"
serde_json = "1.0.91" serde_json = "1.0.91"
[profile.release] [profile.release]
+1
View File
@@ -1,6 +1,7 @@
pub(crate) const CLEAN: &str = "clean"; 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 SHOW: &str = "show"; pub(crate) const SHOW: &str = "show";
pub(crate) const SYNC: &str = "sync"; pub(crate) const SYNC: &str = "sync";
pub(crate) const UNMANAGED: &str = "unmanaged"; 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::*; use crate::action::*;
@@ -16,6 +19,12 @@ fn get_arg_parser() -> Command<'static> {
.arg(Arg::new("group").multiple_values(true)), .arg(Arg::new("group").multiple_values(true)),
) )
.subcommand(Command::new(GROUPS).about("show names of imported groups")) .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( .subcommand(
Command::new(SHOW) Command::new(SHOW)
.about("show packages under an imported group") .about("show packages under an imported group")
@@ -35,3 +44,13 @@ fn get_arg_parser() -> Command<'static> {
pub fn get() -> clap::ArgMatches { pub fn get() -> clap::ArgMatches {
get_arg_parser().get_matches() 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::collections::HashSet;
use std::os::unix::fs::symlink;
use anyhow::{ensure, Context, Result}; use anyhow::{ensure, Context, Result};
use clap::ArgMatches; use clap::ArgMatches;
use crate::action; use crate::action;
use crate::args;
use crate::backend::{Backend, Backends, ToDoPerBackend}; use crate::backend::{Backend, Backends, ToDoPerBackend};
use crate::cmd::run_edit_command; use crate::cmd::run_edit_command;
use crate::env::get_single_var; use crate::env::get_single_var;
use crate::path::get_pacdef_group_dir;
use crate::ui::get_user_confirmation; use crate::ui::get_user_confirmation;
use crate::Group; use crate::Group;
@@ -34,6 +37,7 @@ impl Pacdef {
self.edit_group_files(groups).context("editing group files") self.edit_group_files(groups).context("editing group files")
} }
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::SHOW, groups)) => { Some((action::SHOW, groups)) => {
self.show_group_content(groups).context("showing groups") self.show_group_content(groups).context("showing groups")
} }
@@ -167,6 +171,31 @@ impl Pacdef {
} }
Ok(()) 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>) { fn show_error(error: anyhow::Error, backend: Box<dyn Backend>) {