1use crate::GoLanguage;
2use proto_core::{async_trait, Detector, ProtoError};
3use starbase_utils::fs;
4use std::io::{BufRead, BufReader};
5use std::path::Path;
6
7static GOPREFIX: &str = "go ";
8
9#[async_trait]
10impl Detector<'_> for GoLanguage {
11 async fn detect_version_from(&self, working_dir: &Path) -> Result<Option<String>, ProtoError> {
12 let gowork = working_dir.join("go.work");
13
14 if gowork.exists() {
15 if let Some(version) = scan_for_go_version(&gowork) {
16 return Ok(Some(version));
17 }
18 }
19
20 let gomod = working_dir.join("go.mod");
21
22 if gomod.exists() {
23 if let Some(version) = scan_for_go_version(&gomod) {
24 return Ok(Some(version));
25 }
26 }
27
28 Ok(None)
29 }
30}
31
32fn scan_for_go_version(path: &Path) -> Option<String> {
33 if let Ok(file) = fs::open_file(path) {
34 let buffered = BufReader::new(file);
35
36 for line in buffered.lines().flatten() {
37 if let Some(version) = line.strip_prefix(GOPREFIX) {
38 return Some(version.into());
39 }
40 }
41 }
42
43 None
44}