diff --git a/cmd/purge/vms/vms.go b/cmd/purge/vms/vms.go index 94b7cb19..31948351 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("No data found to display") + 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..b4cac32e 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("No data found to display") + 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..b9d144e4 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("No data found to display") } t.Table.Render() }