From f12f5c5c5abb745d5188479fb8d2842f312de281 Mon Sep 17 00:00:00 2001 From: ASHMIL HUSSAIN K Date: Thu, 28 Nov 2019 23:34:18 +0400 Subject: [PATCH] added filter for all get commands --- cmd/cluster.go | 3 ++- cmd/disk.go | 4 +++- cmd/nodepool.go | 4 +++- cmd/resourcegroup.go | 7 ++++--- pkg/ctl/cluster/cluster.go | 16 ++++++++++------ pkg/ctl/disk/disk.go | 14 ++++++++++---- pkg/ctl/nodepool/nodepool.go | 16 +++++++++++----- pkg/ctl/resourcegroup/resourcegroup.go | 16 +++++++++++----- 8 files changed, 54 insertions(+), 26 deletions(-) diff --git a/cmd/cluster.go b/cmd/cluster.go index 3091edd..e3477c6 100644 --- a/cmd/cluster.go +++ b/cmd/cluster.go @@ -201,10 +201,11 @@ var getClusterCmd = &cobra.Command{ } rgroupName := getViper.GetString("metadata.resource-group") // getting values through viper + clusterFilter, _ := cmd.Flags().GetString("flag") color.Cyan("rgroupName : " + rgroupName) - cluster.GetCluster(rgroupName) + cluster.GetCluster(rgroupName, clusterFilter) }, } diff --git a/cmd/disk.go b/cmd/disk.go index 0fb0369..216e988 100644 --- a/cmd/disk.go +++ b/cmd/disk.go @@ -160,10 +160,11 @@ var getDiskCmd = &cobra.Command{ //getDiskViper.SetDefault("diskResourceGroup", "DiskRGDefault") // for setting a default value diskResourceGroup := getDiskViper.GetString("managedDisk.resource-group") + diskFilter, _ := cmd.Flags().GetString("flag") color.Cyan("diskResourceGroup : " + diskResourceGroup) - disk.GetDisk(diskResourceGroup) + disk.GetDisk(diskResourceGroup, diskFilter) }, } @@ -206,6 +207,7 @@ func init() { getCmd.AddCommand(getDiskCmd) getDiskCmd.PersistentFlags().StringP("rgroupname", "g", "opsFlagDefaultG", "disk resource group") + getDiskCmd.PersistentFlags().StringP("flag", "l", "all", "filtr flag") getDiskViper.BindPFlag("managedDisk.resource-group", getDiskCmd.PersistentFlags().Lookup("rgroupname")) diff --git a/cmd/nodepool.go b/cmd/nodepool.go index 94d57ef..dcad22e 100644 --- a/cmd/nodepool.go +++ b/cmd/nodepool.go @@ -167,10 +167,11 @@ var getNodePoolCmd = &cobra.Command{ // getting values through viper clusterName := getNPViper.GetString("nodePools.cluster-name") rgroupName := getNPViper.GetString("nodePools.resource-group") + nodepoolFilter, _ := cmd.Flags().GetString("flag") fmt.Println("clusterName : ", clusterName, ", ", "rgroupName : ", rgroupName) - nodepool.GetNodePool(clusterName, rgroupName) + nodepool.GetNodePool(clusterName, rgroupName, nodepoolFilter) }, } @@ -229,6 +230,7 @@ func init() { getCmd.AddCommand(getNodePoolCmd) getNodePoolCmd.PersistentFlags().StringP("rgroupname", "g", "rgFlag", "NodePool resource group") getNodePoolCmd.PersistentFlags().StringP("clustername", "c", "clusterFlag", "NodePool cluster") + getNodePoolCmd.PersistentFlags().StringP("flag", "l", "all", "filtr flag") getNPViper.BindPFlag("nodePools.cluster-name", getNodePoolCmd.PersistentFlags().Lookup("clustername")) getNPViper.BindPFlag("nodePools.resource-group", getNodePoolCmd.PersistentFlags().Lookup("rgroupname")) diff --git a/cmd/resourcegroup.go b/cmd/resourcegroup.go index f587bb7..022a959 100644 --- a/cmd/resourcegroup.go +++ b/cmd/resourcegroup.go @@ -128,8 +128,8 @@ var getResourceGroupCmd = &cobra.Command{ if err != nil { // Handle errors reading the config file panic(fmt.Errorf("Fatal error config file: %s \n", err)) } - - resourcegroup.GetResourceGroup() + rGroupFilter, _ := cmd.Flags().GetString("flag") + resourcegroup.GetResourceGroup(rGroupFilter) }, } @@ -166,7 +166,8 @@ func init() { getCmd.AddCommand(getResourceGroupCmd) - getRGViper.BindPFlags(getCmd.PersistentFlags()) + getResourceGroupCmd.PersistentFlags().StringP("flag", "l", "all", "filtr flag") + getRGViper.BindPFlags(getResourceGroupCmd.PersistentFlags()) // Here you will define your flags and configuration settings. // Cobra supports Persistent Flags which will work for this command diff --git a/pkg/ctl/cluster/cluster.go b/pkg/ctl/cluster/cluster.go index 3c02bbf..f016084 100644 --- a/pkg/ctl/cluster/cluster.go +++ b/pkg/ctl/cluster/cluster.go @@ -3,14 +3,15 @@ package cluster import ( "bytes" "fmt" + "os" + "os/exec" + "time" + "github.com/adfolks/aksctl/pkg/ctl/utils" "github.com/fatih/color" "github.com/gernest/wow" "github.com/gernest/wow/spin" "github.com/kyokomi/emoji" - "os" - "os/exec" - "time" ) //CreateCluster creates a cluster @@ -111,7 +112,7 @@ func UpdateCluster(clusterName string, resourceGroupName string) { } //GetCluster will list the available clusters -func GetCluster(resourceGroupName string) { +func GetCluster(resourceGroupName string, clusterFilter string) { a := wow.New(os.Stdout, spin.Get(spin.Dots), "Collecting your k8s Cluster informations") a.Start() time.Sleep(2 * time.Second) @@ -129,8 +130,11 @@ func GetCluster(resourceGroupName string) { return } a.PersistWith(spin.Spinner{}, "....") - fmt.Println("Result: " + out.String()) - fmt.Println("Type:", utils.FilterStringMap(out.String(), "name")) + if clusterFilter == "all" { + fmt.Println("Result: " + out.String()) + } else { + fmt.Println("Result:", utils.FilterStringMap(out.String(), clusterFilter)) + } } diff --git a/pkg/ctl/disk/disk.go b/pkg/ctl/disk/disk.go index d894133..334ec16 100644 --- a/pkg/ctl/disk/disk.go +++ b/pkg/ctl/disk/disk.go @@ -5,12 +5,14 @@ import ( "fmt" "os/exec" + "os" + "time" + + "github.com/adfolks/aksctl/pkg/ctl/utils" "github.com/fatih/color" "github.com/gernest/wow" "github.com/gernest/wow/spin" "github.com/kyokomi/emoji" - "os" - "time" ) //CreateDisk will create a disk @@ -81,7 +83,7 @@ func UpdateDisk(diskName string, diskResourceGroup string, diskSize string) { } //GetDisk will list the available disks -func GetDisk(diskResourceGroup string) { +func GetDisk(diskResourceGroup string, diskFilter string) { a := wow.New(os.Stdout, spin.Get(spin.Dots), "Fetching disks") a.Start() time.Sleep(2 * time.Second) @@ -99,5 +101,9 @@ func GetDisk(diskResourceGroup string) { return } a.PersistWith(spin.Spinner{}, "....") - fmt.Println("Result: " + out.String()) + if diskFilter == "all" { + fmt.Println("Result: " + out.String()) + } else { + fmt.Println("Result:", utils.FilterStringMap(out.String(), diskFilter)) + } } diff --git a/pkg/ctl/nodepool/nodepool.go b/pkg/ctl/nodepool/nodepool.go index cb8bf84..cb0c413 100644 --- a/pkg/ctl/nodepool/nodepool.go +++ b/pkg/ctl/nodepool/nodepool.go @@ -3,13 +3,15 @@ package nodepool import ( "bytes" "fmt" + "os" + "os/exec" + "time" + + "github.com/adfolks/aksctl/pkg/ctl/utils" "github.com/fatih/color" "github.com/gernest/wow" "github.com/gernest/wow/spin" "github.com/kyokomi/emoji" - "os" - "os/exec" - "time" ) //CreateNodePool will create a nodepool @@ -98,7 +100,7 @@ func UpdateNodePool(clusterName string, nodePoolName string, rgroupName string) } //GetNodePool will list the nodepools -func GetNodePool(clusterName string, rgroupName string) { +func GetNodePool(clusterName string, rgroupName string, nodepoolFilter string) { a := wow.New(os.Stdout, spin.Get(spin.Dots), "Fetching nodepools") a.Start() time.Sleep(2 * time.Second) @@ -114,5 +116,9 @@ func GetNodePool(clusterName string, rgroupName string) { return } a.PersistWith(spin.Spinner{}, "....") - fmt.Println("Result: " + out.String()) + if nodepoolFilter == "all" { + fmt.Println("Result: " + out.String()) + } else { + fmt.Println("Result:", utils.FilterStringMap(out.String(), nodepoolFilter)) + } } diff --git a/pkg/ctl/resourcegroup/resourcegroup.go b/pkg/ctl/resourcegroup/resourcegroup.go index d42bfac..d1c42d1 100644 --- a/pkg/ctl/resourcegroup/resourcegroup.go +++ b/pkg/ctl/resourcegroup/resourcegroup.go @@ -3,13 +3,15 @@ package resourcegroup import ( "bytes" "fmt" + "os" + "os/exec" + "time" + + "github.com/adfolks/aksctl/pkg/ctl/utils" "github.com/fatih/color" "github.com/gernest/wow" "github.com/gernest/wow/spin" "github.com/kyokomi/emoji" - "os" - "os/exec" - "time" ) //CreateResourceGroup will create a resource group @@ -100,7 +102,7 @@ func UpdateResourceGroup(rgroupName string) { } //GetResourceGroup will list the resource groups -func GetResourceGroup() { +func GetResourceGroup(filterRgroup string) { a := wow.New(os.Stdout, spin.Get(spin.Dots), "Fetching resource groups") a.Start() time.Sleep(2 * time.Second) @@ -117,5 +119,9 @@ func GetResourceGroup() { return } a.PersistWith(spin.Spinner{}, "....") - fmt.Println("Result: " + out.String()) + if filterRgroup == "all" { + fmt.Println("Result: " + out.String()) + } else { + fmt.Println("Result:", utils.FilterStringMap(out.String(), filterRgroup)) + } }