diff --git a/Cargo.lock b/Cargo.lock index 3705eb3..1b8ac32 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -325,7 +325,7 @@ dependencies = [ [[package]] name = "gitig" -version = "0.1.1" +version = "0.1.2" dependencies = [ "directories", "error-chain", diff --git a/Cargo.toml b/Cargo.toml index ee3bd05..4b20fcd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "gitig" -version = "0.1.1" +version = "0.1.2" authors = ["Marcel Schneider "] edition = "2018" diff --git a/src/app.rs b/src/app.rs index 67ff2fc..541e928 100644 --- a/src/app.rs +++ b/src/app.rs @@ -69,15 +69,21 @@ pub enum Command { }, /// Download a gitignore for a language Get { + /// Append template to an existing .gitignore file + #[structopt(short)] + append: bool, /// The language for which the gitignore should be downloaded /// /// A list with all available languages and projects can be printed with `list-templates`. - lang: String, + lang: String, }, /// List all available templates that can be downloaded ListTemplates, /// Write a completion definition for the specified shell to stdout (bash, zsh, etc.) - DumpCompletions { shell: Option }, + DumpCompletions { + /// Shell to generate completion for + shell: Option, + }, } /// Runs the command `add` @@ -99,7 +105,7 @@ fn run_add(glob: &str) -> Result<()> { } /// Runs the command `get` -fn run_get(lang: &str) -> Result<()> { +fn run_get(lang: &str, append: bool) -> Result<()> { trace!("Run command `get` with lang {}", &lang); let mut root = match git_dir()? { Some(r) => r, @@ -121,7 +127,7 @@ fn run_get(lang: &str) -> Result<()> { }; root.push(".gitignore"); - tmpl.write_to(&root)?; + tmpl.write_to(&root, append)?; trace!("Wrote template to file"); Ok(()) @@ -153,7 +159,7 @@ fn run_dump_completion(shell: Option) -> Result<()> { pub fn main(opts: CliOpts) -> Result<()> { match opts.cmd { Command::Add { glob } => run_add(&glob)?, - Command::Get { lang } => run_get(&lang)?, + Command::Get { lang, append } => run_get(&lang, append)?, Command::ListTemplates => run_list_templates()?, Command::DumpCompletions { shell } => run_dump_completion(shell)?, }; diff --git a/src/template.rs b/src/template.rs index 94eba71..b40f540 100644 --- a/src/template.rs +++ b/src/template.rs @@ -4,7 +4,7 @@ use log::{debug, trace}; use reqwest::blocking::Client; use reqwest::header::{ACCEPT, CONTENT_TYPE, USER_AGENT}; use serde::{Deserialize, Serialize}; -use std::fs::File; +use std::fs::OpenOptions; use std::io::{BufWriter, Write}; use std::path::PathBuf; @@ -102,12 +102,22 @@ impl Template { /// `load_content` has not yet been called. /// Other reasons for `Err` can be io related errors. #[allow(clippy::option_expect_used)] - pub fn write_to(&self, path: &PathBuf) -> Result<()> { - debug!("Writing contents of {} to {}", self.pretty_name(), path.to_string_lossy()); + pub fn write_to(&self, path: &PathBuf, append: bool) -> Result<()> { + debug!( + "Writing contents of {} to {} (append: {})", + self.pretty_name(), + path.to_string_lossy(), + append + ); if self.content.is_none() { return Err(ErrorKind::TemplateNoContent.into()); } - let file = File::create(path)?; + let file = OpenOptions::new() + .write(true) + .append(append) + .create(true) + .open(path) + .chain_err(|| "Error while opening gitignore file to write template")?; let mut writer = BufWriter::new(file); writer.write_all(self.content.as_ref().expect("checked before to be some").as_bytes())?;