Implement directory scanner (#34)

This commit is contained in:
Tim Mulqueen
2019-05-12 13:37:23 -04:00
committed by Matan Kushner
parent d3ce00c516
commit 5fd715e7c3
5 changed files with 184 additions and 86 deletions
+24 -39
View File
@@ -4,59 +4,44 @@ use std::process::Command;
use super::{Context, Module};
/// Creates a segment with the current Python version
/// Creates a segment with the current Go version
///
/// Will display the Python version if any of the following criteria are met:
/// - Current directory contains a `.go` file
/// Will display the Go version if any of the following criteria are met:
/// - 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
/// - Current directory contains a `.go` file
/// - Current directory contains a `Godeps` directory
pub fn segment(context: &Context) -> Option<Module> {
let is_go_project = context.dir_files.iter().any(has_go_files);
let is_go_project = context
.new_scan_dir()
.set_files(&["go.mod", "go.sum", "glide.yaml", "Gopkg.yml", "Gopkg.lock"])
.set_extensions(&["go"])
.set_folders(&["Godeps"])
.scan();
if !is_go_project {
return None;
}
const GO_CHAR: &str = "🐹 ";
let module_color = Color::Cyan.bold();
match get_go_version() {
Some(go_version) => {
const GO_CHAR: &str = "🐹 ";
let module_color = Color::Cyan.bold();
let mut module = Module::new("go");
module.set_style(module_color);
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);
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)
Some(module)
}
None => None,
}
}
fn get_go_version() -> Option<String> {