* Update command added
* Ensure that the upgraded version is really greater
* Update command should not update prerelease
* Minor presentation changes
* Fix command generation in containers from docker-compose file
- Refactored service creation
* Place the full command to the "cmd" package
* Update cobra to v1.4.0
* Updated build and release creation
* Created an install script
* Add more doc
* Add some tests...
* Add a test directive in Makefile
This commit is contained in:
2022-03-31 14:12:20 +02:00
committed by GitHub
parent d0576d4b81
commit 7b774e84d8
18 changed files with 1037 additions and 1035 deletions

140
update/main.go Normal file
View File

@@ -0,0 +1,140 @@
package update
import (
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"os"
"runtime"
"time"
"golang.org/x/mod/semver"
)
var exe, _ = os.Executable()
var Version = "master" // reset by cmd/main.go
// Asset is a github asset from release url.
type Asset struct {
Name string `json:"name"`
URL string `json:"browser_download_url"`
}
// CheckLatestVersion check katenary latest version from release and propose to download it
func CheckLatestVersion() (string, []Asset, error) {
githuburl := "https://api.github.com/repos/metal3d/katenary/releases/latest"
// Create a HTTP client with 1s timeout
client := &http.Client{
Timeout: time.Second * 1,
}
// Create a request
req, err := http.NewRequest("GET", githuburl, nil)
if err != nil {
return "", nil, err
}
// Send the request via a client
resp, err := client.Do(req)
if err != nil {
return "", nil, err
}
defer resp.Body.Close()
// Get tag_name from the json response
var release = struct {
TagName string `json:"tag_name"`
Assets []Asset `json:"assets"`
PreRelease bool `json:"prerelease"`
}{}
err = json.NewDecoder(resp.Body).Decode(&release)
if err != nil {
return "", nil, err
}
// if it's a prerelease, don't update
if release.PreRelease {
return "", nil, errors.New("Prerelease detected, not updating")
}
// no tag, don't update
if release.TagName == "" {
return "", nil, errors.New("No release found")
}
// compare the current version, if the current version is the same or lower than the latest version, don't update
versions := []string{Version, release.TagName}
semver.Sort(versions)
if versions[1] == Version {
return "", nil, errors.New("Current version is the latest version")
}
return release.TagName, release.Assets, nil
}
// DownloadLatestVersion will download the latest version of katenary.
func DownloadLatestVersion(assets []Asset) error {
// Download the latest version
fmt.Println("Downloading the latest version...")
// ok, replace this from the current version to the latest version
err := os.Rename(exe, exe+".old")
if err != nil {
return err
}
// Download the latest version for the current OS
for _, asset := range assets {
switch runtime.GOOS {
case "windows":
if asset.Name == "katenary.exe" {
err = DownloadFile(asset.URL, exe)
}
case "linux":
switch runtime.GOARCH {
case "amd64":
if asset.Name == "katenary-linux-amd64" {
err = DownloadFile(asset.URL, exe)
}
case "arm64":
if asset.Name == "katenary-linux-arm64" {
err = DownloadFile(asset.URL, exe)
}
}
case "darwin":
if asset.Name == "katenary-darwin" {
err = DownloadFile(asset.URL, exe)
}
default:
fmt.Println("Unsupported OS")
err = errors.New("Unsupported OS")
}
}
if err == nil {
// remove the old version
os.Remove(exe + ".old")
} else {
// restore the old version
os.Rename(exe+".old", exe)
}
return err
}
// DownloadFile will download a url to a local file. It also ensure that the file is executable.
func DownloadFile(url, exe string) error {
// Download the url binary to exe path
resp, err := http.Get(url)
if err != nil {
return err
}
defer resp.Body.Close()
fp, err := os.OpenFile(exe, os.O_WRONLY|os.O_CREATE, 0755)
if err != nil {
return err
}
defer fp.Close()
_, err = io.Copy(fp, resp.Body)
return err
}

52
update/update_test.go Normal file
View File

@@ -0,0 +1,52 @@
package update
import (
"fmt"
"os"
"testing"
)
func TestDownloadLatestRelease(t *testing.T) {
// Reset the version to test the latest release
Version = "0.0.0"
// change "exe" to /tmp/test-katenary
exe = "/tmp/test-katenary"
defer os.Remove(exe)
// Now call the CheckLatestVersion function
version, assets, err := CheckLatestVersion()
if err != nil {
t.Errorf("Error: %s", err)
}
fmt.Println("Version found", version)
// Touch exe binary
f, _ := os.OpenFile(exe, os.O_RDONLY|os.O_CREATE, 0755)
f.Write(nil)
f.Close()
err = DownloadLatestVersion(assets)
if err != nil {
t.Errorf("Error: %s", err)
}
}
func TestAlreadyUpToDate(t *testing.T) {
Version = "99999.999.99"
exe = "/tmp/test-katenary"
defer os.Remove(exe)
// Call the version check
version, _, err := CheckLatestVersion()
if err == nil {
t.Errorf("Error: %s", err)
}
t.Log("Version is already the most recent", version)
}