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