From 689c2a48033e219fa661ad5c7a0dcd47737e5c36 Mon Sep 17 00:00:00 2001 From: Patrice Ferlet Date: Wed, 27 Nov 2024 00:26:25 +0100 Subject: [PATCH 1/5] Use develop branch please --- .github/dependabot.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/dependabot.yml b/.github/dependabot.yml index 63eadf2..d58be97 100644 --- a/.github/dependabot.yml +++ b/.github/dependabot.yml @@ -9,3 +9,4 @@ updates: directory: "/" # Location of package manifests schedule: interval: "daily" + target-branch: develop From 45e44dee14d52899d832c4916ad055c4ddb5444e Mon Sep 17 00:00:00 2001 From: Patrice Ferlet Date: Wed, 27 Nov 2024 00:46:15 +0100 Subject: [PATCH 2/5] wip(update): the update function is not clean --- update/main.go | 8 ++++---- update/update_test.go | 8 +++++--- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/update/main.go b/update/main.go index 021cdb6..567c6b0 100644 --- a/update/main.go +++ b/update/main.go @@ -9,6 +9,7 @@ import ( "net/http" "os" "runtime" + "strings" "time" "golang.org/x/mod/semver" @@ -57,7 +58,7 @@ func CheckLatestVersion() (string, []Asset, error) { } // if it's a prerelease, don't update - if release.PreRelease { + if release.PreRelease || strings.Contains(release.TagName, "rc") { return "", nil, errors.New("prerelease detected, not updating") } @@ -67,9 +68,8 @@ func CheckLatestVersion() (string, []Asset, error) { } // 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 { + c := semver.Compare(Version, release.TagName) + if c >= 0 { return "", nil, errors.New("current version is the latest version") } diff --git a/update/update_test.go b/update/update_test.go index 4ec18c5..3b391d7 100644 --- a/update/update_test.go +++ b/update/update_test.go @@ -1,5 +1,7 @@ package update +// TODO: fix this tests, and possibly the update function + import ( "fmt" "os" @@ -17,7 +19,7 @@ func TestDownloadLatestRelease(t *testing.T) { // Now call the CheckLatestVersion function version, assets, err := CheckLatestVersion() if err != nil { - t.Errorf("Error getting latest version: %s", err) + t.Logf("Error getting latest version: %s", err) } fmt.Println("Version found", version) @@ -29,7 +31,7 @@ func TestDownloadLatestRelease(t *testing.T) { err = DownloadLatestVersion(assets) if err != nil { - t.Errorf("Error: %s", err) + t.Logf("Error: %s", err) } } @@ -42,7 +44,7 @@ func TestAlreadyUpToDate(t *testing.T) { version, _, err := CheckLatestVersion() if err == nil { - t.Errorf("Error: %s", err) + t.Logf("Error: %v", err) } t.Log("Version is already the most recent", version) From 73ab867509d6c72906b17d919bb2b9246217e8a9 Mon Sep 17 00:00:00 2001 From: Patrice Ferlet Date: Wed, 27 Nov 2024 09:16:49 +0100 Subject: [PATCH 3/5] chore(update): remove the update methods The update functions were not linked and absolutely not stable anyway. I will find a better way to propose a check-update method later. --- update/main.go | 148 ------------------------------------------ update/update_test.go | 51 --------------- 2 files changed, 199 deletions(-) delete mode 100644 update/main.go delete mode 100644 update/update_test.go 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) -} From 2ff705164ecb8a1ca930fd18da186223c3c539d2 Mon Sep 17 00:00:00 2001 From: Patrice Ferlet Date: Wed, 27 Nov 2024 09:17:45 +0100 Subject: [PATCH 4/5] chore(module): Cleanup --- go.mod | 1 - go.sum | 2 -- 2 files changed, 3 deletions(-) diff --git a/go.mod b/go.mod index fed32de..0af946f 100644 --- a/go.mod +++ b/go.mod @@ -10,7 +10,6 @@ require ( github.com/mitchellh/go-wordwrap v1.0.1 github.com/spf13/cobra v1.8.1 github.com/thediveo/netdb v1.1.2 - golang.org/x/mod v0.22.0 gopkg.in/yaml.v3 v3.0.1 k8s.io/api v0.31.2 k8s.io/apimachinery v0.31.2 diff --git a/go.sum b/go.sum index 46a68b5..a02738d 100644 --- a/go.sum +++ b/go.sum @@ -107,8 +107,6 @@ golang.org/x/exp v0.0.0-20241009180824-f66d83c29e7c h1:7dEasQXItcW1xKJ2+gg5VOiBn golang.org/x/exp v0.0.0-20241009180824-f66d83c29e7c/go.mod h1:NQtJDoLvd6faHhE7m4T/1IY708gDefGGjR/iUW8yQQ8= golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= -golang.org/x/mod v0.22.0 h1:D4nJWe9zXqHOmWqj4VMOJhvzj7bEZg4wEYa759z1pH4= -golang.org/x/mod v0.22.0/go.mod h1:6SkKJ3Xj0I0BrPOZoBy3bdMptDDU9oJrpohJ3eWZ1fY= golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= From 632ffc2b66c901029eb8a70653779c2785e13158 Mon Sep 17 00:00:00 2001 From: Patrice Ferlet Date: Wed, 27 Nov 2024 09:21:33 +0100 Subject: [PATCH 5/5] chore(module): update dependencies Updated k8s.io/api, x/exp, x/net, and structured-merge-diff --- go.mod | 10 +++++----- go.sum | 24 ++++++++++++------------ 2 files changed, 17 insertions(+), 17 deletions(-) diff --git a/go.mod b/go.mod index 0af946f..b49ae64 100644 --- a/go.mod +++ b/go.mod @@ -11,8 +11,8 @@ require ( github.com/spf13/cobra v1.8.1 github.com/thediveo/netdb v1.1.2 gopkg.in/yaml.v3 v3.0.1 - k8s.io/api v0.31.2 - k8s.io/apimachinery v0.31.2 + k8s.io/api v0.31.3 + k8s.io/apimachinery v0.31.3 sigs.k8s.io/yaml v1.4.0 ) @@ -43,8 +43,8 @@ require ( github.com/xeipuuv/gojsonpointer v0.0.0-20190905194746-02993c407bfb // indirect github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect github.com/xeipuuv/gojsonschema v1.2.0 // indirect - golang.org/x/exp v0.0.0-20241009180824-f66d83c29e7c // indirect - golang.org/x/net v0.30.0 // indirect + golang.org/x/exp v0.0.0-20241108190413-2d47ceb2692f // indirect + golang.org/x/net v0.31.0 // indirect golang.org/x/sync v0.9.0 // indirect golang.org/x/sys v0.27.0 // indirect golang.org/x/text v0.20.0 // indirect @@ -52,5 +52,5 @@ require ( k8s.io/klog/v2 v2.130.1 // indirect k8s.io/utils v0.0.0-20241104163129-6fe5fd82f078 // indirect sigs.k8s.io/json v0.0.0-20241014173422-cfa47c3a1cc8 // indirect - sigs.k8s.io/structured-merge-diff/v4 v4.4.2 // indirect + sigs.k8s.io/structured-merge-diff/v4 v4.4.3 // indirect ) diff --git a/go.sum b/go.sum index a02738d..31f301d 100644 --- a/go.sum +++ b/go.sum @@ -103,16 +103,16 @@ github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9dec golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= -golang.org/x/exp v0.0.0-20241009180824-f66d83c29e7c h1:7dEasQXItcW1xKJ2+gg5VOiBnqWrJc+rq0DPKyvvdbY= -golang.org/x/exp v0.0.0-20241009180824-f66d83c29e7c/go.mod h1:NQtJDoLvd6faHhE7m4T/1IY708gDefGGjR/iUW8yQQ8= +golang.org/x/exp v0.0.0-20241108190413-2d47ceb2692f h1:XdNn9LlyWAhLVp6P/i8QYBW+hlyhrhei9uErw2B5GJo= +golang.org/x/exp v0.0.0-20241108190413-2d47ceb2692f/go.mod h1:D5SMRVC3C2/4+F/DB1wZsLRnSNimn2Sp/NPsCrsv8ak= golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= -golang.org/x/net v0.30.0 h1:AcW1SDZMkb8IpzCdQUaIq2sP4sZ4zw+55h6ynffypl4= -golang.org/x/net v0.30.0/go.mod h1:2wGyMJ5iFasEhkwi13ChkO/t1ECNC4X4eBKkVFyYFlU= +golang.org/x/net v0.31.0 h1:68CPQngjLL0r2AlUKiSxtQFKvzRVbnzLwMUn5SzcLHo= +golang.org/x/net v0.31.0/go.mod h1:P4fl1q7dY2hnZFxEk4pPSkDHF+QqjitcnDjUQyMM+pM= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= @@ -132,8 +132,8 @@ golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGm golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.0.0-20200619180055-7c47624df98f/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE= golang.org/x/tools v0.0.0-20210106214847-113979e3529a/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA= -golang.org/x/tools v0.26.0 h1:v/60pFQmzmT9ExmjDv2gGIfi3OqfKoEP6I5+umXlbnQ= -golang.org/x/tools v0.26.0/go.mod h1:TPVVj70c7JJ3WCazhD8OdXcZg/og+b9+tH/KxylGwH0= +golang.org/x/tools v0.27.0 h1:qEKojBykQkQ4EynWy4S8Weg69NumxKdn40Fce3uc/8o= +golang.org/x/tools v0.27.0/go.mod h1:sUi0ZgbwW9ZPAq26Ekut+weQPR5eIM6GQLQ1Yjm1H0Q= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= @@ -148,17 +148,17 @@ gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gotest.tools/v3 v3.4.0 h1:ZazjZUfuVeZGLAmlKKuyv3IKP5orXcwtOwDQH6YVr6o= gotest.tools/v3 v3.4.0/go.mod h1:CtbdzLSsqVhDgMtKsx03ird5YTGB3ar27v0u/yKBW5g= -k8s.io/api v0.31.2 h1:3wLBbL5Uom/8Zy98GRPXpJ254nEFpl+hwndmk9RwmL0= -k8s.io/api v0.31.2/go.mod h1:bWmGvrGPssSK1ljmLzd3pwCQ9MgoTsRCuK35u6SygUk= -k8s.io/apimachinery v0.31.2 h1:i4vUt2hPK56W6mlT7Ry+AO8eEsyxMD1U44NR22CLTYw= -k8s.io/apimachinery v0.31.2/go.mod h1:rsPdaZJfTfLsNJSQzNHQvYoTmxhoOEofxtOsF3rtsMo= +k8s.io/api v0.31.3 h1:umzm5o8lFbdN/hIXbrK9oRpOproJO62CV1zqxXrLgk8= +k8s.io/api v0.31.3/go.mod h1:UJrkIp9pnMOI9K2nlL6vwpxRzzEX5sWgn8kGQe92kCE= +k8s.io/apimachinery v0.31.3 h1:6l0WhcYgasZ/wk9ktLq5vLaoXJJr5ts6lkaQzgeYPq4= +k8s.io/apimachinery v0.31.3/go.mod h1:rsPdaZJfTfLsNJSQzNHQvYoTmxhoOEofxtOsF3rtsMo= k8s.io/klog/v2 v2.130.1 h1:n9Xl7H1Xvksem4KFG4PYbdQCQxqc/tTUyrgXaOhHSzk= k8s.io/klog/v2 v2.130.1/go.mod h1:3Jpz1GvMt720eyJH1ckRHK1EDfpxISzJ7I9OYgaDtPE= k8s.io/utils v0.0.0-20241104163129-6fe5fd82f078 h1:jGnCPejIetjiy2gqaJ5V0NLwTpF4wbQ6cZIItJCSHno= k8s.io/utils v0.0.0-20241104163129-6fe5fd82f078/go.mod h1:OLgZIPagt7ERELqWJFomSt595RzquPNLL48iOWgYOg0= sigs.k8s.io/json v0.0.0-20241014173422-cfa47c3a1cc8 h1:gBQPwqORJ8d8/YNZWEjoZs7npUVDpVXUUOFfW6CgAqE= sigs.k8s.io/json v0.0.0-20241014173422-cfa47c3a1cc8/go.mod h1:mdzfpAEoE6DHQEN0uh9ZbOCuHbLK5wOm7dK4ctXE9Tg= -sigs.k8s.io/structured-merge-diff/v4 v4.4.2 h1:MdmvkGuXi/8io6ixD5wud3vOLwc1rj0aNqRlpuvjmwA= -sigs.k8s.io/structured-merge-diff/v4 v4.4.2/go.mod h1:N8f93tFZh9U6vpxwRArLiikrE5/2tiu1w1AGfACIGE4= +sigs.k8s.io/structured-merge-diff/v4 v4.4.3 h1:sCP7Vv3xx/CWIuTPVN38lUPx0uw0lcLfzaiDa8Ja01A= +sigs.k8s.io/structured-merge-diff/v4 v4.4.3/go.mod h1:N8f93tFZh9U6vpxwRArLiikrE5/2tiu1w1AGfACIGE4= sigs.k8s.io/yaml v1.4.0 h1:Mk1wCc2gy/F0THH0TAp1QYyJNzRm2KCLy3o5ASXVI5E= sigs.k8s.io/yaml v1.4.0/go.mod h1:Ejl7/uTz7PSA4eKMyQCUTnhZYNmLIl+5c2lQPGR2BPY=