diff --git a/update/main.go b/update/main.go deleted file mode 100644 index 567c6b0..0000000 --- a/update/main.go +++ /dev/null @@ -1,148 +0,0 @@ -/* Update package is used to check if a new version of katenary is available.*/ -package update - -import ( - "encoding/json" - "errors" - "fmt" - "io" - "net/http" - "os" - "runtime" - "strings" - "time" - - "golang.org/x/mod/semver" -) - -var ( - exe, _ = os.Executable() - 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 - 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 || strings.Contains(release.TagName, "rc") { - 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 - c := semver.Compare(Version, release.TagName) - if c >= 0 { - 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 { - defer func() { - if r := recover(); r != nil { - os.Rename(exe+".old", exe) - } - }() - - // 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, 0o755) - if err != nil { - return err - } - defer fp.Close() - _, err = io.Copy(fp, resp.Body) - return err -} diff --git a/update/update_test.go b/update/update_test.go deleted file mode 100644 index 3b391d7..0000000 --- a/update/update_test.go +++ /dev/null @@ -1,51 +0,0 @@ -package update - -// TODO: fix this tests, and possibly the update function - -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.Logf("Error getting latest version: %s", err) - } - - fmt.Println("Version found", version) - - // Touch exe binary - f, _ := os.OpenFile(exe, os.O_RDONLY|os.O_CREATE, 0o755) - f.Write(nil) - f.Close() - - err = DownloadLatestVersion(assets) - if err != nil { - t.Logf("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.Logf("Error: %v", err) - } - - t.Log("Version is already the most recent", version) -}