woodpecker/cli/update/updater.go

139 lines
3.1 KiB
Go
Raw Normal View History

2024-02-19 08:16:27 +00:00
package update
import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"os"
"path"
"runtime"
"strings"
2024-02-19 08:16:27 +00:00
"github.com/rs/zerolog/log"
"go.woodpecker-ci.org/woodpecker/v3/version"
2024-02-19 08:16:27 +00:00
)
func CheckForUpdate(ctx context.Context, force bool) (*NewVersion, error) {
return checkForUpdate(ctx, woodpeckerVersionURL, force)
}
func checkForUpdate(ctx context.Context, versionURL string, force bool) (*NewVersion, error) {
log.Debug().Msgf("current version: %s", version.String())
2024-02-19 08:16:27 +00:00
if (version.String() == "dev" || strings.HasPrefix(version.String(), "next-")) && !force {
log.Debug().Msgf("skipping update check for development/next versions")
2024-02-19 08:16:27 +00:00
return nil, nil
}
2024-06-06 09:16:45 +00:00
req, err := http.NewRequestWithContext(ctx, http.MethodGet, versionURL, nil)
2024-02-19 08:16:27 +00:00
if err != nil {
return nil, err
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, errors.New("failed to fetch the latest release")
}
var versionData VersionData
if err := json.NewDecoder(resp.Body).Decode(&versionData); err != nil {
2024-02-19 08:16:27 +00:00
return nil, err
}
upstreamVersion := versionData.Latest
if strings.HasPrefix(version.String(), "next-") {
upstreamVersion = versionData.Next
} else if strings.HasSuffix(version.String(), "rc-") {
upstreamVersion = versionData.RC
2024-02-19 08:16:27 +00:00
}
installedVersion := strings.TrimPrefix(version.Version, "v")
upstreamVersion = strings.TrimPrefix(upstreamVersion, "v")
2024-02-19 08:16:27 +00:00
// using the latest release
if installedVersion == upstreamVersion && !force {
log.Debug().Msgf("no new version available")
return nil, nil
2024-02-19 08:16:27 +00:00
}
log.Debug().Msgf("new version available: %s", upstreamVersion)
2024-02-19 08:16:27 +00:00
assetURL := fmt.Sprintf(githubBinaryURL, upstreamVersion, runtime.GOOS, runtime.GOARCH)
2024-02-19 08:16:27 +00:00
return &NewVersion{
Version: upstreamVersion,
2024-02-19 08:16:27 +00:00
AssetURL: assetURL,
}, nil
}
func downloadNewVersion(ctx context.Context, downloadURL string) (string, error) {
log.Debug().Msgf("downloading new version from %s ...", downloadURL)
2024-02-19 08:16:27 +00:00
2024-06-06 09:16:45 +00:00
req, err := http.NewRequestWithContext(ctx, http.MethodGet, downloadURL, nil)
2024-02-19 08:16:27 +00:00
if err != nil {
return "", err
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
return "", err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return "", errors.New("failed to download the new version")
}
file, err := os.CreateTemp("", "woodpecker-cli-*.tar.gz")
if err != nil {
return "", err
}
defer file.Close()
if _, err := io.Copy(file, resp.Body); err != nil {
return "", err
}
log.Debug().Msgf("new version downloaded to %s", file.Name())
2024-02-19 08:16:27 +00:00
return file.Name(), nil
}
func extractNewVersion(tarFilePath string) (string, error) {
log.Debug().Msgf("extracting new version from %s ...", tarFilePath)
2024-02-19 08:16:27 +00:00
tarFile, err := os.Open(tarFilePath)
if err != nil {
return "", err
}
defer tarFile.Close()
tmpDir, err := os.MkdirTemp("", "woodpecker-cli-*")
if err != nil {
return "", err
}
2024-05-24 20:35:04 +00:00
err = UnTar(tmpDir, tarFile)
2024-02-19 08:16:27 +00:00
if err != nil {
return "", err
}
err = os.Remove(tarFilePath)
if err != nil {
return "", err
}
log.Debug().Msgf("new version extracted to %s", tmpDir)
2024-02-19 08:16:27 +00:00
return path.Join(tmpDir, "woodpecker-cli"), nil
}