github.com/ryanslade/nomad@v0.2.4-0.20160128061903-fc95782f2089/command/agent_info.go (about)

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