Skip to content

Commit

Permalink
adds short form table to info output (#299)
Browse files Browse the repository at this point in the history
Signed-off-by: Jordan Rash <[email protected]>
  • Loading branch information
jordan-rash authored Jun 28, 2024
1 parent d4c82d7 commit 5b52b46
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 15 deletions.
3 changes: 2 additions & 1 deletion nex/nex.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ var (
nodePreflight *fisk.CmdClause

node_info_id_arg = nodesInfo.Arg("id", "Public key of the node you're interested in").Required().String()
node_info_full = nodesInfo.Flag("full", "Long form output").Default("false").UnNegatableBool()

Opts = &models.Options{}
GuiOpts = &models.UiOptions{}
Expand Down Expand Up @@ -266,7 +267,7 @@ func main() {
logger.Error("Failed to list workloads", slog.Any("err", err))
}
case nodesInfo.FullCommand():
err := NodeInfo(ctx, *node_info_id_arg)
err := NodeInfo(ctx, *node_info_id_arg, *node_info_full)
if err != nil {
logger.Error("Failed to get node info", slog.Any("err", err))
}
Expand Down
52 changes: 38 additions & 14 deletions nex/nodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"strings"

"github.com/jedib0t/go-pretty/v6/table"
"github.com/jedib0t/go-pretty/v6/text"
"github.com/nats-io/natscli/columns"
controlapi "github.com/synadia-io/nex/control-api"
"github.com/synadia-io/nex/internal/models"
Expand Down Expand Up @@ -69,7 +70,7 @@ func LameDuck(ctx context.Context, logger *slog.Logger) error {
}

// Uses a control API client to retrieve info on a single node
func NodeInfo(ctx context.Context, nodeid string) error {
func NodeInfo(ctx context.Context, nodeid string, full bool) error {
log := slog.New(slog.NewJSONHandler(io.Discard, nil))
nc, err := models.GenerateConnectionFromOpts(Opts, log)
if err != nil {
Expand All @@ -80,18 +81,17 @@ func NodeInfo(ctx context.Context, nodeid string) error {
if err != nil {
return err
}
renderNodeInfo(nodeInfo, nodeid)
renderNodeInfo(nodeInfo, nodeid, full)

return nil
}

func render(cols *columns.Writer) {
_ = cols.Frender(os.Stdout)
}
func renderNodeInfo(info *controlapi.InfoResponse, id string) {
func renderNodeInfo(info *controlapi.InfoResponse, id string, full bool) {
cols := newColumns("NEX Node Information")

defer render(cols)
cols.AddRow("Node", id)
cols.AddRowf("Xkey", info.PublicXKey)
cols.AddRow("Version", info.Version)
Expand All @@ -114,19 +114,43 @@ func renderNodeInfo(info *controlapi.InfoResponse, id string) {

cols.Indent(0)
}
cols.Println()
render(cols)

if full {
if len(info.Machines) > 0 {
cols.AddSectionTitle("Workloads")
cols.Indent(2)
for _, m := range info.Machines {
cols.Println()
cols.AddRow("Id", m.Id)
cols.AddRow("Healthy", m.Healthy)
cols.AddRow("Runtime", m.Workload.Runtime)
cols.AddRow("Name", m.Workload.Name)
cols.AddRow("Description", m.Workload.Description)
}
cols.Indent(0)
}
} else {
t := table.NewWriter()
t.SetStyle(table.StyleRounded)

t.SetTitle("Workloads")
t.Style().Title.Align = text.AlignCenter
t.AppendHeader(table.Row{"", "ID", "Name", "Runtime"})

if len(info.Machines) > 0 {
cols.AddSectionTitle("Workloads")
cols.Indent(2)
for _, m := range info.Machines {
cols.Println()
cols.AddRow("Id", m.Id)
cols.AddRow("Healthy", m.Healthy)
cols.AddRow("Runtime", m.Workload.Runtime)
cols.AddRow("Name", m.Workload.Name)
cols.AddRow("Description", m.Workload.Description)
health := func() string {
if m.Healthy {
return "🟢"
}
return "🔴"
}()

t.AppendRow(table.Row{health, m.Id, m.Workload.Name, m.Workload.Runtime})
}
cols.Indent(0)

fmt.Println(t.Render())
}
}

Expand Down

0 comments on commit 5b52b46

Please sign in to comment.