From de0b12a98c12acb5e27b8771aeaad614adf09c8f Mon Sep 17 00:00:00 2001 From: Marcel Schneider Date: Wed, 26 Feb 2020 18:48:18 +0100 Subject: [PATCH] add adding of lines --- Cargo.lock | 35 +++++++++++++++++++++++++++++++++++ Cargo.toml | 2 +- src/app.rs | 6 ++++++ src/gitignore.rs | 28 ++++++++++++++++++++++++++++ src/helpers.rs | 1 + src/main.rs | 1 + 6 files changed, 72 insertions(+), 1 deletion(-) create mode 100644 src/gitignore.rs diff --git a/Cargo.lock b/Cargo.lock index 871a5ab..8982e91 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -26,12 +26,40 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f8aac770f1885fd7e387acedd76065302551364496e46b3dd00860b2f8359b9d" +[[package]] +name = "backtrace" +version = "0.3.44" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e4036b9bf40f3cf16aba72a3d65e8a520fc4bafcdc7079aea8f848c58c5b5536" +dependencies = [ + "backtrace-sys", + "cfg-if", + "libc", + "rustc-demangle", +] + +[[package]] +name = "backtrace-sys" +version = "0.1.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5d6575f128516de27e3ce99689419835fce9643a9b215a14d2b5b685be018491" +dependencies = [ + "cc", + "libc", +] + [[package]] name = "bitflags" version = "1.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693" +[[package]] +name = "cc" +version = "1.0.50" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "95e28fa049fda1c330bcf9d723be7663a899c4679724b34c81e9f5a326aab8cd" + [[package]] name = "cfg-if" version = "0.1.10" @@ -70,6 +98,7 @@ version = "0.12.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d371106cc88ffdfb1eabd7111e432da544f16f3e2d7bf1dfe8bf575f1df045cd" dependencies = [ + "backtrace", "version_check", ] @@ -204,6 +233,12 @@ dependencies = [ "redox_syscall", ] +[[package]] +name = "rustc-demangle" +version = "0.1.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4c691c0e608126e00913e33f0ccf3727d5fc84573623b8d65b2df340b5201783" + [[package]] name = "rustversion" version = "1.0.2" diff --git a/Cargo.toml b/Cargo.toml index b169b5b..623eb79 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,7 +14,7 @@ libc = "0.2" [dependencies.error-chain] version = "0.12" -default-features = false # disable pulling in backtrace +#default-features = false # disable pulling in backtrace [profile.release] lto = true diff --git a/src/app.rs b/src/app.rs index e062221..ce5c9c2 100644 --- a/src/app.rs +++ b/src/app.rs @@ -11,6 +11,7 @@ use log::{debug, error, info, trace, warn}; // Local Imports use crate::errors::*; +use crate::gitignore::Gitignore; use crate::helpers::{git_dir, BoilerplateOpts, HELP_TEMPLATE}; /// The verbosity level when no `-q` or `-v` arguments are given, with `0` being `-q` @@ -78,6 +79,11 @@ fn run_add(glob: &str) -> Result<()> { }; info!("Working with git root in {:?}", root); + let mut file_path = PathBuf::from(&root); + file_path.push(".gitignore"); + let gitig = Gitignore::from_path(&file_path); + gitig.add_line(glob)?; + Ok(()) } diff --git a/src/gitignore.rs b/src/gitignore.rs new file mode 100644 index 0000000..6e3d8ec --- /dev/null +++ b/src/gitignore.rs @@ -0,0 +1,28 @@ +use crate::errors::*; +use std::fs::OpenOptions; +use std::path::PathBuf; + +/// An abstraction of the gitignore file +#[derive(Debug, Default)] +pub struct Gitignore { + /// The path to the gitignore + path: PathBuf, +} +impl Gitignore { + /// Create a new Gitignore struct from a git root folder path + /// + /// The given `path` must be a valid path to an existing git root folder + pub fn from_path(path: &PathBuf) -> Self { Gitignore { path: path.clone() } } + + /// Append a line to the file + pub fn add_line(&self, line: &str) -> Result<()> { + use std::io::prelude::*; + let mut file = OpenOptions::new() + .write(true) + .append(true) + .open(&self.path) + .chain_err(|| "Error while opening gitignore file")?; + writeln!(file, "{}", line).chain_err(|| "Error while writing line to gitignore")?; + Ok(()) + } +} diff --git a/src/helpers.rs b/src/helpers.rs index a255611..56765f7 100644 --- a/src/helpers.rs +++ b/src/helpers.rs @@ -6,6 +6,7 @@ // `#![...]` it onto the entire module. #![allow(clippy::result_unwrap_used)] use log::{info, trace}; +use std::fs::File; use crate::errors::*; use std::fs::DirEntry; diff --git a/src/main.rs b/src/main.rs index e36c81b..d80610d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -36,6 +36,7 @@ use structopt::{clap, StructOpt}; // Local imports mod app; +mod gitignore; mod helpers; /// Boilerplate to parse command-line arguments, set up logging, and handle bubbled-up `Error`s.