github.com/emate/nomad@v0.8.2-wo-binpacking/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) Name() string { return "agent-info" }
    30  
    31  func (c *AgentInfoCommand) Run(args []string) int {
    32  	flags := c.Meta.FlagSet(c.Name(), FlagSetClient)
    33  	flags.Usage = func() { c.Ui.Output(c.Help()) }
    34  	if err := flags.Parse(args); err != nil {
    35  		return 1
    36  	}
    37  
    38  	// Check that we got no arguments
    39  	args = flags.Args()
    40  	if len(args) > 0 {
    41  		c.Ui.Error("This command takes no arguments")
    42  		c.Ui.Error(commandErrorText(c))
    43  		return 1
    44  	}
    45  
    46  	// Get the HTTP client
    47  	client, err := c.Meta.Client()
    48  	if err != nil {
    49  		c.Ui.Error(fmt.Sprintf("Error initializing client: %s", err))
    50  		return 1
    51  	}
    52  
    53  	// Query the agent info
    54  	info, err := client.Agent().Self()
    55  	if err != nil {
    56  		c.Ui.Error(fmt.Sprintf("Error querying agent info: %s", err))
    57  		return 1
    58  	}
    59  
    60  	// Sort and output agent info
    61  	statsKeys := make([]string, 0, len(info.Stats))
    62  	for key := range info.Stats {
    63  		statsKeys = append(statsKeys, key)
    64  	}
    65  	sort.Strings(statsKeys)
    66  
    67  	for _, key := range statsKeys {
    68  		c.Ui.Output(key)
    69  		statsData, _ := info.Stats[key]
    70  		statsDataKeys := make([]string, len(statsData))
    71  		i := 0
    72  		for key := range statsData {
    73  			statsDataKeys[i] = key
    74  			i++
    75  		}
    76  		sort.Strings(statsDataKeys)
    77  
    78  		for _, key := range statsDataKeys {
    79  			c.Ui.Output(fmt.Sprintf("  %s = %v", key, statsData[key]))
    80  		}
    81  	}
    82  
    83  	return 0
    84  }