Skip to content

Commit

Permalink
feat: add support for idiomatic go.mod file (jdx#4312)
Browse files Browse the repository at this point in the history
Fix for discussion jdx#4136

---------

Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
Co-authored-by: jdx <216188+jdx@users.noreply.github.com>
  • Loading branch information
3 people authored Feb 5, 2025
1 parent c032db2 commit 842d051
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
2 changes: 2 additions & 0 deletions mise.lock
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@ backend = "core:bun"

[tools.bun.checksums]
"bun-darwin-aarch64.zip" = "sha256:c4d58e06c5c33885b526f4d91a38ca9ebdb9fc3fb4cd547f7d3302055c98e41c"
"bun-linux-x64-baseline.zip" = "sha256:cad7756a6ee16f3432a328f8023fc5cd431106822eacfa6d6d3afbad6fdc24db"

[tools.cargo-binstall]
version = "1.10.22"
backend = "aqua:cargo-bins/cargo-binstall"

[tools.cargo-binstall.checksums]
"cargo-binstall-aarch64-apple-darwin.zip" = "sha256:97ce4a2f18181f052dda266b042d8bb220e48ffe40ca75e796ae4c5e418b9e01"
"cargo-binstall-x86_64-unknown-linux-musl.tgz" = "sha256:74d7c647c7e60bb8464fa551702fdd38a7241f5cedb2c4edc3b11639cd1dae47"

[tools."cargo:cargo-edit"]
version = "0.13.1"
Expand Down
49 changes: 48 additions & 1 deletion src/plugins/core/go.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,19 @@ impl Backend for GoPlugin {
})
}
fn idiomatic_filenames(&self) -> eyre::Result<Vec<String>> {
Ok(vec![".go-version".into()])
Ok(vec![".go-version".into(), "go.mod".into()])
}

fn parse_idiomatic_file(&self, path: &Path) -> eyre::Result<String> {
let v = match path.file_name() {
Some(name) if name == "go.mod" => parse_gomod_file(&file::read_to_string(path)?),
_ => {
// .go-version
let body = file::read_to_string(path)?;
body.trim().trim_start_matches('v').to_string()
}
};
Ok(v)
}

fn install_version_(
Expand Down Expand Up @@ -240,6 +252,19 @@ impl Backend for GoPlugin {
}
}

fn parse_gomod_file(body: &str) -> String {
let v = body
.lines()
.find(|line| line.trim().starts_with("go "))
.unwrap_or_default();
let v = regex!(r#"^[^0-9]*"#).replace_all(v, "").trim().to_string();
// make sure it's like 1.23.0
if !regex!(r"^([0-9.])*$").is_match(&v) {
return "".to_string();
}
v
}

fn platform() -> &'static str {
if cfg!(target_os = "macos") {
"darwin"
Expand Down Expand Up @@ -268,3 +293,25 @@ fn ext() -> &'static str {
"tar.gz"
}
}

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

#[test]
fn test_parse_gomod_file() {
assert_eq!(
parse_gomod_file(indoc! {r#"module example.com/mymodule
go 1.14
require (
example.com/othermodule v1.2.3
example.com/thismodule v1.2.3
example.com/thatmodule v1.2.3
)
replace example.com/thatmodule => ../thatmodule
exclude example.com/thismodule v1.3.0"#}),
"1.14"
);
}
}

0 comments on commit 842d051

Please sign in to comment.