Skip to content

Commit

Permalink
feat(repoflavor): allows specifying the flavor of link to generate
Browse files Browse the repository at this point in the history
Allows one to use --repoflavor [github | stash] to specify the style
of link to generate for commits.
Also allows using 'repo-flavor = "github"' inside the .clog.toml file.
This necessitates the inclusion of the convert feature because of the
use of to_ascii_lowercase().as_str().
  • Loading branch information
orclev committed May 21, 2015
1 parent 1fc9654 commit b3dd576
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 5 deletions.
45 changes: 45 additions & 0 deletions src/clogconfig.rs
Expand Up @@ -4,6 +4,8 @@ use std::path::Path;
use std::fmt::Display;
use std::env;
use std::collections::HashMap;
use std::str::FromStr;
use std::ascii::AsciiExt;

use clap::ArgMatches;
use toml::{Value, Parser};
Expand All @@ -12,10 +14,32 @@ use semver;
use git;
use CLOG_CONFIG_FILE;

pub enum RepoFlavor {
Github,
Stash
}

pub struct RepoFlavorError {
pub unknown_type: String
}

impl FromStr for RepoFlavor {
type Err = RepoFlavorError;

fn from_str(s: &str) -> Result<RepoFlavor, RepoFlavorError> {
return match s.to_ascii_lowercase().as_str() {
"github" => Ok(RepoFlavor::Github),
"stash" => Ok(RepoFlavor::Stash),
val => Err(RepoFlavorError{unknown_type: val.to_owned()})
}
}
}

pub struct ClogConfig {
pub grep: String,
pub format: String,
pub repo: String,
pub repo_flavor: RepoFlavor,
pub version: String,
pub subtitle: String,
pub from: String,
Expand Down Expand Up @@ -80,6 +104,7 @@ impl ClogConfig {
let mut toml_from_latest = None;
let mut toml_repo = None;
let mut toml_subtitle = None;
let mut toml_repo_flavor = None;

let mut outfile = None;

Expand Down Expand Up @@ -117,6 +142,15 @@ impl ClogConfig {
Some(val) => Some(val.as_str().unwrap_or("").to_owned()),
None => Some("".to_owned())
};
toml_repo_flavor = match clog_table.lookup("repo-flavor") {
Some(val) => match val.as_str().unwrap_or("github").parse::<RepoFlavor>() {
Ok(flavor) => Some(flavor),
Err(err) => {
return Err(Box::new(format!("Error parsing file {}\n\nCould not parse value of repo-flavor, {} is not a valid repository flavor", CLOG_CONFIG_FILE, err.unknown_type)))
}
},
None => Some(RepoFlavor::Github)
};
outfile = match clog_table.lookup("outfile") {
Some(val) => Some(val.as_str().unwrap_or("changelog.md").to_owned()),
None => None
Expand Down Expand Up @@ -152,6 +186,16 @@ impl ClogConfig {
None => toml_repo.unwrap_or("".to_owned())
};

let repo_flavor = match matches.value_of("repoflavor") {
Some(flavor) => match flavor.parse::<RepoFlavor>() {
Ok(val) => val,
Err(err) => {
return Err(Box::new(format!("{} is not a valid option for repoflavor, please specify 'github' or 'stash'", err.unknown_type)));
}
},
None => toml_repo_flavor.unwrap_or(RepoFlavor::Github)
};

let subtitle = match matches.value_of("subtitle") {
Some(title) => title.to_owned(),
None => toml_subtitle.unwrap_or("".to_owned())
Expand All @@ -172,6 +216,7 @@ impl ClogConfig {
})),
format: "%H%n%s%n%b%n==END==".to_owned(),
repo: repo,
repo_flavor: repo_flavor,
version: version,
subtitle: subtitle,
from: from,
Expand Down
16 changes: 11 additions & 5 deletions src/log_writer.rs
Expand Up @@ -5,6 +5,7 @@ use time;

use logentry::LogEntry;
use clogconfig::ClogConfig;
use clogconfig::RepoFlavor;

pub struct LogWriter<'a, 'cc> {
writer: &'a mut (Write + 'a),
Expand All @@ -16,15 +17,20 @@ impl<'a, 'cc> LogWriter<'a, 'cc> {
let short_hash = &hash[0..8];
match &options.repo[..] {
"" => format!("({})", short_hash),
link => format!("[{}]({}/commit/{})", short_hash, link, hash)

link => match options.repo_flavor {
RepoFlavor::Github => format!("[{}]({}/commit/{})", short_hash, link, hash),
RepoFlavor::Stash => format!("[{}]({}/commits/{})", short_hash, link, hash)
}
}
}

fn issue_link(&self, issue: &String) -> String {
fn issue_link(&self, issue: &String, options: &ClogConfig) -> String {
match &self.options.repo[..] {
"" => format!("(#{})", issue),
link => format!("[#{}]({}/issues/{})", issue, link, issue)
link => match options.repo_flavor {
RepoFlavor::Github => format!("[#{}]({}/issues/{})", issue, link, issue),
RepoFlavor::Stash => format!("(#{})", issue) // Stash doesn't support issue links
}
}
}

Expand Down Expand Up @@ -70,7 +76,7 @@ impl<'a, 'cc> LogWriter<'a, 'cc> {

if entry.closes.len() > 0 {
let closes_string = entry.closes.iter()
.map(|s| self.issue_link(s))
.map(|s| self.issue_link(s, &self.options))
// FIXME: Connect should be
// used on the Iterator
.collect::<Vec<String>>()
Expand Down
2 changes: 2 additions & 0 deletions src/main.rs
@@ -1,4 +1,5 @@
#![crate_name = "clog"]
#![feature(convert)]

#![cfg_attr(feature = "unstable", feature(plugin))]
#![cfg_attr(feature = "unstable", plugin(regex_macros))]
Expand Down Expand Up @@ -40,6 +41,7 @@ fn main () {
.about("a conventional changelog for the rest of us")
.args_from_usage("-r --repository=[repository] 'e.g. https://github.com/thoughtram/clog'
--from=[from] 'e.g. 12a8546'
--repoflavor=[repoflavor] 'The flavor of repository link to generate, github or stash, defaults to github'
--major 'Increment major version by one (Sets minor and patch to 0)'
--minor 'Increment minor version by one (Sets patch to 0)'
--patch 'Increment patch version by one'
Expand Down

0 comments on commit b3dd576

Please sign in to comment.