Skip to content

Commit 582579d

Browse files
kravciakiwilltry42
authored andcommitted
Use version sort for k3d version list output
Signed-off-by: Martin Kravec <[email protected]>
1 parent e5cdcfd commit 582579d

File tree

8 files changed

+464
-4
lines changed

8 files changed

+464
-4
lines changed

cmd/root.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import (
3535
"github.com/sirupsen/logrus"
3636
"github.com/sirupsen/logrus/hooks/writer"
3737
"github.com/spf13/cobra"
38+
"golang.org/x/mod/semver"
3839

3940
"github.com/google/go-containerregistry/pkg/crane"
4041
"github.com/k3d-io/k3d/v5/cmd/cluster"
@@ -369,10 +370,13 @@ func NewCmdVersionLs() *cobra.Command {
369370
// Sort
370371
if sortMode != VersionLsSortOff {
371372
sort.Slice(filteredTags, func(i, j int) bool {
373+
// Remove the "v" prefix for comparison, only k3s images have prefix
374+
vi, vj := strings.TrimPrefix(filteredTags[i], "v"), strings.TrimPrefix(filteredTags[j], "v")
375+
372376
if sortMode == VersionLsSortAsc {
373-
return filteredTags[i] < filteredTags[j]
377+
return semver.Compare("v"+vi, "v"+vj) < 0
374378
}
375-
return filteredTags[i] > filteredTags[j]
379+
return semver.Compare("v"+vi, "v"+vj) > 0
376380
})
377381
}
378382

@@ -384,7 +388,7 @@ func NewCmdVersionLs() *cobra.Command {
384388
}
385389

386390
cmd.Flags().StringVarP(&flags.includeRegexp, "include", "i", ".*", "Include Regexp (default includes everything")
387-
cmd.Flags().StringVarP(&flags.excludeRegexp, "exclude", "e", ".+(rc|engine|alpha|beta|dev|test|arm|arm64|amd64|s390x).*", "Exclude Regexp (default excludes pre-releases and arch-specific tags)")
391+
cmd.Flags().StringVarP(&flags.excludeRegexp, "exclude", "e", "^sha-|.+(rc|engine|dind|alpha|beta|dev|test|arm|arm64|amd64|s390x).*", "Exclude Regexp (default excludes pre-releases, arch-specific tags and digests)")
388392
cmd.Flags().StringVarP(&flags.format, "format", "f", string(VersionLsOutputFormatRaw), "[DEPRECATED] Use --output instead")
389393
cmd.Flags().StringVarP(&flags.outputFormat, "output", "o", string(VersionLsOutputFormatRaw), "Output Format [raw | repo]")
390394
cmd.MarkFlagsMutuallyExclusive("format", "output")

docs/usage/commands/k3d_version_list.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ k3d version list COMPONENT [flags]
99
### Options
1010

1111
```
12-
-e, --exclude string Exclude Regexp (default excludes pre-releases and arch-specific tags) (default ".+(rc|engine|alpha|beta|dev|test|arm|arm64|amd64|s390x).*")
12+
-e, --exclude string Exclude Regexp (default excludes pre-releases, arch-specific tags and digests) (default "^sha-|.+(rc|engine|dind|alpha|beta|dev|test|arm|arm64|amd64|s390x).*")
1313
-f, --format string [DEPRECATED] Use --output instead (default "raw")
1414
-h, --help help for list
1515
-i, --include string Include Regexp (default includes everything (default ".*")

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ require (
99
github.com/spf13/pflag v1.0.5
1010
github.com/stretchr/testify v1.9.0
1111
go4.org/netipx v0.0.0-20231129151722-fdeea329fbba
12+
golang.org/x/mod v0.17.0
1213
gopkg.in/yaml.v3 v3.0.1
1314
k8s.io/utils v0.0.0-20240502163921-fe8a2dddb1d0
1415
)

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,8 @@ golang.org/x/exp v0.0.0-20230905200255-921286631fa9 h1:GoHiUyI/Tp2nVkLI2mCxVkOjs
383383
golang.org/x/exp v0.0.0-20230905200255-921286631fa9/go.mod h1:S2oDrQGGwySpoQPVqRShND87VCbxmc6bL1Yd2oYrm6k=
384384
golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
385385
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
386+
golang.org/x/mod v0.17.0 h1:zY54UmvipHiNd+pm+m0x9KhZ9hl1/7QNMyxXbc6ICqA=
387+
golang.org/x/mod v0.17.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
386388
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
387389
golang.org/x/net v0.0.0-20181114220301-adae6a3d119a/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
388390
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=

vendor/golang.org/x/mod/LICENSE

Lines changed: 27 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/golang.org/x/mod/PATENTS

Lines changed: 22 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)