Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sort output of agent-info #572

Closed
wants to merge 2 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 18 additions & 5 deletions command/agent_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package command

import (
"fmt"
"sort"
"strings"
)

Expand Down Expand Up @@ -53,14 +54,26 @@ func (c *AgentInfoCommand) Run(args []string) int {
return 1
}

// Sort and output agent info
var stats map[string]interface{}
stats, _ = info["stats"]
stats_keys := make([]string, 0, len(stats))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can simplify this as stats_keys := make([]string, len(stats))

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Keep in mind that stats_keys is not a idiomatic variable notation in Go. It should be statsKeys (camelcase). My 2 cents.

for key := range stats {
stats_keys = append(stats_keys, key)
}
sort.Strings(stats_keys)

for _, key := range stats_keys {
c.Ui.Output(key)
d, _ := stats[key].(map[string]interface{})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a better variable name for d here? It used to be short for data but this was replaced. Between the inner loop and the addition of _keys I'm having a hard time following which variable holds what data. I'm curious if we can name these a bit better like "metric_id" or "agent_info" or something.

d_keys := make([]string, 0, len(d))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Likewise d_keys := make([]string, len(d))

for key := range d {
d_keys = append(d_keys, key)
}
sort.Strings(d_keys)

for section, data := range stats {
c.Ui.Output(section)
d, _ := data.(map[string]interface{})
for k, v := range d {
c.Ui.Output(fmt.Sprintf(" %s = %v", k, v))
for _, key := range d_keys {
c.Ui.Output(fmt.Sprintf(" %s = %v", key, d[key]))
}
}

Expand Down