Added user license file and license bad name check

This commit is contained in:
Alexander Rosenberg 2022-10-07 04:57:59 -07:00
parent 569d921e2a
commit 266f30af09
Signed by: Zander671
GPG Key ID: 5FD0394ADBD72730
3 changed files with 71 additions and 31 deletions

View File

@ -1,2 +1,2 @@
[env] [env]
DATA_PATH = { value = "licenses", relative = true } GLOBAL_DATA_PATH = { value = "licenses", relative = true }

View File

@ -6,9 +6,9 @@ if [ "$#" -ge 1 ]; then
PREFIX="$1" PREFIX="$1"
fi fi
export DATA_PATH="$PREFIX/share/license-tool" export GLOBAL_DATA_PATH="$PREFIX/share/license-tool"
cargo install --path . --root "$PREFIX" cargo install --path . --root "$PREFIX"
printf "Installing licenses to '%s'\n" "$DATA_PATH" printf "Installing licenses to '%s'\n" "$GLOBAL_DATA_PATH"
rm -rf "$DATA_PATH" rm -rf "$GLOBAL_DATA_PATH"
cp -R licenses "$DATA_PATH" cp -R licenses "$GLOBAL_DATA_PATH"

View File

@ -2,16 +2,18 @@ use std::env;
use std::fs; use std::fs;
use std::io; use std::io;
use std::process::exit; use std::process::exit;
use std::collections::HashSet;
const DATA_PATH: &str = env!("DATA_PATH"); const GLOBAL_DATA_PATH: &str = env!("GLOBAL_DATA_PATH");
fn main() { fn main() {
let user_data_file = get_user_data_file();
let args: Vec<String> = env::args().collect(); let args: Vec<String> = env::args().collect();
let license_index; let license_index;
let outfile: &str; let outfile: &str;
if args.len() < 2 || args[1] == "-h" { if args.len() < 2 || args[1] == "-h" {
print_help(); print_help(&user_data_file);
return; return
} else if args[1] == "-o" { } else if args[1] == "-o" {
outfile = get_outfile(&args).unwrap_or_else(|| exit(1)); outfile = get_outfile(&args).unwrap_or_else(|| exit(1));
license_index = 3; license_index = 3;
@ -20,45 +22,83 @@ fn main() {
license_index = 1; license_index = 1;
} }
if args.len() <= license_index { if args.len() <= license_index {
print_help(); print_help(&user_data_file);
return; return
} }
copy_license(&args[license_index], outfile); let license_name = &args[license_index];
if license_name.contains("/") {
eprintln!("error: malformatted license name: '{}'", license_name);
exit(1)
}
copy_license(&license_name, outfile, &user_data_file);
} }
fn copy_license(license: &str, outfile: &str) { fn copy_license(license: &str, outfile: &str, user_data_file: &String) {
let license_path = DATA_PATH.to_owned() + "/" + license; let user_path = user_data_file.to_owned() + "/" + license;
let license_content = fs::read_to_string(license_path).unwrap_or_else(|_| { let license_content = fs::read_to_string(user_path).unwrap_or_else(|_| {
eprintln!("error: could not open license for reading: '{}'", license); let global_path = GLOBAL_DATA_PATH.to_owned() + "/" + license;
exit(1); fs::read_to_string(global_path).unwrap_or_else(|_| {
eprintln!("error: could not open license for reading: '{}'", license);
exit(1)
})
}); });
let _ = fs::write(outfile, license_content).or_else(|_| -> io::Result<()> { let _ = fs::write(outfile, license_content).or_else(|_| -> io::Result<()> {
eprintln!("error: could not write license: '{}'", outfile); eprintln!("error: could not write license: '{}'", outfile);
exit(1); exit(1)
}); });
} }
fn get_outfile(args: &Vec<String>) -> Option<&str> { fn get_outfile(args: &Vec<String>) -> Option<&str> {
if args.len() < 3 { if args.len() < 3 {
eprintln!("error: -o requires an option"); eprintln!("error: -o requires an option");
return None; return None
} }
return Some(&args[2]); Some(&args[2])
} }
fn print_help() { fn print_help(user_data_file: &String) {
println!("license-tool [-h] [-o OUTFILE] <license>"); println!("license-tool [-h] [-o OUTFILE] <license>");
println!("Licenses:"); let mut user_dirs = HashSet::<String>::new();
let dirs = fs::read_dir(DATA_PATH); let user_dir = fs::read_dir(user_data_file);
if dirs.is_err() { if user_dir.is_ok() {
eprintln!("error: could not open data directory"); println!("User Licenses:");
exit(1); let user_error_fun = |_| {
} eprintln!("error: could not read user data directory");
for file in dirs.unwrap() { exit(1)
if file.is_err() { };
eprintln!("error: error reading data directory"); for file in user_dir.unwrap() {
exit(1); let file_name = file.unwrap_or_else(user_error_fun).file_name()
.to_string_lossy().to_string();
println!(" {}", file_name);
user_dirs.insert(file_name);
}
}
let global_dir = fs::read_dir(GLOBAL_DATA_PATH);
if global_dir.is_ok() {
println!("Global Licenses:");
let global_error_fun = |_| {
eprintln!("error: error reading data directory");
exit(1)
};
for file in global_dir.unwrap() {
let file_name = file.unwrap_or_else(global_error_fun).file_name()
.to_string_lossy().to_string();
if user_dirs.contains(&file_name) {
println!(" {} [Overridden]", file_name);
} else {
println!(" {}", file_name);
}
} }
println!(" {}", file.as_ref().unwrap().file_name().to_string_lossy());
} }
} }
fn get_user_data_file() -> String {
let xdg_config = env::var("XDG_CONFIG_HOME").unwrap_or_else(|_| {
let home_dir = env::var("HOME").unwrap_or_else(|_| {
eprintln!("error: '$HOME' not set");
exit(1)
});
home_dir + "/.config"
});
xdg_config + "/license-tool"
}