Skip to content

Commit

Permalink
feature: add 'k3d version ls [k3d|k3s|k3d-proxy|k3d-tools]' command t…
Browse files Browse the repository at this point in the history
…o list tags
  • Loading branch information
iwilltry42 committed Nov 23, 2021
1 parent d78ef48 commit c260b7d
Show file tree
Hide file tree
Showing 18 changed files with 344 additions and 227 deletions.
131 changes: 121 additions & 10 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,12 @@ package cmd

import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"regexp"
"strings"

"github.com/spf13/cobra"
Expand Down Expand Up @@ -85,22 +88,16 @@ All Nodes of a k3d cluster are part of the same docker network.`,
rootCmd.Flags().BoolVar(&flags.version, "version", false, "Show k3d and default k3s version")

// add subcommands
rootCmd.AddCommand(NewCmdCompletion(rootCmd),
rootCmd.AddCommand(
NewCmdVersion(),
NewCmdCompletion(rootCmd),
cluster.NewCmdCluster(),
kubeconfig.NewCmdKubeconfig(),
node.NewCmdNode(),
image.NewCmdImage(),
cfg.NewCmdConfig(),
registry.NewCmdRegistry(),
debug.NewCmdDebug(),
&cobra.Command{
Use: "version",
Short: "Show k3d and default k3s version",
Long: "Show k3d and default k3s version",
Run: func(cmd *cobra.Command, args []string) {
printVersion()
},
},
&cobra.Command{
Use: "runtime-info",
Short: "Show runtime information",
Expand All @@ -116,7 +113,8 @@ All Nodes of a k3d cluster are part of the same docker network.`,
}
},
Hidden: true,
})
},
)

// Init
cobra.OnInitialize(initLogging, initRuntime)
Expand Down Expand Up @@ -208,11 +206,124 @@ func initRuntime() {
}
}

func NewCmdVersion() *cobra.Command {
cmd := &cobra.Command{
Use: "version",
Short: "Show k3d and default k3s version",
Long: "Show k3d and default k3s version",
Run: func(cmd *cobra.Command, args []string) {
printVersion()
},
Args: cobra.NoArgs,
}

cmd.AddCommand(NewCmdVersionLs())

return cmd

}

func printVersion() {
fmt.Printf("k3d version %s\n", version.GetVersion())
fmt.Printf("k3s version %s (default)\n", version.K3sVersion)
}

func NewCmdVersionLs() *cobra.Command {

type VersionLsOutputFormat string

const (
VersionLsOutputFormatRaw VersionLsOutputFormat = "raw"
VersionLsOutputFormatRepo VersionLsOutputFormat = "repo"
)

var VersionLsOutputFormats = map[string]VersionLsOutputFormat{
string(VersionLsOutputFormatRaw): VersionLsOutputFormatRaw,
string(VersionLsOutputFormatRepo): VersionLsOutputFormatRepo,
}

type Flags struct {
includeRegexp string
excludeRegexp string
format string
}

flags := Flags{}

cmd := &cobra.Command{
Use: "list",
Aliases: []string{"ls"},
Short: "List k3d/K3s versions",
ValidArgs: []string{"k3d", "k3s", "k3d-proxy", "k3d-tools"},
Args: cobra.ExactValidArgs(1),
Run: func(cmd *cobra.Command, args []string) {
var format VersionLsOutputFormat
if f, ok := VersionLsOutputFormats[flags.format]; !ok {
l.Log().Fatalf("Unknown output format '%s'", flags.format)
} else {
format = f
}

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))
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 {
l.Log().Fatalln(err)
}

excludeRegexp, err := regexp.Compile(flags.excludeRegexp)
if err != nil {
l.Log().Fatalln(err)
}

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

},
}

cmd.Flags().StringVarP(&flags.includeRegexp, "include", "i", ".*", "Include Regexp")
cmd.Flags().StringVarP(&flags.excludeRegexp, "exclude", "e", "", "Exclude Regexp")
cmd.Flags().StringVarP(&flags.format, "format", "f", string(VersionLsOutputFormatRaw), "Output Format")

return cmd
}

// NewCmdCompletion creates a new completion command
func NewCmdCompletion(rootCmd *cobra.Command) *cobra.Command {

Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ require (
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/go-cmp v0.5.6 // indirect
github.com/google/gofuzz v1.1.0 // indirect
github.com/gorilla/mux v1.7.3 // indirect
github.com/gorilla/mux v1.8.0 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/json-iterator/go v1.1.11 // indirect
Expand Down Expand Up @@ -87,7 +87,7 @@ require (
golang.org/x/crypto v0.0.0-20210817164053-32db794688a5 // indirect
golang.org/x/net v0.0.0-20211111160137-58aab5ef257a // indirect
golang.org/x/oauth2 v0.0.0-20210819190943-2bc19b11175f // indirect
golang.org/x/term v0.0.0-20210220032956-6a3ed077a48d // indirect
golang.org/x/term v0.0.0-20210615171337-6886f2dfbf5b // indirect
golang.org/x/time v0.0.0-20210723032227-1f47c861a9ac // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto v0.0.0-20211112145013-271947fe86fd // indirect
Expand Down
6 changes: 4 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -482,8 +482,9 @@ github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORR
github.com/gorilla/handlers v0.0.0-20150720190736-60c7bfde3e33/go.mod h1:Qkdc/uu4tH4g6mTK6auzZ766c4CA0Ng8+o/OAirnOIQ=
github.com/gorilla/mux v1.7.0/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs=
github.com/gorilla/mux v1.7.2/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs=
github.com/gorilla/mux v1.7.3 h1:gnP5JzjVOuiZD07fKKToCAOjS0yOpj/qPETTXCCS6hw=
github.com/gorilla/mux v1.7.3/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs=
github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
github.com/gorilla/websocket v0.0.0-20170926233335-4201258b820c/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ=
github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ=
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
Expand Down Expand Up @@ -1144,8 +1145,9 @@ golang.org/x/sys v0.0.0-20211112143042-c6105e7cf70d h1:jp6PtFmjL+vGsuzd86xYqaJGv
golang.org/x/sys v0.0.0-20211112143042-c6105e7cf70d/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210220032956-6a3ed077a48d h1:SZxvLBoTP5yHO3Frd4z4vrF+DBX9vMVanchswa69toE=
golang.org/x/term v0.0.0-20210220032956-6a3ed077a48d/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210615171337-6886f2dfbf5b h1:9zKuko04nR4gjZ4+DNjHqRlAJqbJETHwiNKDqTfOjfE=
golang.org/x/term v0.0.0-20210615171337-6886f2dfbf5b/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
Expand Down
2 changes: 1 addition & 1 deletion pkg/types/k3s/channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ type Channel struct {

type ChannelServerResponse struct {
Channels []struct {
Channel `json:,squash`
Channel `json:",squash"`
} `json:"data"`
}

Expand Down
91 changes: 89 additions & 2 deletions vendor/github.com/gorilla/mux/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 0 additions & 18 deletions vendor/github.com/gorilla/mux/context.go

This file was deleted.

25 changes: 10 additions & 15 deletions vendor/github.com/gorilla/mux/middleware.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit c260b7d

Please sign in to comment.