implement filenames on commandline
This commit is contained in:
parent
12da2e2970
commit
708c57e726
3 changed files with 57 additions and 15 deletions
|
|
@ -12,11 +12,26 @@ enum DirListEntry {
|
|||
Text(String)
|
||||
}
|
||||
|
||||
impl ToString for DirListEntry {
|
||||
impl DirListEntry {
|
||||
fn to_string(&self) -> String {
|
||||
match self {
|
||||
DirListEntry::Text(line) => line.clone(),
|
||||
DirListEntry::Path(path) => path.display().to_string()[2..].to_string(),
|
||||
// TODO only strip first to chars if they're `./`
|
||||
DirListEntry::Path(path) => {
|
||||
let checkstring = path.display().to_string();
|
||||
if &checkstring[0..2] == "./" {
|
||||
path.display().to_string()[2..].to_string()
|
||||
} else {
|
||||
path.display().to_string()
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
fn exists(&self) -> bool {
|
||||
match self {
|
||||
DirListEntry::Text(filename) => Path::new(filename).exists(),
|
||||
DirListEntry::Path(path) => path.exists(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -72,12 +87,12 @@ impl DirList {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn from_list(list: &[String]) -> Self {
|
||||
pub fn from_list(list: &[PathBuf]) -> Self {
|
||||
Self {
|
||||
safe_source: false,
|
||||
noop: false,
|
||||
verbose: false,
|
||||
entries: list.iter().map(|l| DirListEntry::Text(l.to_string())).collect(),
|
||||
entries: list.iter().map(|l| DirListEntry::Path(l.clone())).collect(),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -102,8 +117,20 @@ impl DirList {
|
|||
let mut intermediate_files: HashMap<String, String> = HashMap::new();
|
||||
|
||||
self.run_basic_checks(target_list)?;
|
||||
|
||||
if ! self.safe_source {
|
||||
println!("Implement check if target files already exist");
|
||||
// if target == source => next
|
||||
// if target exists and is not in source list => Err
|
||||
// if target exists and is in source list => use intermediate files, which is normal functionality below
|
||||
let tgt_len = target_list.entries.len();
|
||||
for i in 0..tgt_len {
|
||||
if self.entries[i].to_string() != target_list.entries[i].to_string() {
|
||||
if target_list.entries[i].exists() {
|
||||
return Err(format!("ERROR: Target '{}' already exists", target_list.entries[i].to_string()).to_string());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
for i in 0..src_len {
|
||||
|
|
@ -151,8 +178,7 @@ impl DirList {
|
|||
fn run_basic_checks(&self, target_list: &DirList) -> Result<(), String> {
|
||||
// Make sure there are an equal number of sources and destinations
|
||||
if self.entries.len() != target_list.entries.len() {
|
||||
return Err("ERROR: Source and target list don't have the same number of
|
||||
lines.".to_string());
|
||||
return Err("ERROR: Source and target list don't have the same number of lines.".to_string());
|
||||
}
|
||||
|
||||
// Make sure destination names are not empty
|
||||
|
|
|
|||
28
src/main.rs
28
src/main.rs
|
|
@ -39,9 +39,8 @@ struct Cli {
|
|||
#[arg(short, long)]
|
||||
editor: Option<String>,
|
||||
|
||||
// /// Apply substitution pattern(s) instead of using an editor
|
||||
// #[arg(short, long)]
|
||||
// pattern: Option<Vec<String>>,
|
||||
#[arg(required = false)]
|
||||
files: Vec<std::path::PathBuf>,
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -85,14 +84,31 @@ fn edit_temp_file(tmpfile: &String, editor_cli: &Option<String>) {
|
|||
}
|
||||
|
||||
fn main() {
|
||||
let args = Cli::parse();
|
||||
let mut args = Cli::parse();
|
||||
|
||||
// Place lockfile to not have multiple mvw processes running in the same dir at the same time
|
||||
lock_dir();
|
||||
let _cleanup = Cleanup; // Cleanup LOCK_FILE on panic
|
||||
|
||||
// read directory contents into vector of Strings
|
||||
let mut source_list = DirList::from_current_dir();
|
||||
// Do some pre-checks if files were entered on the commandline
|
||||
// Prevent duplicates
|
||||
args.files.sort();
|
||||
args.files.dedup();
|
||||
if ! args.files.is_empty() {
|
||||
for file in &args.files {
|
||||
// Check if remaining files exist
|
||||
if ! file.exists() {
|
||||
println!("File {:?} does not exist, exiting", file);
|
||||
process::exit(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let mut source_list = if ! args.files.is_empty() {
|
||||
DirList::from_list(&args.files)
|
||||
} else {
|
||||
DirList::from_current_dir()
|
||||
};
|
||||
|
||||
// create named tempfile and fill with paths
|
||||
let temp_file = create_temp_file();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue