From c78fee6016b8d50f06e442a0d76b512ab9859fab Mon Sep 17 00:00:00 2001 From: Itxaka Date: Thu, 20 Apr 2023 09:57:58 +0200 Subject: [PATCH] bug: Fix version list (#1323) * :bug: Fix version list First version in the list is the latest one. Alos moves the check for same version above the current place, so it can check before asking if you want to update to the same version Signed-off-by: Itxaka * :seedling: Rework versioning for upgrade Use the semver lib to parse the versions into a proper collection where it can be parsed and versions compared and sorted properly Signed-off-by: Itxaka * :robot: lint Signed-off-by: Itxaka --------- Signed-off-by: Itxaka --- cmd/agent/main.go | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/cmd/agent/main.go b/cmd/agent/main.go index d935cca..c9aa1c7 100644 --- a/cmd/agent/main.go +++ b/cmd/agent/main.go @@ -2,8 +2,11 @@ package main import ( "context" + "encoding/json" "fmt" + "os" + "strings" "github.com/kairos-io/kairos/v2/internal/agent" "github.com/kairos-io/kairos/v2/internal/bus" @@ -12,16 +15,37 @@ import ( "github.com/kairos-io/kairos-sdk/bundles" "github.com/kairos-io/kairos-sdk/machine" "github.com/kairos-io/kairos-sdk/state" - "github.com/kairos-io/kairos-sdk/utils" "github.com/kairos-io/kairos/v2/internal/common" "github.com/kairos-io/kairos/v2/pkg/config" "github.com/kairos-io/kairos/v2/pkg/config/collector" + "github.com/Masterminds/semver/v3" "github.com/urfave/cli/v2" + "gopkg.in/yaml.v3" ) var configScanDir = []string{"/oem", "/usr/local/cloud-config", "/run/initramfs/live"} +// ReleasesToOutput gets a semver.Collection and outputs it in the given format +// Only used here. +func ReleasesToOutput(rels semver.Collection, output string) []string { + // Set them back to their original version number with the v in front + var stringRels []string + for _, v := range rels { + stringRels = append(stringRels, v.Original()) + } + switch strings.ToLower(output) { + case "yaml": + d, _ := yaml.Marshal(stringRels) + return []string{string(d)} + case "json": + d, _ := json.Marshal(stringRels) + return []string{string(d)} + default: + return stringRels + } +} + var cmds = []*cli.Command{ { Name: "upgrade", @@ -71,7 +95,7 @@ See https://kairos.io/docs/upgrade/manual/ for documentation. Description: `List all available releases versions`, Action: func(c *cli.Context) error { releases := agent.ListReleases() - list := utils.ListToOutput(releases, c.String("output")) + list := ReleasesToOutput(releases, c.String("output")) for _, i := range list { fmt.Println(i) }