From 4f502f7d100bad8dca22517d211ed100ea1ae6f9 Mon Sep 17 00:00:00 2001 From: Sharath P J Date: Mon, 18 Nov 2024 15:01:31 +0530 Subject: [PATCH 1/2] Updated proper response for no records while executing purge command --- cmd/purge/vms/vms.go | 5 +++++ cmd/purge/volumes/volumes.go | 11 +++++++++-- pkg/utils/table.go | 11 ++++++----- 3 files changed, 20 insertions(+), 7 deletions(-) diff --git a/cmd/purge/vms/vms.go b/cmd/purge/vms/vms.go index 94b7cb19..20688572 100644 --- a/cmd/purge/vms/vms.go +++ b/cmd/purge/vms/vms.go @@ -52,6 +52,11 @@ pvsadm purge --help for information return err } + if len(instances) == 0 { + klog.Info("\n--NO DATA FOUND--") + return nil + } + t := utils.NewTable() t.SetHeader([]string{"Name", "IP Addresses", "Image", "CPUS", "RAM", "STATUS", "Creation Date"}) for _, instance := range instances { diff --git a/cmd/purge/volumes/volumes.go b/cmd/purge/volumes/volumes.go index 46ceeef7..18de77f4 100644 --- a/cmd/purge/volumes/volumes.go +++ b/cmd/purge/volumes/volumes.go @@ -16,12 +16,14 @@ package volumes import ( "fmt" + + "github.com/spf13/cobra" + "k8s.io/klog/v2" + "github.com/ppc64le-cloud/pvsadm/pkg" "github.com/ppc64le-cloud/pvsadm/pkg/audit" "github.com/ppc64le-cloud/pvsadm/pkg/client" "github.com/ppc64le-cloud/pvsadm/pkg/utils" - "github.com/spf13/cobra" - "k8s.io/klog/v2" ) var Cmd = &cobra.Command{ @@ -48,6 +50,11 @@ pvsadm purge --help for information return fmt.Errorf("failed to get the list of volumes: %v", err) } + if len(volumes) == 0 { + klog.Info("\n--NO DATA FOUND--") + return nil + } + t := utils.NewTable() t.SetHeader([]string{"Name", "Volume ID", "State", "Last Update Date"}) for _, volume := range volumes { diff --git a/pkg/utils/table.go b/pkg/utils/table.go index d1304a98..308dd89d 100644 --- a/pkg/utils/table.go +++ b/pkg/utils/table.go @@ -16,13 +16,15 @@ package utils import ( "fmt" - "github.com/go-openapi/strfmt" - "github.com/olekukonko/tablewriter" "os" "reflect" "strconv" "strings" "sync" + + "github.com/go-openapi/strfmt" + "github.com/olekukonko/tablewriter" + "k8s.io/klog/v2" ) type Table struct { @@ -49,9 +51,8 @@ func (t *Table) Render(rows interface{}, exclude []string) { s := reflect.ValueOf(rows) for i := 0; i < s.Len(); i++ { noData = false - var headers []string + var headers, row []string val := s.Index(i).Elem() - var row []string for i := 0; i < val.NumField(); i++ { if f := strings.ToLower(val.Type().Field(i).Name); Contains(exclude, f) { continue @@ -65,7 +66,7 @@ func (t *Table) Render(rows interface{}, exclude []string) { } } if noData { - fmt.Println("\n--NO DATA FOUND--") + klog.Info("\n--NO DATA FOUND--") } t.Table.Render() } From b4f70124fb302793b6733cc05ed066805c311f98 Mon Sep 17 00:00:00 2001 From: Sharath P J Date: Mon, 18 Nov 2024 15:01:31 +0530 Subject: [PATCH 2/2] Updated proper response for no records while executing purge command --- cmd/purge/vms/vms.go | 2 +- cmd/purge/volumes/volumes.go | 2 +- pkg/utils/table.go | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/cmd/purge/vms/vms.go b/cmd/purge/vms/vms.go index 20688572..31948351 100644 --- a/cmd/purge/vms/vms.go +++ b/cmd/purge/vms/vms.go @@ -53,7 +53,7 @@ pvsadm purge --help for information } if len(instances) == 0 { - klog.Info("\n--NO DATA FOUND--") + klog.Info("No data found to display") return nil } diff --git a/cmd/purge/volumes/volumes.go b/cmd/purge/volumes/volumes.go index 18de77f4..b4cac32e 100644 --- a/cmd/purge/volumes/volumes.go +++ b/cmd/purge/volumes/volumes.go @@ -51,7 +51,7 @@ pvsadm purge --help for information } if len(volumes) == 0 { - klog.Info("\n--NO DATA FOUND--") + klog.Info("No data found to display") return nil } diff --git a/pkg/utils/table.go b/pkg/utils/table.go index 308dd89d..b9d144e4 100644 --- a/pkg/utils/table.go +++ b/pkg/utils/table.go @@ -66,7 +66,7 @@ func (t *Table) Render(rows interface{}, exclude []string) { } } if noData { - klog.Info("\n--NO DATA FOUND--") + klog.Info("No data found to display") } t.Table.Render() }