Browse Source

Add option to `get` to append content

master
Schneider 5 years ago
parent
commit
069341dd4c
  1. 2
      Cargo.lock
  2. 2
      Cargo.toml
  3. 14
      src/app.rs
  4. 18
      src/template.rs

2
Cargo.lock

@ -325,7 +325,7 @@ dependencies = [
[[package]] [[package]]
name = "gitig" name = "gitig"
version = "0.1.1"
version = "0.1.2"
dependencies = [ dependencies = [
"directories", "directories",
"error-chain", "error-chain",

2
Cargo.toml

@ -1,6 +1,6 @@
[package] [package]
name = "gitig" name = "gitig"
version = "0.1.1"
version = "0.1.2"
authors = ["Marcel Schneider <marcel@webschneider.org>"] authors = ["Marcel Schneider <marcel@webschneider.org>"]
edition = "2018" edition = "2018"

14
src/app.rs

@ -69,6 +69,9 @@ pub enum Command {
}, },
/// Download a gitignore for a language /// Download a gitignore for a language
Get { Get {
/// Append template to an existing .gitignore file
#[structopt(short)]
append: bool,
/// The language for which the gitignore should be downloaded /// The language for which the gitignore should be downloaded
/// ///
/// A list with all available languages and projects can be printed with `list-templates`. /// A list with all available languages and projects can be printed with `list-templates`.
@ -77,7 +80,10 @@ pub enum Command {
/// List all available templates that can be downloaded /// List all available templates that can be downloaded
ListTemplates, ListTemplates,
/// Write a completion definition for the specified shell to stdout (bash, zsh, etc.) /// Write a completion definition for the specified shell to stdout (bash, zsh, etc.)
DumpCompletions { shell: Option<structopt::clap::Shell> },
DumpCompletions {
/// Shell to generate completion for
shell: Option<structopt::clap::Shell>,
},
} }
/// Runs the command `add` /// Runs the command `add`
@ -99,7 +105,7 @@ fn run_add(glob: &str) -> Result<()> {
} }
/// Runs the command `get` /// 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); trace!("Run command `get` with lang {}", &lang);
let mut root = match git_dir()? { let mut root = match git_dir()? {
Some(r) => r, Some(r) => r,
@ -121,7 +127,7 @@ fn run_get(lang: &str) -> Result<()> {
}; };
root.push(".gitignore"); root.push(".gitignore");
tmpl.write_to(&root)?;
tmpl.write_to(&root, append)?;
trace!("Wrote template to file"); trace!("Wrote template to file");
Ok(()) Ok(())
@ -153,7 +159,7 @@ fn run_dump_completion(shell: Option<structopt::clap::Shell>) -> Result<()> {
pub fn main(opts: CliOpts) -> Result<()> { pub fn main(opts: CliOpts) -> Result<()> {
match opts.cmd { match opts.cmd {
Command::Add { glob } => run_add(&glob)?, 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::ListTemplates => run_list_templates()?,
Command::DumpCompletions { shell } => run_dump_completion(shell)?, Command::DumpCompletions { shell } => run_dump_completion(shell)?,
}; };

18
src/template.rs

@ -4,7 +4,7 @@ use log::{debug, trace};
use reqwest::blocking::Client; use reqwest::blocking::Client;
use reqwest::header::{ACCEPT, CONTENT_TYPE, USER_AGENT}; use reqwest::header::{ACCEPT, CONTENT_TYPE, USER_AGENT};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::fs::File;
use std::fs::OpenOptions;
use std::io::{BufWriter, Write}; use std::io::{BufWriter, Write};
use std::path::PathBuf; use std::path::PathBuf;
@ -102,12 +102,22 @@ impl Template {
/// `load_content` has not yet been called. /// `load_content` has not yet been called.
/// Other reasons for `Err` can be io related errors. /// Other reasons for `Err` can be io related errors.
#[allow(clippy::option_expect_used)] #[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() { if self.content.is_none() {
return Err(ErrorKind::TemplateNoContent.into()); 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); let mut writer = BufWriter::new(file);
writer.write_all(self.content.as_ref().expect("checked before to be some").as_bytes())?; writer.write_all(self.content.as_ref().expect("checked before to be some").as_bytes())?;

Loading…
Cancel
Save