Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce imosid force option for overwriting files #4

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,14 @@ pub fn build_app() -> Command {
.help("file or directory to apply")
.required(true)
.value_parser(value_parser!(PathBuf)),
)
.arg(
Arg::new("force")
.short('f')
.long("force")
.about("force apply even if there are conflicts")
.required(false)
.takes_value(false),
),
)
.subcommand(
Expand Down
29 changes: 24 additions & 5 deletions src/files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -394,9 +394,28 @@ impl DotFile {
didsomething
}

pub fn write_to_file(&mut self) {
pub fn write_to_file(&mut self, force: bool) {
let targetname = &expand_tilde(&self.filename);
let newfile = File::create(targetname);
if force {
match newfile {
Err(_) => {
println!("error: could not write to file {}", &self.filename);
panic!("write_to_file");
}
Ok(mut file) => match &mut self.metafile {
None => {
file.write_all(self.to_string().as_bytes()).unwrap();
}
Some(metafile) => {
file.write_all(metafile.content.as_bytes()).unwrap();
metafile.write_to_file();
}
},
}
} else {
// existing code
}
match newfile {
Err(_) => {
println!("error: could not write to file {}", &self.filename);
Expand Down Expand Up @@ -575,8 +594,8 @@ impl DotFile {
// return true if file will be modified
// applies other file to self
// TODO: return result
pub fn applyfile(&mut self, inputfile: &DotFile) -> bool {
if !self.can_apply(inputfile) {
pub fn applyfile(&mut self, inputfile: &DotFile, force: bool) -> bool {
if force || !self.can_apply(inputfile) {
return false;
}
match &mut self.metafile {
Expand Down Expand Up @@ -630,7 +649,7 @@ impl DotFile {

// apply entire content if file is managed by metafile
Some(metafile) => {
if !metafile.modified {
if !force && !metafile.modified {
if let Some(applymetafile) = &inputfile.metafile {
if applymetafile.modified {
println!("source file {} modified", &applymetafile.parentfile);
Expand All @@ -650,7 +669,7 @@ impl DotFile {
);
return true;
}
} else {
} else if !force {
println!(
"{}",
format!("target {} modified, skipping", &self.filename.bold()).yellow()
Expand Down