style: Clean up Golang module (#612)

This commit is contained in:
Matias Kotlik 2019-10-31 20:53:28 -05:00 committed by Matan Kushner
parent f2cb529d1b
commit e01c41eddf
1 changed files with 11 additions and 16 deletions

View File

@ -26,22 +26,17 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
return None; return None;
} }
match get_go_version() {
Some(go_version) => {
let mut module = context.new_module("golang"); let mut module = context.new_module("golang");
let config: GoConfig = GoConfig::try_load(module.config); let config: GoConfig = GoConfig::try_load(module.config);
module.set_style(config.style); module.set_style(config.style);
module.create_segment("symbol", &config.symbol); module.create_segment("symbol", &config.symbol);
let formatted_version = format_go_version(&go_version)?; let formatted_version = format_go_version(&get_go_version()?)?;
module.create_segment("version", &config.version.with_value(&formatted_version)); module.create_segment("version", &config.version.with_value(&formatted_version));
Some(module) Some(module)
} }
None => None,
}
}
fn get_go_version() -> Option<String> { fn get_go_version() -> Option<String> {
Command::new("go") Command::new("go")
@ -52,6 +47,9 @@ fn get_go_version() -> Option<String> {
} }
fn format_go_version(go_stdout: &str) -> Option<String> { fn format_go_version(go_stdout: &str) -> Option<String> {
// go version output looks like this:
// go version go1.13.3 linux/amd64
let version = go_stdout let version = go_stdout
// split into ["", "1.12.4 linux/amd64"] // split into ["", "1.12.4 linux/amd64"]
.splitn(2, "go version go") .splitn(2, "go version go")
@ -62,10 +60,7 @@ fn format_go_version(go_stdout: &str) -> Option<String> {
// return "1.12.4" // return "1.12.4"
.next()?; .next()?;
let mut formatted_version = String::with_capacity(version.len() + 1); Some(format!("v{}", version))
formatted_version.push('v');
formatted_version.push_str(version);
Some(formatted_version)
} }
#[cfg(test)] #[cfg(test)]