From f405bf002ab21fe38bfaee0e40664006dcac2aaf Mon Sep 17 00:00:00 2001 From: Rick <1450685+LinuxSuRen@users.noreply.github.com> Date: Wed, 18 May 2022 09:50:23 +0800 Subject: [PATCH] Support autocompletion of the version in k3d installation (#260) --- go.mod | 3 ++- go.sum | 4 ++-- kubectl-plugin/install/k3d.go | 18 ++++++++++++++++++ 3 files changed, 22 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index e241f3d..1232bef 100644 --- a/go.mod +++ b/go.mod @@ -11,9 +11,10 @@ require ( github.com/evanphx/json-patch v4.9.0+incompatible github.com/gdamore/tcell/v2 v2.4.1-0.20210905002822-f057f0a857a1 github.com/go-git/go-git/v5 v5.4.2 + github.com/google/go-github/v29 v29.0.3 github.com/huandu/xstrings v1.3.2 // indirect github.com/jenkins-x/go-scm v1.11.1 - github.com/linuxsuren/cobra-extension v0.0.13 + github.com/linuxsuren/cobra-extension v0.0.15 github.com/linuxsuren/go-cli-alias v0.0.10 github.com/linuxsuren/http-downloader v0.0.35 github.com/mitchellh/copystructure v1.1.1 // indirect diff --git a/go.sum b/go.sum index 9ac4014..cb4adc9 100644 --- a/go.sum +++ b/go.sum @@ -378,8 +378,8 @@ github.com/liggitt/tabwriter v0.0.0-20181228230101-89fcab3d43de/go.mod h1:zAbeS9 github.com/linuxsuren/cobra-extension v0.0.6/go.mod h1:qcEJv7BbL0UpK6MbrTESP/nKf1+z1wQdMAnE1NBl3QQ= github.com/linuxsuren/cobra-extension v0.0.10/go.mod h1:nDsXgvm0lSWVV+byAEfwhIGFDoIp0Sq9wkfNUDBp5do= github.com/linuxsuren/cobra-extension v0.0.11/go.mod h1:gn69Dtq72JCGZbjW46c14MoE0z/BtDAQWV9Jvr9KczY= -github.com/linuxsuren/cobra-extension v0.0.13 h1:nqqvZMeyfhxSHXeCjejg4yR9kRFP+0R79R8lZwmpevs= -github.com/linuxsuren/cobra-extension v0.0.13/go.mod h1:gn69Dtq72JCGZbjW46c14MoE0z/BtDAQWV9Jvr9KczY= +github.com/linuxsuren/cobra-extension v0.0.15 h1:uEsn4U+W3XBCFuhyHdkzbVobDR5DD/qsyZZsSZ5H7YU= +github.com/linuxsuren/cobra-extension v0.0.15/go.mod h1:gn69Dtq72JCGZbjW46c14MoE0z/BtDAQWV9Jvr9KczY= github.com/linuxsuren/go-cli-alias v0.0.10 h1:qzaz9RiTVKIWryxw3Hx0q//wjt+VIP+BElgRlSWrDhU= github.com/linuxsuren/go-cli-alias v0.0.10/go.mod h1:g+k4hlbE/lilrEq/5emhMznb9my/lOXSH++/mPli6gw= github.com/linuxsuren/http-downloader v0.0.2-0.20201207132639-19888a6beaec/go.mod h1:zRZY9FCDBuYNDxbI2Ny5suasZsMk7J6q9ecQ3V3PIqI= diff --git a/kubectl-plugin/install/k3d.go b/kubectl-plugin/install/k3d.go index e3f587e..baec357 100644 --- a/kubectl-plugin/install/k3d.go +++ b/kubectl-plugin/install/k3d.go @@ -4,8 +4,10 @@ import ( "bytes" "fmt" "github.com/Masterminds/semver" + "github.com/google/go-github/v29/github" "github.com/kubesphere-sigs/ks/kubectl-plugin/common" "github.com/kubesphere-sigs/ks/kubectl-plugin/types" + gh "github.com/linuxsuren/cobra-extension/github" "github.com/linuxsuren/http-downloader/pkg/installer" "github.com/spf13/cobra" "io/ioutil" @@ -64,6 +66,22 @@ You can get more details from https://github.com/rancher/k3d/`, "rancher/k3s:v1.21.4-k3s1")) _ = cmd.RegisterFlagCompletionFunc("components", common.PluginAbleComponentsCompletion()) _ = cmd.RegisterFlagCompletionFunc("nightly", common.ArrayCompletion("latest")) + _ = cmd.RegisterFlagCompletionFunc("version", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { + return getKubeSphereVersionList(), cobra.ShellCompDirectiveNoFileComp + }) + return +} + +func getKubeSphereVersionList() (tags []string) { + ghClient := &gh.ReleaseClient{ + Client: github.NewClient(nil), + } + + if tagList, err := ghClient.GetTagList("kubesphere", "kubesphere", 6); err == nil && tagList != nil { + for i := range tagList { + tags = append(tags, tagList[i].Name) + } + } return }