Skip to content

Commit

Permalink
fix: k3d version ls (now via crane) (#1286)
Browse files Browse the repository at this point in the history
  • Loading branch information
iwilltry42 authored May 17, 2023
1 parent c511600 commit 2d5db00
Show file tree
Hide file tree
Showing 70 changed files with 13,472 additions and 37 deletions.
60 changes: 28 additions & 32 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,8 @@ package cmd

import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"regexp"
"sort"
Expand All @@ -37,6 +35,7 @@ import (
"github.com/sirupsen/logrus/hooks/writer"
"github.com/spf13/cobra"

"github.com/google/go-containerregistry/pkg/crane"
"github.com/k3d-io/k3d/v5/cmd/cluster"
cfg "github.com/k3d-io/k3d/v5/cmd/config"
"github.com/k3d-io/k3d/v5/cmd/debug"
Expand Down Expand Up @@ -254,6 +253,13 @@ func NewCmdVersionLs() *cobra.Command {
string(VersionLsSortOff): VersionLsSortOff,
}

var imageRepos map[string]string = map[string]string{
"k3d": "ghcr.io/k3d-io/k3d",
"k3d-tools": "ghcr.io/k3d-io/k3d-tools",
"k3d-proxy": "ghcr.io/k3d-io/k3d-proxy",
"k3s": "docker.io/rancher/k3s",
}

type Flags struct {
includeRegexp string
excludeRegexp string
Expand All @@ -271,6 +277,12 @@ func NewCmdVersionLs() *cobra.Command {
ValidArgs: []string{"k3d", "k3s", "k3d-proxy", "k3d-tools"},
Args: cobra.MatchAll(cobra.ExactArgs(1), cobra.OnlyValidArgs),
Run: func(cmd *cobra.Command, args []string) {

repo, ok := imageRepos[args[0]]
if !ok {
l.Log().Fatalf("Unknown target '%s'", args[0])
}

var format VersionLsOutputFormat
if f, ok := VersionLsOutputFormats[flags.format]; !ok {
l.Log().Fatalf("Unknown output format '%s'", flags.format)
Expand All @@ -285,26 +297,10 @@ func NewCmdVersionLs() *cobra.Command {
sortMode = m
}

urlTpl := "https://registry.hub.docker.com/v1/repositories/%s/tags"
org := "rancher"
repo := fmt.Sprintf("%s/%s", org, args[0])
resp, err := http.Get(fmt.Sprintf(urlTpl, repo))
tags, err := crane.ListTags(repo)
if err != nil {
l.Log().Fatalln(err)
}
defer resp.Body.Close()
type Layers struct {
Layer string
Name string
}
body, err := io.ReadAll(resp.Body)
if err != nil {
l.Log().Fatalln(err)
}
respJSON := &[]Layers{}
if err := json.Unmarshal(body, respJSON); err != nil {
l.Log().Fatalln(err)
}

includeRegexp, err := regexp.Compile(flags.includeRegexp)
if err != nil {
Expand All @@ -316,41 +312,41 @@ func NewCmdVersionLs() *cobra.Command {
l.Log().Fatalln(err)
}

tags := []string{}
filteredTags := []string{}

for _, tag := range *respJSON {
if includeRegexp.Match([]byte(tag.Name)) {
if flags.excludeRegexp == "" || !excludeRegexp.Match([]byte(tag.Name)) {
for _, tag := range tags {
if includeRegexp.Match([]byte(tag)) {
if flags.excludeRegexp == "" || !excludeRegexp.Match([]byte(tag)) {
switch format {
case VersionLsOutputFormatRaw:
tags = append(tags, tag.Name)
filteredTags = append(filteredTags, tag)
case VersionLsOutputFormatRepo:
tags = append(tags, fmt.Sprintf("%s:%s\n", repo, tag.Name))
filteredTags = append(filteredTags, fmt.Sprintf("%s:%s\n", repo, tag))
default:
l.Log().Fatalf("Unknown output format '%+v'", format)
}
} else {
l.Log().Tracef("Tag %s excluded (regexp: `%s`)", tag.Name, flags.excludeRegexp)
l.Log().Tracef("Tag %s excluded (regexp: `%s`)", tag, flags.excludeRegexp)
}
} else {
l.Log().Tracef("Tag %s not included (regexp: `%s`)", tag.Name, flags.includeRegexp)
l.Log().Tracef("Tag %s not included (regexp: `%s`)", tag, flags.includeRegexp)
}
}

// Sort
if sortMode != VersionLsSortOff {
sort.Slice(tags, func(i, j int) bool {
sort.Slice(filteredTags, func(i, j int) bool {
if sortMode == VersionLsSortAsc {
return tags[i] < tags[j]
return filteredTags[i] < filteredTags[j]
}
return tags[i] > tags[j]
return filteredTags[i] > filteredTags[j]
})
}

if flags.limit > 0 {
tags = tags[0:flags.limit]
filteredTags = filteredTags[0:flags.limit]
}
fmt.Println(strings.Join(tags, "\n"))
fmt.Println(strings.Join(filteredTags, "\n"))

},
}
Expand Down
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ require (

require (
github.com/goodhosts/hostsfile v0.1.1
github.com/google/go-containerregistry v0.12.2-0.20230106184643-b063f6aeac72
github.com/rancher/wharfie v0.6.1
github.com/spf13/pflag v1.0.5
k8s.io/utils v0.0.0-20230406110748-d93618cff8a2
Expand All @@ -50,6 +51,7 @@ require (
github.com/asaskevich/govalidator v0.0.0-20210307081110-f21760c49a8d // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/containerd/stargz-snapshotter/estargz v0.12.1 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dimchansky/utfbom v1.1.1 // indirect
github.com/docker/distribution v2.8.1+incompatible // indirect
Expand All @@ -59,7 +61,6 @@ require (
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/google/go-containerregistry v0.12.2-0.20230106184643-b063f6aeac72 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/gorilla/mux v1.8.0 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
Expand All @@ -86,6 +87,7 @@ require (
github.com/spf13/cast v1.5.0 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/subosito/gotenv v1.4.2 // indirect
github.com/vbatts/tar-split v0.11.2 // indirect
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect
go.uber.org/atomic v1.9.0 // indirect
go.uber.org/multierr v1.8.0 // indirect
Expand Down
8 changes: 8 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,9 @@ github.com/containerd/containerd v1.7.0 h1:G/ZQr3gMZs6ZT0qPUZ15znx5QSdQdASW11nXT
github.com/containerd/containerd v1.7.0/go.mod h1:QfR7Efgb/6X2BDpTPJRvPTYDE9rsF0FsXX9J8sIs/sc=
github.com/containerd/continuity v0.3.0 h1:nisirsYROK15TAMVukJOUyGJjz4BNQJBVsNvAXZJ/eg=
github.com/containerd/stargz-snapshotter/estargz v0.12.1 h1:+7nYmHJb0tEkcRaAW+MHqoKaJYZmkikupxCqVtmPuY0=
github.com/containerd/stargz-snapshotter/estargz v0.12.1/go.mod h1:12VUuCq3qPq4y8yUW+l5w3+oXV3cx2Po3KSe/SmPGqw=
github.com/corpix/uarand v0.1.1 h1:RMr1TWc9F4n5jiPDzFHtmaUXLKLNUFK0SgCLo4BhX/U=
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/creack/pty v1.1.18 h1:n56/Zwd5o6whRC5PMGretI4IdRLlmBXYNjScPaBgsbY=
Expand Down Expand Up @@ -253,6 +255,7 @@ github.com/juju/loggo v0.0.0-20190526231331-6e530bcce5d8/go.mod h1:vgyd7OREkbtVE
github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w=
github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/klauspost/compress v1.15.11/go.mod h1:QPwzmACJjUTFsnSHH934V6woptycfrDDJnH7hvFVbGM=
github.com/klauspost/compress v1.16.0 h1:iULayQNOReoYUe+1qtKOqw9CwJv3aNQu8ivo7lw1HU4=
github.com/klauspost/compress v1.16.0/go.mod h1:ntbaceVETuRiXiv4DpjP66DpAtAGkEQskQzEyD//IeE=
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
Expand Down Expand Up @@ -356,10 +359,13 @@ github.com/rancher/wharfie v0.6.1 h1:IBC/ix0nugVP/5rsra6ujsZ0DIQjD9qccYntA4t2FTM
github.com/rancher/wharfie v0.6.1/go.mod h1:2UObJVP+UAvHqOGG7JWWCnOfFehS72+hI+4CYiiPsc8=
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/rogpeppe/go-internal v1.6.1 h1:/FiVV8dS/e+YqF2JvO3yXRFbBLTIuSDkuC7aBOAvL+k=
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
github.com/sirupsen/logrus v1.0.6/go.mod h1:pMByvHTf9Beacp5x1UXfOR9xyW/9antXMhjMPG0dEzc=
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
github.com/sirupsen/logrus v1.4.1/go.mod h1:ni0Sbl8bgC9z8RoU9G6nDWqqs/fq4eDPysMBDgk/93Q=
github.com/sirupsen/logrus v1.7.0/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
github.com/sirupsen/logrus v1.9.0 h1:trlNQbNUG3OdDrDil03MCb1H2o9nJ1x4/5LYw7byDE0=
github.com/sirupsen/logrus v1.9.0/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/spf13/afero v1.9.3 h1:41FoI0fD7OR7mGcKE/aOiLkGreyf8ifIOQmJANWogMk=
Expand Down Expand Up @@ -397,7 +403,9 @@ github.com/subosito/gotenv v1.4.2 h1:X1TuBLAMDFbaTAChgCBLu3DU3UPyELpnF2jjJ2cz/S8
github.com/subosito/gotenv v1.4.2/go.mod h1:ayKnFf/c6rvx/2iiLrJUk1e6plDbT3edrFNGqEflhK0=
github.com/theupdateframework/notary v0.7.0 h1:QyagRZ7wlSpjT5N2qQAh/pN+DVqgekv4DzbAiAiEL3c=
github.com/theupdateframework/notary v0.7.0/go.mod h1:c9DRxcmhHmVLDay4/2fUYdISnHqbFDGRSlXPO0AhYWw=
github.com/urfave/cli v1.22.4/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0=
github.com/vbatts/tar-split v0.11.2 h1:Via6XqJr0hceW4wff3QRzD5gAk/tatMw/4ZA7cTlIME=
github.com/vbatts/tar-split v0.11.2/go.mod h1:vV3ZuO2yWSVsz+pfFzDG/upWH1JhjOiEaWq6kXyQ3VI=
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f/go.mod h1:N2zxlSyiKSe5eX1tZViRH5QA0qijqEDrYZiPEAiq3wU=
github.com/xeipuuv/gojsonpointer v0.0.0-20190905194746-02993c407bfb h1:zGWFAtiMcyryUHoUjUJX0/lt1H2+i2Ka2n+D3DImSNo=
github.com/xeipuuv/gojsonpointer v0.0.0-20190905194746-02993c407bfb/go.mod h1:N2zxlSyiKSe5eX1tZViRH5QA0qijqEDrYZiPEAiq3wU=
Expand Down
Loading

0 comments on commit 2d5db00

Please sign in to comment.