Skip to content

Commit

Permalink
refactor(autok3s): change get command
Browse files Browse the repository at this point in the history
  • Loading branch information
rancher-sy-bot committed Aug 20, 2020
1 parent e162d48 commit 338e104
Showing 1 changed file with 47 additions and 41 deletions.
88 changes: 47 additions & 41 deletions cmd/get.go
Original file line number Diff line number Diff line change
@@ -1,69 +1,53 @@
package cmd

import (
"fmt"
"os"
"strings"

"github.com/Jason-ZW/autok3s/pkg/cluster"
"github.com/Jason-ZW/autok3s/pkg/common"
"github.com/Jason-ZW/autok3s/pkg/providers"
"github.com/Jason-ZW/autok3s/pkg/utils"

"github.com/olekukonko/tablewriter"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

func GetCommand() *cobra.Command {
cmd := &cobra.Command{
var (
getCmd = &cobra.Command{
Use: "get",
Short: "Display one or many resources",
ValidArgs: []string{"provider", "cluster"},
Args: cobra.RangeArgs(1, 2),
Example: ` autok3s get provider`,
ValidArgs: []string{"cluster"},
Args: cobra.ExactArgs(1),
Example: ` autok3s get cluster`,
}

cmd.Run = func(cmd *cobra.Command, args []string) {
name, region string
)

func init() {
getCmd.Flags().StringVar(&name, "name", name, "Cluster name")
getCmd.Flags().StringVar(&region, "region", region, "Physical locations (data centers) that spread all over the world to reduce the network latency")
}

func GetCommand() *cobra.Command {
getCmd.Run = func(cmd *cobra.Command, args []string) {
switch args[0] {
case "provider":
getProvider(args)
case "cluster":
getCluster(args)
getCluster()
}
}
return cmd
}

func getProvider(args []string) {
table := tablewriter.NewWriter(os.Stdout)
table.SetBorder(false)
table.SetHeaderLine(false)
table.SetColumnSeparator("")
table.SetAlignment(tablewriter.ALIGN_LEFT)
table.SetHeader([]string{"Provider", "InTree"})

input := ""
if len(args) > 1 {
input = args[1]
}

for _, v := range providers.SupportedProviders(input) {
table.Append(v)
}
table.Render()
return getCmd
}

func getCluster(args []string) {
func getCluster() {
table := tablewriter.NewWriter(os.Stdout)
table.SetBorder(false)
table.SetHeaderLine(false)
table.SetColumnSeparator("")
table.SetAlignment(tablewriter.ALIGN_LEFT)
table.SetHeader([]string{"Name", "Provider", "Masters", "Workers"})

input := ""
if len(args) > 1 {
input = args[1]
}
table.SetHeader([]string{"Name", "Region", "Provider", "Masters", "Workers"})

v := common.CfgPath
if v == "" {
Expand All @@ -81,18 +65,40 @@ func getCluster(args []string) {
}

for _, r := range result {
if input != "" {
if input == r.Name {
if name != "" && region != "" {
if fmt.Sprintf("%s.%s", name, region) == r.Name {
table.Append([]string{
name,
region,
r.Provider,
r.Master,
r.Worker,
})
}
} else if name != "" {
if strings.Contains(r.Name, name) {
table.Append([]string{
r.Name[:strings.LastIndex(r.Name, ".")],
r.Name[strings.LastIndex(r.Name, ".")+1:],
r.Provider,
r.Master,
r.Worker,
})
}
} else if region != "" {
if strings.Contains(r.Name, region) {
table.Append([]string{
r.Name,
r.Name[:strings.LastIndex(r.Name, ".")],
r.Name[strings.LastIndex(r.Name, ".")+1:],
r.Provider,
r.Master,
r.Worker,
})
}
} else {
table.Append([]string{
r.Name,
r.Name[:strings.LastIndex(r.Name, ".")],
r.Name[strings.LastIndex(r.Name, ".")+1:],
r.Provider,
r.Master,
r.Worker,
Expand Down

0 comments on commit 338e104

Please # to comment.