From d88169d5c2c020ef7c5fc40b483d193f071b2184 Mon Sep 17 00:00:00 2001 From: Miles Johnson Date: Sat, 6 Apr 2024 20:22:39 -0700 Subject: [PATCH] new: Support `dist-url` setting. (#20) --- CHANGELOG.md | 6 ++++++ Cargo.lock | 2 +- Cargo.toml | 2 +- README.md | 2 ++ src/config.rs | 15 +++++++++++++++ src/lib.rs | 1 + src/proto.rs | 27 +++++++++++---------------- tests/versions_test.rs | 2 +- 8 files changed, 38 insertions(+), 19 deletions(-) create mode 100644 src/config.rs diff --git a/CHANGELOG.md b/CHANGELOG.md index 1f782a5..09888fe 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # Changelog +## 0.10.2 + +#### 🚀 Updates + +- Added a `dist-url` config setting, allowing the download host to be customized. + ## 0.10.1 #### 🚀 Updates diff --git a/Cargo.lock b/Cargo.lock index ffe11b6..0e9059a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1264,7 +1264,7 @@ dependencies = [ [[package]] name = "go_plugin" -version = "0.10.1" +version = "0.10.2" dependencies = [ "extism-pdk", "proto_pdk", diff --git a/Cargo.toml b/Cargo.toml index 22d208e..756682c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "go_plugin" -version = "0.10.1" +version = "0.10.2" edition = "2021" license = "MIT" publish = false diff --git a/README.md b/README.md index 4374b1e..e025732 100644 --- a/README.md +++ b/README.md @@ -19,10 +19,12 @@ go = "source:https://github.com/moonrepo/go-plugin/releases/download/vX.Y.Z/go_p Go plugin can be configured with a `.prototools` file. +- `dist-url` (string) - The distribution URL to download Go archives from. Supports `{version}` and `{file}` tokens. - `gobin` (bool) - When enabled, will inject a `GOBIN` environment variable into your shell. Defaults to `true`. ```toml [tools.go] +dist-url = "https://..." gobin = false ``` diff --git a/src/config.rs b/src/config.rs new file mode 100644 index 0000000..e930a73 --- /dev/null +++ b/src/config.rs @@ -0,0 +1,15 @@ +#[derive(Debug, serde::Deserialize, serde::Serialize)] +#[serde(default, deny_unknown_fields, rename_all = "kebab-case")] +pub struct GoPluginConfig { + pub dist_url: String, + pub gobin: bool, +} + +impl Default for GoPluginConfig { + fn default() -> Self { + Self { + dist_url: "https://dl.google.com/go/{file}".into(), + gobin: true, + } + } +} diff --git a/src/lib.rs b/src/lib.rs index a235f26..2a36bc5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,3 +1,4 @@ +mod config; #[cfg(feature = "wasm")] mod proto; #[cfg(feature = "wasm")] diff --git a/src/proto.rs b/src/proto.rs index 6cfb4fb..97eba9a 100644 --- a/src/proto.rs +++ b/src/proto.rs @@ -1,7 +1,7 @@ +use crate::config::GoPluginConfig; use crate::version::{from_go_version, to_go_version}; use extism_pdk::*; use proto_pdk::*; -use serde::Deserialize; use std::collections::HashMap; #[host_fn] @@ -12,18 +12,6 @@ extern "ExtismHost" { static NAME: &str = "Go"; static BIN: &str = "go"; -#[derive(Deserialize)] -#[serde(default, rename_all = "kebab-case")] -struct GoConfig { - gobin: bool, -} - -impl Default for GoConfig { - fn default() -> Self { - Self { gobin: true } - } -} - #[plugin_fn] pub fn register_tool(Json(_): Json) -> FnResult> { Ok(Json(ToolMetadataOutput { @@ -124,10 +112,17 @@ pub fn download_prebuilt( format!("{prefix}.tar.gz") }; + let host = get_tool_config::()?.dist_url; + Ok(Json(DownloadPrebuiltOutput { archive_prefix: Some("go".into()), - checksum_url: Some(format!("https://dl.google.com/go/{filename}.sha256")), - download_url: format!("https://dl.google.com/go/{filename}"), + checksum_url: Some( + host.replace("{version}", &version) + .replace("{file}", &format!("{filename}.sha256")), + ), + download_url: host + .replace("{version}", &version) + .replace("{file}", &filename), download_name: Some(filename), ..DownloadPrebuiltOutput::default() })) @@ -157,7 +152,7 @@ pub fn locate_executables( pub fn sync_shell_profile( Json(input): Json, ) -> FnResult> { - let config = get_tool_config::()?; + let config = get_tool_config::()?; Ok(Json(SyncShellProfileOutput { check_var: "GOBIN".into(), diff --git a/tests/versions_test.rs b/tests/versions_test.rs index 2b1c749..677f8e6 100644 --- a/tests/versions_test.rs +++ b/tests/versions_test.rs @@ -5,7 +5,7 @@ generate_resolve_versions_tests!("go-test", { "1.11" => "1.11.13", "1.9.0-rc2" => "1.9.0-rc2", "1.21.0" => "1.21.0", - "1.21" => "1.21.8", + "1.21" => "1.21.9", }); #[test]