implement filenames on commandline
This commit is contained in:
parent
12da2e2970
commit
708c57e726
3 changed files with 57 additions and 15 deletions
4
Cargo.lock
generated
4
Cargo.lock
generated
|
|
@ -1,6 +1,6 @@
|
||||||
# This file is automatically @generated by Cargo.
|
# This file is automatically @generated by Cargo.
|
||||||
# It is not intended for manual editing.
|
# It is not intended for manual editing.
|
||||||
version = 3
|
version = 4
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "anstream"
|
name = "anstream"
|
||||||
|
|
@ -206,7 +206,7 @@ checksum = "78b3ae25bc7c8c38cec158d1f2757ee79e9b3740fbc7ccf0e59e4b08d793fa89"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "mvw"
|
name = "mvw"
|
||||||
version = "0.1.5"
|
version = "0.1.6"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"clap",
|
"clap",
|
||||||
"dialoguer",
|
"dialoguer",
|
||||||
|
|
|
||||||
|
|
@ -12,11 +12,26 @@ enum DirListEntry {
|
||||||
Text(String)
|
Text(String)
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ToString for DirListEntry {
|
impl DirListEntry {
|
||||||
fn to_string(&self) -> String {
|
fn to_string(&self) -> String {
|
||||||
match self {
|
match self {
|
||||||
DirListEntry::Text(line) => line.clone(),
|
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 {
|
Self {
|
||||||
safe_source: false,
|
safe_source: false,
|
||||||
noop: false,
|
noop: false,
|
||||||
verbose: 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();
|
let mut intermediate_files: HashMap<String, String> = HashMap::new();
|
||||||
|
|
||||||
self.run_basic_checks(target_list)?;
|
self.run_basic_checks(target_list)?;
|
||||||
|
|
||||||
if ! self.safe_source {
|
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 {
|
for i in 0..src_len {
|
||||||
|
|
@ -151,8 +178,7 @@ impl DirList {
|
||||||
fn run_basic_checks(&self, target_list: &DirList) -> Result<(), String> {
|
fn run_basic_checks(&self, target_list: &DirList) -> Result<(), String> {
|
||||||
// Make sure there are an equal number of sources and destinations
|
// Make sure there are an equal number of sources and destinations
|
||||||
if self.entries.len() != target_list.entries.len() {
|
if self.entries.len() != target_list.entries.len() {
|
||||||
return Err("ERROR: Source and target list don't have the same number of
|
return Err("ERROR: Source and target list don't have the same number of lines.".to_string());
|
||||||
lines.".to_string());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Make sure destination names are not empty
|
// Make sure destination names are not empty
|
||||||
|
|
|
||||||
28
src/main.rs
28
src/main.rs
|
|
@ -39,9 +39,8 @@ struct Cli {
|
||||||
#[arg(short, long)]
|
#[arg(short, long)]
|
||||||
editor: Option<String>,
|
editor: Option<String>,
|
||||||
|
|
||||||
// /// Apply substitution pattern(s) instead of using an editor
|
#[arg(required = false)]
|
||||||
// #[arg(short, long)]
|
files: Vec<std::path::PathBuf>,
|
||||||
// pattern: Option<Vec<String>>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -85,14 +84,31 @@ fn edit_temp_file(tmpfile: &String, editor_cli: &Option<String>) {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
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
|
// Place lockfile to not have multiple mvw processes running in the same dir at the same time
|
||||||
lock_dir();
|
lock_dir();
|
||||||
let _cleanup = Cleanup; // Cleanup LOCK_FILE on panic
|
let _cleanup = Cleanup; // Cleanup LOCK_FILE on panic
|
||||||
|
|
||||||
// read directory contents into vector of Strings
|
// Do some pre-checks if files were entered on the commandline
|
||||||
let mut source_list = DirList::from_current_dir();
|
// 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
|
// create named tempfile and fill with paths
|
||||||
let temp_file = create_temp_file();
|
let temp_file = create_temp_file();
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue