github.com/hernad/nomad@v1.6.112/command/agent_info.go (about) 1 // Copyright (c) HashiCorp, Inc. 2 // SPDX-License-Identifier: MPL-2.0 3 4 package command 5 6 import ( 7 "fmt" 8 "sort" 9 "strings" 10 11 "github.com/posener/complete" 12 ) 13 14 type AgentInfoCommand struct { 15 Meta 16 } 17 18 func (c *AgentInfoCommand) Help() string { 19 helpText := ` 20 Usage: nomad agent-info [options] 21 22 Display status information about the local agent. 23 24 When ACLs are enabled, this command requires a token with the 'agent:read' 25 capability. 26 27 General Options: 28 29 ` + generalOptionsUsage(usageOptsDefault|usageOptsNoNamespace) + ` 30 31 Agent Info Options: 32 33 -json 34 Output the node in its JSON format. 35 36 -t 37 Format and display node using a Go template. 38 ` 39 return strings.TrimSpace(helpText) 40 } 41 42 func (c *AgentInfoCommand) Synopsis() string { 43 return "Display status information about the local agent" 44 } 45 46 func (c *AgentInfoCommand) AutocompleteFlags() complete.Flags { 47 return mergeAutocompleteFlags(c.Meta.AutocompleteFlags(FlagSetClient), 48 complete.Flags{ 49 "-json": complete.PredictNothing, 50 "-t": complete.PredictAnything, 51 }) 52 } 53 54 func (c *AgentInfoCommand) AutocompleteArgs() complete.Predictor { 55 return complete.PredictNothing 56 } 57 58 func (c *AgentInfoCommand) Name() string { return "agent-info" } 59 60 func (c *AgentInfoCommand) Run(args []string) int { 61 var json bool 62 var tmpl string 63 64 flags := c.Meta.FlagSet(c.Name(), FlagSetClient) 65 flags.Usage = func() { c.Ui.Output(c.Help()) } 66 flags.BoolVar(&json, "json", false, "") 67 flags.StringVar(&tmpl, "t", "", "") 68 69 if err := flags.Parse(args); err != nil { 70 c.Ui.Error(fmt.Sprintf("Error parsing flags: %s", err)) 71 return 1 72 } 73 74 // Check that we got no arguments 75 args = flags.Args() 76 if len(args) > 0 { 77 c.Ui.Error("This command takes no arguments") 78 c.Ui.Error(commandErrorText(c)) 79 return 1 80 } 81 82 // Get the HTTP client 83 client, err := c.Meta.Client() 84 if err != nil { 85 c.Ui.Error(fmt.Sprintf("Error initializing client: %s", err)) 86 return 1 87 } 88 89 // Query the agent info 90 info, err := client.Agent().Self() 91 if err != nil { 92 c.Ui.Error(fmt.Sprintf("Error querying agent info: %s", err)) 93 return 1 94 } 95 96 // If output format is specified, format and output the agent info 97 if json || len(tmpl) > 0 { 98 out, err := Format(json, tmpl, info) 99 if err != nil { 100 c.Ui.Error(fmt.Sprintf("Error formatting output: %s", err)) 101 return 1 102 } 103 104 c.Ui.Output(out) 105 return 0 106 } 107 108 // Sort and output agent info 109 statsKeys := make([]string, 0, len(info.Stats)) 110 for key := range info.Stats { 111 statsKeys = append(statsKeys, key) 112 } 113 sort.Strings(statsKeys) 114 115 for _, key := range statsKeys { 116 c.Ui.Output(key) 117 statsData := info.Stats[key] 118 statsDataKeys := make([]string, len(statsData)) 119 i := 0 120 for key := range statsData { 121 statsDataKeys[i] = key 122 i++ 123 } 124 sort.Strings(statsDataKeys) 125 126 for _, key := range statsDataKeys { 127 c.Ui.Output(fmt.Sprintf(" %s = %v", key, statsData[key])) 128 } 129 } 130 131 return 0 132 }