Skip to content

Commit

Permalink
loading tags better...
Browse files Browse the repository at this point in the history
  • Loading branch information
mhkarimi1383 committed Oct 26, 2022
1 parent a1acdb6 commit 7b0271e
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 42 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,7 @@ and put the following line into your `.bashrc` or `.zshrc`
export PATH="$HOME/.kvm/bin:$PATH"
```

then grab the package binary and put it into a dir that is covered by `$PATH`
install package itself by running
```shell
go install github.com/mhkarimi1383/kvm@latest
```
92 changes: 54 additions & 38 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,50 +20,66 @@ var rootCmd = &cobra.Command{
Long: `Managing kubectl versions can help you when managing multiple clusters with different versions
and can make you fully compatible with your clusters.`,
Run: func(cmd *cobra.Command, args []string) {
tags, err := helper.GetVersions()
if err != nil {
panic(err)
}
idx, err := fuzzyfinder.Find(tags,
func(i int) string {
return tags[i]
}, fuzzyfinder.WithPreviewWindow(func(i, w, h int) string {
if i == -1 {
return ""
}
return fmt.Sprintf("Change log and more info:\n https://github.com/kubernetes/kubernetes/releases/tag/%s", tags[i])
}))
if err != nil {
panic(err)
}
version := tags[idx]
homePath, err := os.UserHomeDir()
if err != nil {
panic(err)
}
fmt.Printf("selected: %v\n", version)
basePath := homePath + "/.kvm/"
binPath := homePath + "/.kvm/bin/"
if _, err := os.Stat(basePath + "kubectl-" + version); errors.Is(err, os.ErrNotExist) {
path, err := helper.DownloadKubectlBinary(version)
page := 1
for {
tags := []string{
"More...",
}
versionTags, err := helper.GetVersions(page)
tags = append(tags, versionTags...)
if err != nil {
panic(err)
}
err = helper.MoveFile(path, basePath+"kubectl-"+version)
idx, err := fuzzyfinder.Find(tags,
func(i int) string {
return tags[i]
}, fuzzyfinder.WithPreviewWindow(func(i, w, h int) string {
if i == -1 {
return ""
}
if tags[i] != "More..." {
return fmt.Sprintf("Change log and more info:\n https://github.com/kubernetes/kubernetes/releases/tag/%s", tags[i])
} else {
return "Load more tags"
}
}))
if err != nil {
panic(err)
}
} else if err != nil {
panic(err)
}
os.Remove(binPath + "kubectl")
err = os.Symlink(basePath+"kubectl-"+version, binPath+"kubectl")
if err != nil {
panic(err)
}
err = os.Chmod(basePath+"kubectl-"+version, 0775)
if err != nil {
panic(err)
version := tags[idx]
homePath, err := os.UserHomeDir()
if err != nil {
panic(err)
}
fmt.Printf("selected: %v\n", version)
if version == "More..." {
page += 1
} else {
basePath := homePath + "/.kvm/"
binPath := homePath + "/.kvm/bin/"
if _, err := os.Stat(basePath + "kubectl-" + version); errors.Is(err, os.ErrNotExist) {
path, err := helper.DownloadKubectlBinary(version)
if err != nil {
panic(err)
}
err = helper.MoveFile(path, basePath+"kubectl-"+version)
if err != nil {
panic(err)
}
} else if err != nil {
panic(err)
}
os.Remove(binPath + "kubectl")
err = os.Symlink(basePath+"kubectl-"+version, binPath+"kubectl")
if err != nil {
panic(err)
}
err = os.Chmod(basePath+"kubectl-"+version, 0775)
if err != nil {
panic(err)
}
return
}
}
},
}
Expand Down
6 changes: 3 additions & 3 deletions helper/version_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import (
"github.com/google/go-github/v48/github"
)

func GetVersions() ([]string, error) {
func GetVersions(page int) ([]string, error) {
gh := github.NewClient(nil)
tags, _, err := gh.Repositories.ListTags(context.TODO(), "kubernetes", "kubernetes", &github.ListOptions{
Page: 1,
PerPage: 15,
Page: page,
PerPage: 100,
})
if err != nil {
return nil, err
Expand Down

0 comments on commit 7b0271e

Please sign in to comment.