From 9b1c1decb86b5c82354a8a10331d33a9c52d7f2b Mon Sep 17 00:00:00 2001 From: no-realm <7832423+no-realm@users.noreply.github.com> Date: Wed, 23 Oct 2024 00:56:26 +0200 Subject: [PATCH] add function for fetching zls version based on the given zig version --- cli/error.go | 1 + cli/version.go | 66 +++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 66 insertions(+), 1 deletion(-) diff --git a/cli/error.go b/cli/error.go index f71a596..66bac89 100644 --- a/cli/error.go +++ b/cli/error.go @@ -18,4 +18,5 @@ var ( ErrInvalidInput = errors.New("invalid input") // ErrDownloadFail is an an error when fetching Zig, or constructing a target URL to fetch Zig. ErrDownloadFail = errors.New("failed to download Zig") + ErrNoZlsVersion = errors.New("zls release worker returned error") ) diff --git a/cli/version.go b/cli/version.go index 18d5697..15b5f5b 100644 --- a/cli/version.go +++ b/cli/version.go @@ -10,8 +10,10 @@ import ( "fmt" "io" "net/http" + "net/url" "os" "path/filepath" + "strconv" "github.com/tristanisham/zvm/cli/meta" @@ -76,7 +78,7 @@ func (z *ZVM) fetchZlsTaggedVersionMap() (zigVersionMap, error) { log.Debug("inital ZRW", "url", z.Settings.ZlsReleaseWorkerBaseUrl) if err := z.loadSettings(); err != nil { - log.Warnf("could not load version map from settings: %q", err) + log.Warnf("could not load zls release worker base url from settings: %q", err) log.Debug("zrw", z.Settings.ZlsReleaseWorkerBaseUrl) } @@ -126,4 +128,66 @@ func (z *ZVM) fetchZlsTaggedVersionMap() (zigVersionMap, error) { return rawVersionStructure, nil } +// note: the zls release-worker uses the same index format as zig, but without the latest master entry. +// this function does not write the result to a file. +func (z *ZVM) fetchZlsVersionByZigVersion(version string) (zigVersion, error) { + log.Debug("inital ZRW", "url", z.Settings.ZlsReleaseWorkerBaseUrl) + + if err := z.loadSettings(); err != nil { + log.Warnf("could not load zls release worker base url from settings: %q", err) + log.Debug("zrw", z.Settings.ZlsReleaseWorkerBaseUrl) + } + + defaultZrwBaseUrl := "https://releases.zigtools.org" + + zrwBaseUrl := z.Settings.ZlsReleaseWorkerBaseUrl + + log.Debug("setting's ZRW", "url", zrwBaseUrl) + + if len(zrwBaseUrl) == 0 { + zrwBaseUrl = defaultZrwBaseUrl + } + + // https://github.com/zigtools/release-worker?tab=readme-ov-file#query-parameters + // The compatibility query parameter must be either only-runtime or full: + // full: Request a ZLS build that can be built and used with the given Zig version. + // only-runtime: Request a ZLS build that can be used at runtime with the given Zig version but may not be able to build ZLS from source. + selectVersionUrl := fmt.Sprintf("%s/v1/zls/select-version?zig_version=%s&compatibility=full", zrwBaseUrl, url.QueryEscape(version)) + req, err := http.NewRequest("GET", selectVersionUrl, nil) + if err != nil { + return nil, err + } + + req.Header.Set("User-Agent", "zvm "+meta.VERSION) + client := http.DefaultClient + resp, err := client.Do(req) + if err != nil { + return nil, err + } + defer resp.Body.Close() + + versions, err := io.ReadAll(resp.Body) + if err != nil { + return nil, err + } + + rawVersionStructure := make(zigVersion) + if err := json.Unmarshal(versions, &rawVersionStructure); err != nil { + var syntaxErr *json.SyntaxError + if errors.As(err, &syntaxErr) { + return nil, fmt.Errorf("%w: %w", ErrInvalidVersionMap, err) + } + + return nil, err + } + + if code, ok := rawVersionStructure["code"]; ok { + codeStr := strconv.FormatFloat(code.(float64), 'f', 0, 64) + msg := rawVersionStructure["message"] + return nil, fmt.Errorf("%w: code %s: %s", ErrNoZlsVersion, codeStr, msg) + } + + return rawVersionStructure, nil +} + // statelessFetchVersionMap is the same as fetchVersionMap but it doesn't write to disk. Will probably be depreciated and nuked from orbit when my