summaryrefslogtreecommitdiffstats
path: root/src/modules/go.rs
blob: f6b2f2c9a991472dff0e9a3fabfddf857091b7bb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
use ansi_term::Color;
use std::path::PathBuf;
use std::process::Command;

use super::{Context, Module};

/// Creates a segment with the current Python version
///
/// Will display the Python version if any of the following criteria are met:
///     - Current directory contains a `.go` file
///     - Current directory contains a `go.mod` file
///     - Current directory contains a `go.sum` file
///     - Current directory contains a `Godeps` directory
///     - Current directory contains a `glide.yaml` file
///     - Current directory contains a `Gopkg.yml` file
///     - Current directory contains a `Gopkg.lock` file
pub fn segment(context: &Context) -> Option<Module> {
    let is_go_project = context.dir_files.iter().any(has_go_files);
    if !is_go_project {
        return None;
    }

    const GO_CHAR: &str = "🐹 ";
    let module_color = Color::Cyan.bold();

    let mut module = Module::new("go");
    module.set_style(module_color);

    let go_version = get_go_version()?;
    let formatted_version = format_go_version(go_version)?;
    module.new_segment("symbol", GO_CHAR);
    module.new_segment("version", formatted_version);

    Some(module)
}

fn has_go_files(dir_entry: &PathBuf) -> bool {
    let is_go_mod =
        |d: &PathBuf| -> bool { d.is_file() && d.file_name().unwrap_or_default() == "go.mod" };
    let is_go_sum =
        |d: &PathBuf| -> bool { d.is_file() && d.file_name().unwrap_or_default() == "go.sum" };
    let is_godeps =
        |d: &PathBuf| -> bool { d.is_dir() && d.file_name().unwrap_or_default() == "Godeps" };
    let is_glide_yaml =
        |d: &PathBuf| -> bool { d.is_file() && d.file_name().unwrap_or_default() == "glide.yaml" };
    let is_go_file =
        |d: &PathBuf| -> bool { d.is_file() && d.extension().unwrap_or_default() == "go" };
    let is_gopkg_yml =
        |d: &PathBuf| -> bool { d.is_file() && d.file_name().unwrap_or_default() == "Gopkg.yml" };
    let is_gopkg_lock =
        |d: &PathBuf| -> bool { d.is_file() && d.file_name().unwrap_or_default() == "Gopkg.lock" };

    is_go_mod(&dir_entry)
        || is_go_sum(&dir_entry)
        || is_godeps(&dir_entry)
        || is_glide_yaml(&dir_entry)
        || is_go_file(&dir_entry)
        || is_gopkg_yml(&dir_entry)
        || is_gopkg_lock(&dir_entry)
}

fn get_go_version() -> Option<String> {
    Command::new("go")
        .arg("version")
        .output()
        .ok()
        .and_then(|output| String::from_utf8(output.stdout).ok())
}

fn format_go_version(go_stdout: String) -> Option<String> {
    let mut version = go_stdout
        // split into ["", "1.12.4 linux/amd64"]
        .splitn(2, "go version go")
        // return "1.12.4 linux/amd64"
        .nth(1)?
        // split into ["1.12.4", "linux/amd64"]
        .split_whitespace()
        // return "1.12.4"
        .next()?;

    let mut formatted_version = String::with_capacity(version.len() + 1);
    formatted_version.push('v');
    formatted_version.push_str(version);
    Some(formatted_version)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_format_go_version() {
        let input = String::from("go version go1.12 darwin/amd64");
        assert_eq!(format_go_version(input), Some("v1.12".to_string()));
    }
}