Skip to content

Commit ca28c6b

Browse files
refactor: extract some code to files
1 parent b3afeed commit ca28c6b

5 files changed

Lines changed: 78 additions & 77 deletions

File tree

.travis.yml

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
language: rust
22
rust:
3-
- stable
4-
- beta
5-
- nightly
3+
- stable
4+
- beta
5+
- nightly
66
matrix:
7-
allow_failures:
8-
- rust: nightly
9-
fast_finish: true
7+
allow_failures:
8+
- rust: nightly
9+
fast_finish: true
1010
script:
11-
- cargo build --release
11+
- cargo build --release
1212
deploy:
13-
provider: releases
14-
api_key:
15-
secure: GV2nTBbjxp3L07ny/W/jpLQvVR8y/q/3BDhrjVV6NsaQxAdWC5Lk79u5UDgY2HYgeGY2wKOrQO31MJF9cmXfxd3gDS+hRkT8HXwIxbqnE6rjAnC+WcJ9cXGF500pUyhtY4OVjOWNexs3q3JwZgqjDjr1MSRZZM+iv+i+I5ug37d53+no1hBv+008/IczC0MrfluqzsR4oJVBDL9Sw9lhATae4YGaC/LFo343pqb204O5xATUg9do/wXnh0DOsrICliLAuDNncBMRwMf0VqG7oTcd9U+ZyGfHLVAPQ2kFAWTQXz6le9H+A6jZfYISiD2Q3HM57xkJ+vR8HZ9M+szF6VV3nvcDzUt93txEkmXOVRlN/r+nirr/wicPQ/SBOhaPiBCg2LZfHocIoMPpuHxq3RPwFFTTb8gldbLfS6Kt7wmgH/i2t4zQzS1qHTsJO50ru6MFwAiZ0va05LcwPKS4C/4anDH4Xd8ViqUAzRcj5uSCX1+obLV37kCMGqRmdSWdLnu8VgKqLWf3OzydcmD4gr8Q67Ak/3BMGSwmOr5l6JPi3itRz4dzFZjksYwaJj6eNWTuuEQnW0ygvGyMNWPfqcNJZM8KxJIuzWHkbgW6uwS29X+Oyq+M7MRvxWh3G6S6KLsrNk+jAWwfXCfQVdCaaAmkOXllKK86qKrh7h634Dk=
16-
file: target/release/gitweb
17-
on:
18-
repo: yoannfleurydev/gitweb
13+
provider: releases
14+
api_key:
15+
secure: GV2nTBbjxp3L07ny/W/jpLQvVR8y/q/3BDhrjVV6NsaQxAdWC5Lk79u5UDgY2HYgeGY2wKOrQO31MJF9cmXfxd3gDS+hRkT8HXwIxbqnE6rjAnC+WcJ9cXGF500pUyhtY4OVjOWNexs3q3JwZgqjDjr1MSRZZM+iv+i+I5ug37d53+no1hBv+008/IczC0MrfluqzsR4oJVBDL9Sw9lhATae4YGaC/LFo343pqb204O5xATUg9do/wXnh0DOsrICliLAuDNncBMRwMf0VqG7oTcd9U+ZyGfHLVAPQ2kFAWTQXz6le9H+A6jZfYISiD2Q3HM57xkJ+vR8HZ9M+szF6VV3nvcDzUt93txEkmXOVRlN/r+nirr/wicPQ/SBOhaPiBCg2LZfHocIoMPpuHxq3RPwFFTTb8gldbLfS6Kt7wmgH/i2t4zQzS1qHTsJO50ru6MFwAiZ0va05LcwPKS4C/4anDH4Xd8ViqUAzRcj5uSCX1+obLV37kCMGqRmdSWdLnu8VgKqLWf3OzydcmD4gr8Q67Ak/3BMGSwmOr5l6JPi3itRz4dzFZjksYwaJj6eNWTuuEQnW0ygvGyMNWPfqcNJZM8KxJIuzWHkbgW6uwS29X+Oyq+M7MRvxWh3G6S6KLsrNk+jAWwfXCfQVdCaaAmkOXllKK86qKrh7h634Dk=
16+
file: "target/release/gitweb"
17+
on:
18+
repo: yoannfleurydev/gitweb
19+
tags: true

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8+
## [Unrealeased]
9+
810
## [0.1.2] - 2019-02-17
911

1012
### Added

src/git.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
use crate::logger::Logger;
2+
use git2::{ErrorCode, Repository};
3+
4+
pub fn get_repo() -> Repository {
5+
return match Repository::open(".") {
6+
Ok(repo) => repo,
7+
Err(e) => panic!("failed to open: {}", e),
8+
};
9+
}
10+
11+
pub fn get_branch(repo: &Repository, logger: &Logger) -> String {
12+
let head = match repo.head() {
13+
Ok(head) => Some(head),
14+
Err(ref e) if e.code() == ErrorCode::UnbornBranch || e.code() == ErrorCode::NotFound => {
15+
None
16+
}
17+
Err(e) => panic!("failed to get head ref {}", e),
18+
};
19+
20+
let head = head.as_ref().and_then(|h| h.shorthand());
21+
logger.print(
22+
format!(
23+
"# On branch {}",
24+
head.unwrap_or("Not currently on any branch")
25+
)
26+
.as_str(),
27+
);
28+
29+
String::from(head.unwrap_or("master"))
30+
}

src/logger.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
pub struct Logger {
2+
verbose: bool,
3+
}
4+
5+
impl Logger {
6+
pub fn new(verbose: bool) -> Logger {
7+
Logger { verbose: verbose }
8+
}
9+
10+
pub fn print(&self, text: &str) {
11+
if self.verbose {
12+
println!("gitweb: {}", text)
13+
}
14+
}
15+
}

src/main.rs

Lines changed: 17 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@ extern crate open;
33
extern crate regex;
44
extern crate structopt;
55

6-
use git2::{ErrorCode, Repository};
76
use regex::Regex;
87
use std::env;
98
use std::process::Command;
109
use structopt::StructOpt;
1110

11+
mod git;
12+
mod logger;
13+
1214
#[derive(Debug, StructOpt)]
1315
// Rename all will use the name of the field
1416
#[structopt(rename_all = "kebab-case")]
@@ -30,82 +32,39 @@ pub struct Opt {
3032
verbose: bool,
3133
}
3234

33-
#[cfg(target_os = "linux")]
35+
/// Function to open the browser using the system shell.
3436
fn open_browser(browser: &String, url: &String) {
3537
Command::new(browser)
3638
.arg(url)
3739
.output()
3840
.expect("failed to execute process");
3941
}
4042

41-
#[cfg(target_os = "windows")]
42-
fn open_browser(browser: &String, url: &String) {
43-
Command::new(browser)
44-
.arg(url)
45-
.output()
46-
.expect("failed to execute process");
47-
}
48-
49-
fn get_repo() -> Repository {
50-
return match Repository::open(".") {
51-
Ok(repo) => repo,
52-
Err(e) => panic!("failed to open: {}", e),
53-
};
54-
}
55-
56-
fn get_branch(repo: &Repository, verbose: &bool) -> String {
57-
let head = match repo.head() {
58-
Ok(head) => Some(head),
59-
Err(ref e) if e.code() == ErrorCode::UnbornBranch || e.code() == ErrorCode::NotFound => {
60-
None
61-
}
62-
Err(e) => panic!("failed to get head ref {}", e),
63-
};
64-
65-
let head = head.as_ref().and_then(|h| h.shorthand());
66-
print_verbose(
67-
format!(
68-
"# On branch {}",
69-
head.unwrap_or("Not currently on any branch")
70-
)
71-
.as_str(),
72-
verbose,
73-
);
74-
75-
String::from(head.unwrap_or("master"))
76-
}
77-
78-
fn print_verbose(string: &str, verbose: &bool) {
79-
if *verbose {
80-
println!("{}", string)
81-
}
82-
}
43+
const BROWSER: &str = "BROWSER";
8344

8445
fn main() {
8546
// Get the command line options
8647
let opt = Opt::from_args();
48+
let logger = logger::Logger::new(opt.verbose);
8749

88-
print_verbose("Verbose is active", &opt.verbose);
50+
logger.print("Verbose is active");
8951

9052
// Check that the user is in a git repository.
91-
let repo = get_repo();
53+
let repo = git::get_repo();
9254

9355
// Get the branch to show in the browser.
9456
let branch = match opt.branch {
9557
Some(branch) => branch,
9658
None => {
97-
print_verbose("No branch given, getting current one", &opt.verbose);
59+
logger.print("No branch given, getting current one");
9860

99-
get_branch(&repo, &opt.verbose)
61+
git::get_branch(&repo, &logger)
10062
}
10163
};
10264

103-
let remote_name = &opt.remote.unwrap_or("origin".to_string());
65+
let remote_name = &opt.remote.unwrap_or("origin".to_owned());
10466

105-
print_verbose(
106-
format!("Getting remote for {}", remote_name).as_str(),
107-
&opt.verbose,
108-
);
67+
logger.print(format!("Getting remote for {}", remote_name).as_str());
10968

11069
let optional_remote = match repo.find_remote(remote_name) {
11170
Ok(remote) => remote,
@@ -136,23 +95,17 @@ fn main() {
13695
if opt.browser.is_some() {
13796
let option_browser = opt.browser.unwrap();
13897

139-
print_verbose(
140-
format!("Browser {} given as option", option_browser).as_str(),
141-
&opt.verbose,
142-
);
98+
logger.print(format!("Browser {} given as option", option_browser).as_str());
14399

144100
open_browser(&option_browser, &url);
145101
} else {
146-
match env::var("BROWSER") {
102+
match env::var(BROWSER) {
147103
// If the environment variable is available, open the web browser.
148104
Ok(browser) => open_browser(&browser, &url),
105+
// Else, open the default browser of the system.
149106
Err(e) => {
150-
print_verbose(
151-
format!("BROWSER variable not available : {}", e).as_str(),
152-
&opt.verbose,
153-
);
154-
155-
print_verbose("Opening default browser", &opt.verbose);
107+
logger.print(format!("{} variable not available : {}", BROWSER, e).as_str());
108+
logger.print("Opening default browser");
156109

157110
// Open the default web browser on the current system.
158111
match open::that(url) {

0 commit comments

Comments
 (0)