Skip to content

Commit

Permalink
add function for fetching zls version based on the given zig version
Browse files Browse the repository at this point in the history
  • Loading branch information
no-realm committed Oct 22, 2024
1 parent b93b355 commit 9b1c1de
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 1 deletion.
1 change: 1 addition & 0 deletions cli/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
)
66 changes: 65 additions & 1 deletion cli/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ import (
"fmt"
"io"
"net/http"
"net/url"
"os"
"path/filepath"
"strconv"

"github.com/tristanisham/zvm/cli/meta"

Expand Down Expand Up @@ -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)
}

Expand Down Expand Up @@ -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

0 comments on commit 9b1c1de

Please sign in to comment.