Compare commits
5 commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0613a3aae8 | ||
|
|
bbd06db2df | ||
|
|
00f30708ac | ||
|
|
333857cb3a | ||
|
|
ee154a2c15 |
10 changed files with 173 additions and 44 deletions
17
cmd.go
17
cmd.go
|
|
@ -45,9 +45,9 @@ Examples:
|
||||||
RunE: runUpdate,
|
RunE: runUpdate,
|
||||||
}
|
}
|
||||||
|
|
||||||
updateCmd.PersistentFlags().StringVar(&updateChannel, "channel", "stable", "Release channel: stable, beta, alpha, or dev")
|
updateCmd.PersistentFlags().StringVar(&updateChannel, "channel", "stable", "Release channel: stable, beta, alpha, prerelease, or dev")
|
||||||
updateCmd.PersistentFlags().BoolVar(&updateForce, "force", false, "Force update even if already on latest version")
|
updateCmd.PersistentFlags().BoolVar(&updateForce, "force", false, "Force update even if already on latest version")
|
||||||
updateCmd.Flags().BoolVar(&updateCheck, "check", false, "Only check for updates, don't apply")
|
updateCmd.Flags().BoolVar(&updateCheck, "check", false, "Only check for updates, do not apply")
|
||||||
updateCmd.Flags().IntVar(&updateWatchPID, "watch-pid", 0, "Internal: watch for parent PID to die then restart")
|
updateCmd.Flags().IntVar(&updateWatchPID, "watch-pid", 0, "Internal: watch for parent PID to die then restart")
|
||||||
_ = updateCmd.Flags().MarkHidden("watch-pid")
|
_ = updateCmd.Flags().MarkHidden("watch-pid")
|
||||||
|
|
||||||
|
|
@ -55,7 +55,9 @@ Examples:
|
||||||
Use: "check",
|
Use: "check",
|
||||||
Short: "Check for available updates",
|
Short: "Check for available updates",
|
||||||
RunE: func(cmd *cobra.Command, args []string) error {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
|
previousCheck := updateCheck
|
||||||
updateCheck = true
|
updateCheck = true
|
||||||
|
defer func() { updateCheck = previousCheck }()
|
||||||
return runUpdate(cmd, args)
|
return runUpdate(cmd, args)
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
@ -70,24 +72,25 @@ func runUpdate(cmd *cobra.Command, args []string) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
currentVersion := cli.AppVersion
|
currentVersion := cli.AppVersion
|
||||||
|
normalizedChannel := normaliseGitHubChannel(updateChannel)
|
||||||
|
|
||||||
cli.Print("%s %s\n", cli.DimStyle.Render("Current version:"), cli.ValueStyle.Render(currentVersion))
|
cli.Print("%s %s\n", cli.DimStyle.Render("Current version:"), cli.ValueStyle.Render(currentVersion))
|
||||||
cli.Print("%s %s/%s\n", cli.DimStyle.Render("Platform:"), runtime.GOOS, runtime.GOARCH)
|
cli.Print("%s %s/%s\n", cli.DimStyle.Render("Platform:"), runtime.GOOS, runtime.GOARCH)
|
||||||
cli.Print("%s %s\n\n", cli.DimStyle.Render("Channel:"), updateChannel)
|
cli.Print("%s %s\n\n", cli.DimStyle.Render("Channel:"), normalizedChannel)
|
||||||
|
|
||||||
// Handle dev channel specially - it's a prerelease tag, not a semver channel
|
// Handle dev channel specially - it's a prerelease tag, not a semver channel
|
||||||
if updateChannel == "dev" {
|
if normalizedChannel == "dev" {
|
||||||
return handleDevUpdate(currentVersion)
|
return handleDevUpdate(currentVersion)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check for newer version
|
// Check for newer version
|
||||||
release, updateAvailable, err := CheckForNewerVersion(repoOwner, repoName, updateChannel, true)
|
release, updateAvailable, err := CheckForNewerVersion(repoOwner, repoName, normalizedChannel, true)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return cli.Wrap(err, "failed to check for updates")
|
return cli.Wrap(err, "failed to check for updates")
|
||||||
}
|
}
|
||||||
|
|
||||||
if release == nil {
|
if release == nil {
|
||||||
cli.Print("%s No releases found in %s channel\n", cli.WarningStyle.Render("!"), updateChannel)
|
cli.Print("%s No releases found in %s channel\n", cli.WarningStyle.Render("!"), normalizedChannel)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -140,7 +143,7 @@ func handleDevUpdate(currentVersion string) error {
|
||||||
client := NewGithubClient()
|
client := NewGithubClient()
|
||||||
|
|
||||||
// Fetch the dev release directly by tag
|
// Fetch the dev release directly by tag
|
||||||
release, err := client.GetLatestRelease(context.TODO(), repoOwner, repoName, "beta")
|
release, err := client.GetLatestRelease(context.Background(), repoOwner, repoName, "beta")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// Try fetching the "dev" tag directly
|
// Try fetching the "dev" tag directly
|
||||||
return handleDevTagUpdate(currentVersion)
|
return handleDevTagUpdate(currentVersion)
|
||||||
|
|
|
||||||
|
|
@ -26,9 +26,7 @@ The `CheckOnStartup` field can take one of the following values:
|
||||||
|
|
||||||
If you are using the example CLI provided in `cmd/updater`, the following flags are available:
|
If you are using the example CLI provided in `cmd/updater`, the following flags are available:
|
||||||
|
|
||||||
* `--check-update`: Check for new updates without applying them.
|
* `--check`: Check for new updates without applying them.
|
||||||
* `--do-update`: Perform an update if available.
|
* `--channel`: Set the update channel (e.g., stable, beta, alpha). Defaults to `stable`.
|
||||||
* `--channel`: Set the update channel (e.g., stable, beta, alpha). If not set, it's determined from the current version tag.
|
* `--force`: Force update even when already on latest.
|
||||||
* `--force-semver-prefix`: Force 'v' prefix on semver tags (default `true`).
|
* `--watch-pid`: Internal flag used during restart after update.
|
||||||
* `--release-url-format`: A URL format for release assets.
|
|
||||||
* `--pull-request`: Update to a specific pull request (integer ID).
|
|
||||||
|
|
|
||||||
|
|
@ -3,8 +3,10 @@ package updater
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"context"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
|
"strings"
|
||||||
|
|
||||||
coreerr "forge.lthn.ai/core/go-log"
|
coreerr "forge.lthn.ai/core/go-log"
|
||||||
)
|
)
|
||||||
|
|
@ -31,10 +33,16 @@ func GetLatestUpdateFromURL(baseURL string) (*GenericUpdateInfo, error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, coreerr.E("GetLatestUpdateFromURL", "invalid base URL", err)
|
return nil, coreerr.E("GetLatestUpdateFromURL", "invalid base URL", err)
|
||||||
}
|
}
|
||||||
// Append latest.json to the path
|
|
||||||
u.Path += "/latest.json"
|
|
||||||
|
|
||||||
resp, err := http.Get(u.String())
|
// 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 {
|
if err != nil {
|
||||||
return nil, coreerr.E("GetLatestUpdateFromURL", "failed to fetch latest.json", err)
|
return nil, coreerr.E("GetLatestUpdateFromURL", "failed to fetch latest.json", err)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
17
github.go
17
github.go
|
|
@ -52,10 +52,13 @@ var NewAuthenticatedClient = func(ctx context.Context) *http.Client {
|
||||||
if token == "" {
|
if token == "" {
|
||||||
return http.DefaultClient
|
return http.DefaultClient
|
||||||
}
|
}
|
||||||
|
|
||||||
ts := oauth2.StaticTokenSource(
|
ts := oauth2.StaticTokenSource(
|
||||||
&oauth2.Token{AccessToken: token},
|
&oauth2.Token{AccessToken: token},
|
||||||
)
|
)
|
||||||
return oauth2.NewClient(ctx, ts)
|
client := oauth2.NewClient(ctx, ts)
|
||||||
|
client.Timeout = defaultHTTPTimeout
|
||||||
|
return client
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *githubClient) GetPublicRepos(ctx context.Context, userOrOrg string) ([]string, error) {
|
func (g *githubClient) GetPublicRepos(ctx context.Context, userOrOrg string) ([]string, error) {
|
||||||
|
|
@ -71,11 +74,10 @@ func (g *githubClient) getPublicReposWithAPIURL(ctx context.Context, apiURL, use
|
||||||
if err := ctx.Err(); err != nil {
|
if err := ctx.Err(); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
req, err := http.NewRequestWithContext(ctx, "GET", url, nil)
|
req, err := newAgentRequest(ctx, "GET", url)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
req.Header.Set("User-Agent", "Borg-Data-Collector")
|
|
||||||
resp, err := client.Do(req)
|
resp, err := client.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|
@ -85,11 +87,10 @@ func (g *githubClient) getPublicReposWithAPIURL(ctx context.Context, apiURL, use
|
||||||
_ = resp.Body.Close()
|
_ = resp.Body.Close()
|
||||||
// Try organization endpoint
|
// Try organization endpoint
|
||||||
url = fmt.Sprintf("%s/orgs/%s/repos", apiURL, userOrOrg)
|
url = fmt.Sprintf("%s/orgs/%s/repos", apiURL, userOrOrg)
|
||||||
req, err = http.NewRequestWithContext(ctx, "GET", url, nil)
|
req, err = newAgentRequest(ctx, "GET", url)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
req.Header.Set("User-Agent", "Borg-Data-Collector")
|
|
||||||
resp, err = client.Do(req)
|
resp, err = client.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|
@ -143,11 +144,10 @@ func (g *githubClient) GetLatestRelease(ctx context.Context, owner, repo, channe
|
||||||
client := NewAuthenticatedClient(ctx)
|
client := NewAuthenticatedClient(ctx)
|
||||||
url := fmt.Sprintf("https://api.github.com/repos/%s/%s/releases", owner, repo)
|
url := fmt.Sprintf("https://api.github.com/repos/%s/%s/releases", owner, repo)
|
||||||
|
|
||||||
req, err := http.NewRequestWithContext(ctx, "GET", url, nil)
|
req, err := newAgentRequest(ctx, "GET", url)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
req.Header.Set("User-Agent", "Borg-Data-Collector")
|
|
||||||
|
|
||||||
resp, err := client.Do(req)
|
resp, err := client.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -198,11 +198,10 @@ func (g *githubClient) GetReleaseByPullRequest(ctx context.Context, owner, repo
|
||||||
client := NewAuthenticatedClient(ctx)
|
client := NewAuthenticatedClient(ctx)
|
||||||
url := fmt.Sprintf("https://api.github.com/repos/%s/%s/releases", owner, repo)
|
url := fmt.Sprintf("https://api.github.com/repos/%s/%s/releases", owner, repo)
|
||||||
|
|
||||||
req, err := http.NewRequestWithContext(ctx, "GET", url, nil)
|
req, err := newAgentRequest(ctx, "GET", url)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
req.Header.Set("User-Agent", "Borg-Data-Collector")
|
|
||||||
|
|
||||||
resp, err := client.Do(req)
|
resp, err := client.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
||||||
|
|
@ -82,6 +82,54 @@ func TestDetermineChannel_Good(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestCheckForUpdatesByTag_UsesCurrentVersionChannel(t *testing.T) {
|
||||||
|
originalVersion := Version
|
||||||
|
originalCheckForUpdates := CheckForUpdates
|
||||||
|
defer func() {
|
||||||
|
Version = originalVersion
|
||||||
|
CheckForUpdates = originalCheckForUpdates
|
||||||
|
}()
|
||||||
|
|
||||||
|
var gotChannel string
|
||||||
|
CheckForUpdates = func(owner, repo, channel string, forceSemVerPrefix bool, releaseURLFormat string) error {
|
||||||
|
gotChannel = channel
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
Version = "v2.0.0-rc.1"
|
||||||
|
if err := CheckForUpdatesByTag("owner", "repo"); err != nil {
|
||||||
|
t.Fatalf("unexpected error: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if gotChannel != "beta" {
|
||||||
|
t.Fatalf("expected beta channel, got %q", gotChannel)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCheckOnlyByTag_UsesCurrentVersionChannel(t *testing.T) {
|
||||||
|
originalVersion := Version
|
||||||
|
originalCheckOnly := CheckOnly
|
||||||
|
defer func() {
|
||||||
|
Version = originalVersion
|
||||||
|
CheckOnly = originalCheckOnly
|
||||||
|
}()
|
||||||
|
|
||||||
|
var gotChannel string
|
||||||
|
CheckOnly = func(owner, repo, channel string, forceSemVerPrefix bool, releaseURLFormat string) error {
|
||||||
|
gotChannel = channel
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
Version = "v2.0.0-alpha.1"
|
||||||
|
if err := CheckOnlyByTag("owner", "repo"); err != nil {
|
||||||
|
t.Fatalf("unexpected error: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if gotChannel != "alpha" {
|
||||||
|
t.Fatalf("expected alpha channel, got %q", gotChannel)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestGetDownloadURL_Good(t *testing.T) {
|
func TestGetDownloadURL_Good(t *testing.T) {
|
||||||
osName := runtime.GOOS
|
osName := runtime.GOOS
|
||||||
archName := runtime.GOARCH
|
archName := runtime.GOARCH
|
||||||
|
|
|
||||||
14
go.mod
14
go.mod
|
|
@ -1,11 +1,11 @@
|
||||||
module forge.lthn.ai/core/go-update
|
module dappco.re/go/core/update
|
||||||
|
|
||||||
go 1.26.0
|
go 1.26.0
|
||||||
|
|
||||||
require (
|
require (
|
||||||
forge.lthn.ai/core/cli v0.3.6
|
dappco.re/go/core/cli v0.3.6
|
||||||
forge.lthn.ai/core/go-io v0.1.5
|
dappco.re/go/core/io v0.1.5
|
||||||
forge.lthn.ai/core/go-log v0.0.4
|
dappco.re/go/core/log v0.0.4
|
||||||
github.com/Snider/Borg v0.2.0
|
github.com/Snider/Borg v0.2.0
|
||||||
github.com/minio/selfupdate v0.6.0
|
github.com/minio/selfupdate v0.6.0
|
||||||
github.com/spf13/cobra v1.10.2
|
github.com/spf13/cobra v1.10.2
|
||||||
|
|
@ -16,9 +16,9 @@ require (
|
||||||
require (
|
require (
|
||||||
aead.dev/minisign v0.3.0 // indirect
|
aead.dev/minisign v0.3.0 // indirect
|
||||||
dario.cat/mergo v1.0.0 // indirect
|
dario.cat/mergo v1.0.0 // indirect
|
||||||
forge.lthn.ai/core/go v0.3.1 // indirect
|
dappco.re/go/core v0.3.1 // indirect
|
||||||
forge.lthn.ai/core/go-i18n v0.1.6 // indirect
|
dappco.re/go/core/i18n v0.1.6 // indirect
|
||||||
forge.lthn.ai/core/go-inference v0.1.5 // indirect
|
dappco.re/go/core/inference v0.1.5 // indirect
|
||||||
github.com/Microsoft/go-winio v0.6.2 // indirect
|
github.com/Microsoft/go-winio v0.6.2 // indirect
|
||||||
github.com/ProtonMail/go-crypto v1.4.0 // indirect
|
github.com/ProtonMail/go-crypto v1.4.0 // indirect
|
||||||
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
|
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
|
||||||
|
|
|
||||||
32
http_client.go
Normal file
32
http_client.go
Normal file
|
|
@ -0,0 +1,32 @@
|
||||||
|
package updater
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
const defaultHTTPTimeout = 30 * time.Second
|
||||||
|
|
||||||
|
var NewHTTPClient = func() *http.Client {
|
||||||
|
return &http.Client{Timeout: defaultHTTPTimeout}
|
||||||
|
}
|
||||||
|
|
||||||
|
func newAgentRequest(ctx context.Context, method, url string) (*http.Request, error) {
|
||||||
|
req, err := http.NewRequestWithContext(ctx, method, url, nil)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
req.Header.Set("User-Agent", updaterUserAgent())
|
||||||
|
return req, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func updaterUserAgent() string {
|
||||||
|
version := formatVersionForDisplay(Version, true)
|
||||||
|
if version == "" {
|
||||||
|
version = "unknown"
|
||||||
|
}
|
||||||
|
return fmt.Sprintf("agent-go-update/%s", version)
|
||||||
|
}
|
||||||
15
service.go
15
service.go
|
|
@ -30,7 +30,8 @@ type UpdateServiceConfig struct {
|
||||||
// repository URL (e.g., "https://github.com/owner/repo") or a base URL
|
// repository URL (e.g., "https://github.com/owner/repo") or a base URL
|
||||||
// for a generic HTTP update server.
|
// for a generic HTTP update server.
|
||||||
RepoURL string
|
RepoURL string
|
||||||
// Channel specifies the release channel to track (e.g., "stable", "prerelease").
|
// Channel specifies the release channel to track (e.g., "stable", "beta", or "prerelease").
|
||||||
|
// "prerelease" is normalised to "beta" to match the GitHub release filter.
|
||||||
// This is only used for GitHub-based updates.
|
// This is only used for GitHub-based updates.
|
||||||
Channel string
|
Channel string
|
||||||
// CheckOnStartup determines the update behavior when the service starts.
|
// CheckOnStartup determines the update behavior when the service starts.
|
||||||
|
|
@ -63,6 +64,7 @@ func NewUpdateService(config UpdateServiceConfig) (*UpdateService, error) {
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
if isGitHub {
|
if isGitHub {
|
||||||
|
config.Channel = normaliseGitHubChannel(config.Channel)
|
||||||
owner, repo, err = ParseRepoURL(config.RepoURL)
|
owner, repo, err = ParseRepoURL(config.RepoURL)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, coreerr.E("NewUpdateService", "failed to parse GitHub repo URL", err)
|
return nil, coreerr.E("NewUpdateService", "failed to parse GitHub repo URL", err)
|
||||||
|
|
@ -127,3 +129,14 @@ func ParseRepoURL(repoURL string) (owner string, repo string, err error) {
|
||||||
}
|
}
|
||||||
return parts[0], parts[1], nil
|
return parts[0], parts[1], nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func normaliseGitHubChannel(channel string) string {
|
||||||
|
channel = strings.ToLower(strings.TrimSpace(channel))
|
||||||
|
if channel == "" {
|
||||||
|
return "stable"
|
||||||
|
}
|
||||||
|
if channel == "prerelease" {
|
||||||
|
return "beta"
|
||||||
|
}
|
||||||
|
return channel
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -12,13 +12,15 @@ func TestNewUpdateService(t *testing.T) {
|
||||||
config UpdateServiceConfig
|
config UpdateServiceConfig
|
||||||
expectError bool
|
expectError bool
|
||||||
isGitHub bool
|
isGitHub bool
|
||||||
|
wantChannel string
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
name: "Valid GitHub URL",
|
name: "Valid GitHub URL",
|
||||||
config: UpdateServiceConfig{
|
config: UpdateServiceConfig{
|
||||||
RepoURL: "https://github.com/owner/repo",
|
RepoURL: "https://github.com/owner/repo",
|
||||||
},
|
},
|
||||||
isGitHub: true,
|
isGitHub: true,
|
||||||
|
wantChannel: "stable",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "Valid non-GitHub URL",
|
name: "Valid non-GitHub URL",
|
||||||
|
|
@ -27,6 +29,24 @@ func TestNewUpdateService(t *testing.T) {
|
||||||
},
|
},
|
||||||
isGitHub: false,
|
isGitHub: false,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
name: "GitHub channel is normalised",
|
||||||
|
config: UpdateServiceConfig{
|
||||||
|
RepoURL: "https://github.com/owner/repo",
|
||||||
|
Channel: " Beta ",
|
||||||
|
},
|
||||||
|
isGitHub: true,
|
||||||
|
wantChannel: "beta",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "GitHub prerelease channel maps to beta",
|
||||||
|
config: UpdateServiceConfig{
|
||||||
|
RepoURL: "https://github.com/owner/repo",
|
||||||
|
Channel: " prerelease ",
|
||||||
|
},
|
||||||
|
isGitHub: true,
|
||||||
|
wantChannel: "beta",
|
||||||
|
},
|
||||||
{
|
{
|
||||||
name: "Invalid GitHub URL",
|
name: "Invalid GitHub URL",
|
||||||
config: UpdateServiceConfig{
|
config: UpdateServiceConfig{
|
||||||
|
|
@ -45,6 +65,9 @@ func TestNewUpdateService(t *testing.T) {
|
||||||
if err == nil && service.isGitHub != tc.isGitHub {
|
if err == nil && service.isGitHub != tc.isGitHub {
|
||||||
t.Errorf("Expected isGitHub: %v, got: %v", tc.isGitHub, service.isGitHub)
|
t.Errorf("Expected isGitHub: %v, got: %v", tc.isGitHub, service.isGitHub)
|
||||||
}
|
}
|
||||||
|
if err == nil && tc.wantChannel != "" && service.config.Channel != tc.wantChannel {
|
||||||
|
t.Errorf("Expected GitHub channel %q, got %q", tc.wantChannel, service.config.Channel)
|
||||||
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
25
updater.go
25
updater.go
|
|
@ -31,17 +31,24 @@ var NewGithubClient = func() GithubClient {
|
||||||
// DoUpdate is a variable that holds the function to perform the actual update.
|
// DoUpdate is a variable that holds the function to perform the actual update.
|
||||||
// This can be replaced in tests to prevent actual updates.
|
// This can be replaced in tests to prevent actual updates.
|
||||||
var DoUpdate = func(url string) error {
|
var DoUpdate = func(url string) error {
|
||||||
resp, err := http.Get(url)
|
client := NewHTTPClient()
|
||||||
|
req, err := newAgentRequest(context.Background(), "GET", url)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return coreerr.E("DoUpdate", "failed to create update request", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
resp, err := client.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
return coreerr.E("DoUpdate", "failed to download update", err)
|
||||||
}
|
}
|
||||||
defer func(Body io.ReadCloser) {
|
defer func(Body io.ReadCloser) {
|
||||||
err := Body.Close()
|
_ = Body.Close()
|
||||||
if err != nil {
|
|
||||||
fmt.Printf("failed to close response body: %v\n", err)
|
|
||||||
}
|
|
||||||
}(resp.Body)
|
}(resp.Body)
|
||||||
|
|
||||||
|
if resp.StatusCode != http.StatusOK {
|
||||||
|
return coreerr.E("DoUpdate", fmt.Sprintf("failed to download update: %s", resp.Status), nil)
|
||||||
|
}
|
||||||
|
|
||||||
err = selfupdate.Apply(resp.Body, selfupdate.Options{})
|
err = selfupdate.Apply(resp.Body, selfupdate.Options{})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if rerr := selfupdate.RollbackError(err); rerr != nil {
|
if rerr := selfupdate.RollbackError(err); rerr != nil {
|
||||||
|
|
@ -49,8 +56,6 @@ var DoUpdate = func(url string) error {
|
||||||
}
|
}
|
||||||
return coreerr.E("DoUpdate", "update failed", err)
|
return coreerr.E("DoUpdate", "update failed", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Println("Update applied successfully.")
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -140,14 +145,14 @@ var CheckOnly = func(owner, repo, channel string, forceSemVerPrefix bool, releas
|
||||||
// CheckForUpdatesByTag checks for and applies updates from GitHub based on the channel
|
// CheckForUpdatesByTag checks for and applies updates from GitHub based on the channel
|
||||||
// determined by the current application's version tag (e.g., 'stable' or 'prerelease').
|
// determined by the current application's version tag (e.g., 'stable' or 'prerelease').
|
||||||
var CheckForUpdatesByTag = func(owner, repo string) error {
|
var CheckForUpdatesByTag = func(owner, repo string) error {
|
||||||
channel := determineChannel(Version, false) // isPreRelease is false for current version
|
channel := determineChannel(Version, semver.Prerelease(formatVersionForComparison(Version)) != "")
|
||||||
return CheckForUpdates(owner, repo, channel, true, "")
|
return CheckForUpdates(owner, repo, channel, true, "")
|
||||||
}
|
}
|
||||||
|
|
||||||
// CheckOnlyByTag checks for updates from GitHub based on the channel determined by the
|
// CheckOnlyByTag checks for updates from GitHub based on the channel determined by the
|
||||||
// current version tag, without applying them.
|
// current version tag, without applying them.
|
||||||
var CheckOnlyByTag = func(owner, repo string) error {
|
var CheckOnlyByTag = func(owner, repo string) error {
|
||||||
channel := determineChannel(Version, false) // isPreRelease is false for current version
|
channel := determineChannel(Version, semver.Prerelease(formatVersionForComparison(Version)) != "")
|
||||||
return CheckOnly(owner, repo, channel, true, "")
|
return CheckOnly(owner, repo, channel, true, "")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue