Skip to content

Commit

Permalink
Refactor the runtime settings into a struct (#1)
Browse files Browse the repository at this point in the history
This makes more sense than having a lot of parameters. Making this change makes it possible to do a lot of other nice changes, will probably continue doing one of these as soon as I get this reviewed. :)
  • Loading branch information
perlun committed Nov 24, 2016
1 parent 66ff4f5 commit 27ee48d
Showing 1 changed file with 39 additions and 26 deletions.
65 changes: 39 additions & 26 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,39 @@ use std::env;
use std::process::Command;
use std::process::exit;

pub fn get_commits(dir: &str, from_revision: &str, to_revision: &str) {
let range = format!("{}..{}", from_revision, to_revision);

let output = Command::new("git")
.arg("log")
.arg("--oneline")
.arg(&range)
.current_dir(dir)
.output().unwrap_or_else(|e| panic!("Failed to run 'git log' with error: {}", e));

let stdout_output = String::from_utf8_lossy(&output.stdout);
let lines = stdout_output
.split('\n')
.collect::<Vec<_>>();
let mut lines_iterator = lines.iter();

print!("## {}\n\n", to_revision);

loop {
match lines_iterator.next() {
Some(line) => {
if line.is_empty() { return; }
println!("* {}", line)
},
None => break
struct Settings<'a> {
repository_path: &'a str,
from_revision: &'a str,
to_revision: &'a str
}

impl<'a> Settings<'a> {
pub fn get_commits(self) {
let range = format!("{}..{}", self.from_revision, self.to_revision);

let output = Command::new("git")
.arg("log")
.arg("--oneline")
.arg(&range)
.current_dir(self.repository_path)
.output().unwrap_or_else(|e| panic!("Failed to run 'git log' with error: {}", e));

let stdout_output = String::from_utf8_lossy(&output.stdout);
let lines = stdout_output
.split('\n')
.collect::<Vec<_>>();
let mut lines_iterator = lines.iter();

print!("## {}\n\n", self.to_revision);

loop {
match lines_iterator.next() {
Some(line) => {
if line.is_empty() { return; }
println!("* {}", line)
},
None => break
}
}
}
}
Expand All @@ -35,7 +43,12 @@ fn main() {
let args: Vec<_> = env::args().collect();

if args.len() == 4 {
get_commits(&args[1], &args[2], &args[3]);
let settings = Settings {
repository_path: &args[1],
from_revision: &args[2],
to_revision: &args[3]
};
settings.get_commits();
}
else {
println!("Usage: {} <path> <from_revision> <to_revision>\n", args[0]);
Expand Down

0 comments on commit 27ee48d

Please sign in to comment.