Browse Source

add adding of lines

master
Schneider 5 years ago
parent
commit
de0b12a98c
Signed by: schneider GPG Key ID: 3F50B02A50039F3B
  1. 35
      Cargo.lock
  2. 2
      Cargo.toml
  3. 6
      src/app.rs
  4. 28
      src/gitignore.rs
  5. 1
      src/helpers.rs
  6. 1
      src/main.rs

35
Cargo.lock

@ -26,12 +26,40 @@ version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f8aac770f1885fd7e387acedd76065302551364496e46b3dd00860b2f8359b9d" 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]] [[package]]
name = "bitflags" name = "bitflags"
version = "1.2.1" version = "1.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693" checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693"
[[package]]
name = "cc"
version = "1.0.50"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "95e28fa049fda1c330bcf9d723be7663a899c4679724b34c81e9f5a326aab8cd"
[[package]] [[package]]
name = "cfg-if" name = "cfg-if"
version = "0.1.10" version = "0.1.10"
@ -70,6 +98,7 @@ version = "0.12.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d371106cc88ffdfb1eabd7111e432da544f16f3e2d7bf1dfe8bf575f1df045cd" checksum = "d371106cc88ffdfb1eabd7111e432da544f16f3e2d7bf1dfe8bf575f1df045cd"
dependencies = [ dependencies = [
"backtrace",
"version_check", "version_check",
] ]
@ -204,6 +233,12 @@ dependencies = [
"redox_syscall", "redox_syscall",
] ]
[[package]]
name = "rustc-demangle"
version = "0.1.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4c691c0e608126e00913e33f0ccf3727d5fc84573623b8d65b2df340b5201783"
[[package]] [[package]]
name = "rustversion" name = "rustversion"
version = "1.0.2" version = "1.0.2"

2
Cargo.toml

@ -14,7 +14,7 @@ libc = "0.2"
[dependencies.error-chain] [dependencies.error-chain]
version = "0.12" version = "0.12"
default-features = false # disable pulling in backtrace
#default-features = false # disable pulling in backtrace
[profile.release] [profile.release]
lto = true lto = true

6
src/app.rs

@ -11,6 +11,7 @@ use log::{debug, error, info, trace, warn};
// Local Imports // Local Imports
use crate::errors::*; use crate::errors::*;
use crate::gitignore::Gitignore;
use crate::helpers::{git_dir, BoilerplateOpts, HELP_TEMPLATE}; use crate::helpers::{git_dir, BoilerplateOpts, HELP_TEMPLATE};
/// The verbosity level when no `-q` or `-v` arguments are given, with `0` being `-q` /// 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); 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(()) Ok(())
} }

28
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(())
}
}

1
src/helpers.rs

@ -6,6 +6,7 @@
// `#![...]` it onto the entire module. // `#![...]` it onto the entire module.
#![allow(clippy::result_unwrap_used)] #![allow(clippy::result_unwrap_used)]
use log::{info, trace}; use log::{info, trace};
use std::fs::File;
use crate::errors::*; use crate::errors::*;
use std::fs::DirEntry; use std::fs::DirEntry;

1
src/main.rs

@ -36,6 +36,7 @@ use structopt::{clap, StructOpt};
// Local imports // Local imports
mod app; mod app;
mod gitignore;
mod helpers; mod helpers;
/// Boilerplate to parse command-line arguments, set up logging, and handle bubbled-up `Error`s. /// Boilerplate to parse command-line arguments, set up logging, and handle bubbled-up `Error`s.

Loading…
Cancel
Save