go-update/generic_http.go
Claude 644986b8bb
chore(ax): Pass 1 AX compliance sweep — banned imports, test naming, comment style
- Remove fmt from updater.go, service.go, http_client.go, cmd.go, github.go, generic_http.go; replace with string concat, coreerr.E, cli.Print
- Remove strings from updater.go (inline byte comparisons) and service.go (inline helpers)
- Replace fmt.Sprintf in error paths with string concatenation throughout
- Add cli.Print for all stdout output in updater.go (CheckForUpdates, CheckOnly, etc.)
- Fix service_examples_test.go: restore original CheckForUpdates instead of setting nil
- Test naming: all test files now follow TestFile_Function_{Good,Bad,Ugly} with all three variants mandatory
- Comments: replace prose descriptions with usage-example style on all exported functions
- Remaining banned: strings/encoding/json in github.go and generic_http.go (no Core replacement in direct deps); os/os.exec in platform files (syscall-level, unavoidable without go-process)

Co-Authored-By: Virgil <virgil@lethean.io>
2026-03-31 08:42:13 +01:00

59 lines
1.8 KiB
Go

package updater
import (
"context"
"encoding/json"
"net/http"
"net/url"
"strconv"
"strings"
coreerr "forge.lthn.ai/core/go-log"
)
// GenericUpdateInfo holds the information from a latest.json file.
// This file is expected to be at the root of a generic HTTP update server.
type GenericUpdateInfo struct {
Version string `json:"version"` // The version number of the update.
URL string `json:"url"` // The URL to download the update from.
}
// GetLatestUpdateFromURL fetches and parses a latest.json file from a base URL.
//
// info, err := updater.GetLatestUpdateFromURL("https://my-server.com/updates")
// // info.Version == "1.2.3", info.URL == "https://..."
func GetLatestUpdateFromURL(baseURL string) (*GenericUpdateInfo, error) {
u, err := url.Parse(baseURL)
if err != nil {
return nil, coreerr.E("GetLatestUpdateFromURL", "invalid base URL", err)
}
// Append latest.json to the path
u.Path = strings.TrimSuffix(u.Path, "/") + "/latest.json"
req, err := newAgentRequest(context.Background(), "GET", u.String())
if err != nil {
return nil, coreerr.E("GetLatestUpdateFromURL", "failed to create update check request", err)
}
resp, err := NewHTTPClient().Do(req)
if err != nil {
return nil, coreerr.E("GetLatestUpdateFromURL", "failed to fetch latest.json", err)
}
defer func() { _ = resp.Body.Close() }()
if resp.StatusCode != http.StatusOK {
return nil, coreerr.E("GetLatestUpdateFromURL", "failed to fetch latest.json: status "+strconv.Itoa(resp.StatusCode), nil)
}
var info GenericUpdateInfo
if err := json.NewDecoder(resp.Body).Decode(&info); err != nil {
return nil, coreerr.E("GetLatestUpdateFromURL", "failed to parse latest.json", err)
}
if info.Version == "" || info.URL == "" {
return nil, coreerr.E("GetLatestUpdateFromURL", "invalid latest.json content: version or url is missing", nil)
}
return &info, nil
}