1package command
2
3import (
4	"fmt"
5	"sort"
6	"strings"
7
8	"github.com/posener/complete"
9)
10
11type AgentInfoCommand struct {
12	Meta
13}
14
15func (c *AgentInfoCommand) Help() string {
16	helpText := `
17Usage: nomad agent-info [options]
18
19  Display status information about the local agent.
20
21General Options:
22
23  ` + generalOptionsUsage()
24	return strings.TrimSpace(helpText)
25}
26
27func (c *AgentInfoCommand) Synopsis() string {
28	return "Display status information about the local agent"
29}
30
31func (c *AgentInfoCommand) AutocompleteFlags() complete.Flags {
32	return c.Meta.AutocompleteFlags(FlagSetClient)
33}
34
35func (c *AgentInfoCommand) AutocompleteArgs() complete.Predictor {
36	return complete.PredictNothing
37}
38
39func (c *AgentInfoCommand) Name() string { return "agent-info" }
40
41func (c *AgentInfoCommand) Run(args []string) int {
42	flags := c.Meta.FlagSet(c.Name(), FlagSetClient)
43	flags.Usage = func() { c.Ui.Output(c.Help()) }
44	if err := flags.Parse(args); err != nil {
45		return 1
46	}
47
48	// Check that we got no arguments
49	args = flags.Args()
50	if len(args) > 0 {
51		c.Ui.Error("This command takes no arguments")
52		c.Ui.Error(commandErrorText(c))
53		return 1
54	}
55
56	// Get the HTTP client
57	client, err := c.Meta.Client()
58	if err != nil {
59		c.Ui.Error(fmt.Sprintf("Error initializing client: %s", err))
60		return 1
61	}
62
63	// Query the agent info
64	info, err := client.Agent().Self()
65	if err != nil {
66		c.Ui.Error(fmt.Sprintf("Error querying agent info: %s", err))
67		return 1
68	}
69
70	// Sort and output agent info
71	statsKeys := make([]string, 0, len(info.Stats))
72	for key := range info.Stats {
73		statsKeys = append(statsKeys, key)
74	}
75	sort.Strings(statsKeys)
76
77	for _, key := range statsKeys {
78		c.Ui.Output(key)
79		statsData, _ := info.Stats[key]
80		statsDataKeys := make([]string, len(statsData))
81		i := 0
82		for key := range statsData {
83			statsDataKeys[i] = key
84			i++
85		}
86		sort.Strings(statsDataKeys)
87
88		for _, key := range statsDataKeys {
89			c.Ui.Output(fmt.Sprintf("  %s = %v", key, statsData[key]))
90		}
91	}
92
93	return 0
94}
95