Skip to content

Commit

Permalink
refactor(changelog): remove unused writter mode: Append & Replace
Browse files Browse the repository at this point in the history
  • Loading branch information
oknozor committed Oct 24, 2020
1 parent 72a6925 commit 098d6c0
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 35 deletions.
4 changes: 2 additions & 2 deletions src/bin/cog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ use cocogitto::conventional::commit::CommitType;
use cocogitto::conventional::version::VersionIncrement;
use cocogitto::git::hook::HookKind;
use cocogitto::log::filter::{CommitFilter, CommitFilters};
use cocogitto::log::output::Output;
use cocogitto::CocoGitto;
use cocogitto::{conventional::changelog::WriterMode, log::output::Output};
use std::process::exit;

const APP_SETTINGS: &[AppSettings] = &[
Expand Down Expand Up @@ -60,7 +60,7 @@ fn main() -> Result<()> {
let pre = subcommand.value_of("pre");

// TODO mode to cli
cocogitto.create_version(increment, WriterMode::Prepend, pre)?
cocogitto.create_version(increment, pre)?
}
VERIFY => {
let subcommand = matches.subcommand_matches(VERIFY).unwrap();
Expand Down
32 changes: 2 additions & 30 deletions src/conventional/changelog.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
use self::WriterMode::*;
use crate::conventional::commit::Commit;
use crate::{OidOf, COMMITS_METADATA};
use anyhow::Result;
use itertools::Itertools;
use std::fs;
use std::path::PathBuf;

pub enum WriterMode {
Replace,
Prepend,
Append,
}

pub(crate) struct Changelog {
pub from: OidOf,
pub to: OidOf,
Expand All @@ -23,27 +16,14 @@ pub(crate) struct Changelog {
pub(crate) struct ChangelogWriter {
pub(crate) changelog: Changelog,
pub(crate) path: PathBuf,
pub(crate) mode: WriterMode,
}

impl ChangelogWriter {
pub(crate) fn write(&mut self) -> Result<()> {
match &self.mode {
Append => self.insert(),
Prepend => self.insert(),
Replace => self.replace(),
}
}

fn insert(&mut self) -> Result<()> {
pub fn write(&mut self) -> Result<()> {
let mut changelog_content =
fs::read_to_string(&self.path).unwrap_or_else(|_err| Changelog::changelog_template());

let separator_idx = match self.mode {
Append => changelog_content.rfind("- - -"),
Prepend => changelog_content.find("- - -"),
_ => unreachable!(),
};
let separator_idx = changelog_content.find("- - -");

if let Some(idx) = separator_idx {
let markdown_changelog = self.changelog.markdown(false);
Expand All @@ -59,14 +39,6 @@ impl ChangelogWriter {
))
}
}

fn replace(&mut self) -> Result<()> {
let mut content = Changelog::default_header();
content.push_str(&self.changelog.markdown(false));
content.push_str(Changelog::default_footer().as_str());

fs::write(&self.path, content).map_err(|err| anyhow!(err))
}
}

impl Changelog {
Expand Down
4 changes: 1 addition & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use crate::settings::{HookType, Settings};
use anyhow::{Context, Result};
use chrono::Utc;
use colored::*;
use conventional::changelog::{Changelog, ChangelogWriter, WriterMode};
use conventional::changelog::{Changelog, ChangelogWriter};
use conventional::commit::Commit;
use conventional::commit::{CommitConfig, CommitMessage, CommitType};
use conventional::version::{parse_pre_release, VersionIncrement};
Expand Down Expand Up @@ -331,7 +331,6 @@ impl CocoGitto {
pub fn create_version(
&mut self,
increment: VersionIncrement,
mode: WriterMode,
pre_release: Option<&str>,
) -> Result<()> {
let statuses = self.repository.get_statuses()?;
Expand Down Expand Up @@ -387,7 +386,6 @@ impl CocoGitto {
let mut writter = ChangelogWriter {
changelog,
path: self.changelog_path.clone(),
mode,
};

writter
Expand Down

0 comments on commit 098d6c0

Please sign in to comment.