Skip to content

Commit

Permalink
Beautify output for dhcpserver commands
Browse files Browse the repository at this point in the history
  • Loading branch information
kishen-v committed Apr 19, 2024
1 parent 0ecc6a6 commit 1639a0f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
18 changes: 14 additions & 4 deletions cmd/dhcpserver/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ package dhcpserver
import (
"fmt"

"github.com/davecgh/go-spew/spew"
"github.com/spf13/cobra"
"k8s.io/klog/v2"

"github.com/ppc64le-cloud/pvsadm/pkg"
"github.com/ppc64le-cloud/pvsadm/pkg/client"
"github.com/ppc64le-cloud/pvsadm/pkg/utils"
)

var (
Expand All @@ -47,12 +47,22 @@ var getCmd = &cobra.Command{
return err
}

servers, err := pvmclient.DHCPClient.Get(id)
server, err := pvmclient.DHCPClient.Get(id)
if err != nil {
return fmt.Errorf("failed to get a dhcpserver, err: %v", err)
}

spew.Dump(servers)
if server.Network.Name == nil {
klog.Infof("DHCP client is not ACTIVE yet, please retry in sometime.")
return nil
}
var IPandMAC string
for _, lease := range server.Leases {
IPandMAC += fmt.Sprintf("%s - %s ", *lease.InstanceIP, *lease.InstanceMacAddress)
}
table := utils.NewTable()
table.SetHeader([]string{"Network Name", "IP - MAC", "Status"})
table.Append([]string{*server.Network.Name, IPandMAC, *server.Status})
table.Table.Render()
return nil
},
}
Expand Down
16 changes: 15 additions & 1 deletion cmd/dhcpserver/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,21 @@ var listCmd = &cobra.Command{
}

table := utils.NewTable()
table.Render(dhcpservers, nil)
table.SetHeader([]string{"ID", "Network ID", "Network Name", "Status"})
if len(dhcpservers) != 0 {
for _, dhcpserver := range dhcpservers {
if dhcpserver.Network.ID == nil || dhcpserver.Network.Name == nil {
// just in case, if the network is not ready, and the DHCP status reports as BUILD.
// printing the available information must suffice.
table.Append([]string{*dhcpserver.ID, "", "", *dhcpserver.Status})
continue
}
table.Append([]string{*dhcpserver.ID, *dhcpserver.Network.ID, *dhcpserver.Network.Name, *dhcpserver.Status})
}
table.Table.Render()
return nil
}
klog.Info("There are no DHCP servers associated with the instance id provided.")
return nil
},
}

0 comments on commit 1639a0f

Please sign in to comment.