Skip to content

Commit

Permalink
Add kuberlr bin subcommand
Browse files Browse the repository at this point in the history
This subcommand list all the kubectl binaries found on the system
  • Loading branch information
flavio committed Jun 24, 2020
1 parent c65b063 commit 4771a0a
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
56 changes: 56 additions & 0 deletions cmd/kuberlr/bins.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package main

import (
"fmt"
"os"

"github.com/jedib0t/go-pretty/v6/table"
"github.com/jedib0t/go-pretty/v6/text"
"github.com/spf13/cobra"

versioner "github.com/flavio/kuberlr/internal/versioner"
)

func printBinTable(bins versioner.KubectlBinaries) {
t := table.NewWriter()
t.SetOutputMirror(os.Stdout)
t.AppendHeader(table.Row{"#", "Version", "Binary"})
for i, b := range bins {
t.AppendRow([]interface{}{i + 1, b.Version, b.Path})
}
t.Render()
}

// NewBinsCmd creates a new `kuberlr bins` cobra command
func NewBinsCmd() *cobra.Command {
return &cobra.Command{
Use: "bins",
Short: "Print information about the kubectl binaries found",
Run: func(cmd *cobra.Command, args []string) {
localCache := versioner.NewLocalCacheHandler()

systemBins, err := localCache.SystemKubectlBinaries()

fmt.Printf("%s\n", text.FgGreen.Sprint("system-wide kubectl binaries"))
if err != nil {
fmt.Printf("Error retrieving binaries: %v\n", err)
} else if len(systemBins) == 0 {
fmt.Println("No binaries found.")
} else {
printBinTable(systemBins)
}

fmt.Printf("\n\n")
localBins, err := localCache.LocalKubectlBinaries()

fmt.Printf("%s\n", text.FgGreen.Sprint("local kubectl binaries"))
if err != nil {
fmt.Printf("Error retrieving binaries: %v\n", err)
} else if len(localBins) == 0 {
fmt.Println("No binaries found.")
} else {
printBinTable(localBins)
}
},
}
}
1 change: 1 addition & 0 deletions cmd/kuberlr/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ func newRootCmd() *cobra.Command {

cmd.AddCommand(
NewVersionCmd(),
NewBinsCmd(),
)

flags.RegisterVerboseFlag(cmd.PersistentFlags())
Expand Down

0 comments on commit 4771a0a

Please sign in to comment.