Changeset 5bd9641 in gignore


Ignore:
Timestamp:
19 Feb 2026, 22:16:46 (2 months ago)
Author:
Asher Mullaney <asher.mullaney@…>
Branches:
file_locations, help, logging, man, multi_arg, trunk
Children:
eb42226
Parents:
d5c0cff
Message:

Updated file locations. Also added method.

Files:
1 added
3 edited

Legend:

Unmodified
Added
Removed
  • Cargo.toml

    rd5c0cff r5bd9641  
    66[dependencies]
    77clap = { version = "4.5.53", features = ["derive"] }
    8 home = "0.5.12"
    98anyhow = "1.0.100"
    109serde = { version = "1.0.228", features = ["derive"] }
  • src/main.rs

    rd5c0cff r5bd9641  
    184184            open_truncate(".gitignore".as_ref()).context("cannot open `.gitignore` in truncate mode")?.write_all(b"").context("cannot write to `.gitignore`")?;
    185185
    186             // Re-add lines to file, without [PATH].
     186            // Re-add lines to file, without PATH.
    187187            for line in lines {
    188188                open_append(".gitignore".as_ref()).context("cannot open `.gitignore` in append mode")?
     
    219219            }
    220220        } => {
    221             template::add(&template::Template::new(source_file.as_ref(), name, *append)).context("cannot add template")?;
     221            let x = template::Template::new(source_file.as_ref(), name, *append);
     222
     223            template::add(x).context("cannot add template")?;
    222224
    223225            Ok(())
  • src/template.rs

    rd5c0cff r5bd9641  
    44        PathBuf,
    55    },
    6     io::Write,
     6    io::{self, Write},
    77    fs,
    88    os::unix::fs::MetadataExt,
     9    process,
    910};
    1011
     
    4142        &self.source_file
    4243    }
     44    fn change_source_file<T: AsRef<Path>>(&mut self, new: T) {
     45        self.source_file = new.as_ref().to_owned();
     46    }
    4347    pub(crate) fn get_name(&self) -> &str {
    4448        &self.name
     
    117121}
    118122
    119 pub(crate) fn add(template: &Template) -> Result<()> {
     123pub(crate) fn add(mut template: Template) -> Result<()> {
    120124    if !fs::exists(template.get_source_file()).context(format!("cannot search for `{}`", template.get_name()))? {
    121125        return Err(Error::msg(format!("The source file specified, `{}`, does not exist.", template.get_source_file().display())));
    122126    }
    123127
     128    if (&*(template.get_source_file().to_string_lossy())).split_terminator('.').nth(2) != Some("ggnr") {
     129        eprintln!("WARNING: specified source file, `{}`, does not have the file extension for gignore templates (`.ggnr`). Continue anyway? [y/N]", template.get_source_file().display());
     130
     131        let mut buf = String::new();
     132        io::stdin()
     133            .read_line(&mut buf)
     134            .context("cannot read from stdin")?;
     135
     136        match &*buf.to_lowercase().trim() {
     137            "y" => { eprintln!("Continuing..."); },
     138            _ => {
     139                eprintln!("User denied operation. Exiting.");
     140                process::exit(0);
     141            },
     142        }
     143    };
     144
    124145    let new_template_path = format!("/usr/lib/gignore/{}", template.get_source_file().display());
     146
     147    if fs::exists(&new_template_path).context(format!("cannot search for `{new_template_path}`"))? {
     148        eprintln!("WARNING: the file `{new_template_path}` already exists. Overwrite it? [y/N]");
     149       
     150        let mut buf = String::new();
     151        io::stdin()
     152            .read_line(&mut buf)
     153            .context("cannot read from stdin")?;
     154
     155        match &*buf.to_lowercase().trim() {
     156            "y" => {
     157                eprintln!("Continuing...");
     158
     159                fs::remove(&new_template_path).
     160            },
     161            _ => {
     162                eprintln!("User denied operation. Exiting.");
     163                process::exit(0);
     164            },
     165        }
     166    };
    125167
    126168    drop(
     
    142184    }
    143185
    144     fs::remove_file(template.get_source_file()).context(format!("cannot remove `{}`", template.get_source_file().display()))?;
    145 
    146186    if find_name(template.get_name()).context("cannot search for name in template library")?.is_some() {
    147187        return Err(Error::msg(format!("A template with the name `{}` already exists!", template.get_name())));
    148188    }
     189    template.change_source_file(new_template_path);
     190
    149191    template.save().context("cannot save template")?;
    150192
Note: See TracChangeset for help on using the changeset viewer.