Skip to content

Commit

Permalink
nomad agent-info: Add json/gotemplate formatting (#9788)
Browse files Browse the repository at this point in the history
* nomad agent-info: Add json/gotemplate formatting
* Add CHANGELOG entry
* update docs
  • Loading branch information
davemay99 authored and backspace committed Jan 22, 2021
1 parent 6cf5168 commit a3d76c3
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ IMPROVEMENTS:
* build: Updated to Go 1.15.6. [[GH-9686](https://github.com/hashicorp/nomad/issues/9686)]
* client: Improve support for AWS Graviton instances [[GH-7989](https://github.com/hashicorp/nomad/issues/7989)]
* consul/connect: Interpolate the connect, service meta, and service canary meta blocks with the task environment [[GH-9586](https://github.com/hashicorp/nomad/pull/9586)]
* cli: Added JSON/go template formatting to agent-info command. [[GH-9788](https://github.com/hashicorp/nomad/pull/9788)]

BUG FIXES:
* client: Fixed a bug where non-`docker` tasks with network isolation were restarted on client restart. [[GH-9757](https://github.com/hashicorp/nomad/issues/9757)]
Expand Down
36 changes: 34 additions & 2 deletions command/agent_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,16 @@ Usage: nomad agent-info [options]
General Options:
` + generalOptionsUsage(usageOptsDefault|usageOptsNoNamespace)
` + generalOptionsUsage(usageOptsDefault|usageOptsNoNamespace) + `
Agent Info Options:
-json
Output the node in its JSON format.
-t
Format and display node using a Go template.
`
return strings.TrimSpace(helpText)
}

Expand All @@ -32,7 +41,11 @@ func (c *AgentInfoCommand) Synopsis() string {
}

func (c *AgentInfoCommand) AutocompleteFlags() complete.Flags {
return c.Meta.AutocompleteFlags(FlagSetClient)
return mergeAutocompleteFlags(c.Meta.AutocompleteFlags(FlagSetClient),
complete.Flags{
"-json": complete.PredictNothing,
"-t": complete.PredictAnything,
})
}

func (c *AgentInfoCommand) AutocompleteArgs() complete.Predictor {
Expand All @@ -42,9 +55,16 @@ func (c *AgentInfoCommand) AutocompleteArgs() complete.Predictor {
func (c *AgentInfoCommand) Name() string { return "agent-info" }

func (c *AgentInfoCommand) Run(args []string) int {
var json bool
var tmpl string

flags := c.Meta.FlagSet(c.Name(), FlagSetClient)
flags.Usage = func() { c.Ui.Output(c.Help()) }
flags.BoolVar(&json, "json", false, "")
flags.StringVar(&tmpl, "t", "", "")

if err := flags.Parse(args); err != nil {
c.Ui.Error(fmt.Sprintf("Error parsing flags: %s", err))
return 1
}

Expand All @@ -70,6 +90,18 @@ func (c *AgentInfoCommand) Run(args []string) int {
return 1
}

// If output format is specified, format and output the agent info
if json || len(tmpl) > 0 {
out, err := Format(json, tmpl, info)
if err != nil {
c.Ui.Error(fmt.Sprintf("Error formatting output: %s", err))
return 1
}

c.Ui.Output(out)
return 0
}

// Sort and output agent info
statsKeys := make([]string, 0, len(info.Stats))
for key := range info.Stats {
Expand Down
34 changes: 34 additions & 0 deletions command/agent_info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,40 @@ func TestAgentInfoCommand_Run(t *testing.T) {
}
}

func TestAgentInfoCommand_Run_JSON(t *testing.T) {
t.Parallel()
srv, _, url := testServer(t, false, nil)
defer srv.Shutdown()

ui := cli.NewMockUi()
cmd := &AgentInfoCommand{Meta: Meta{Ui: ui}}

code := cmd.Run([]string{"-address=" + url, "-json"})
if code != 0 {
t.Fatalf("expected exit 0, got: %d", code)
}
if out := ui.OutputWriter.String(); !strings.Contains(out, "\"config\": {") {
t.Fatalf("expected config stanza in output json")
}
}

func TestAgentInfoCommand_Run_Gotemplate(t *testing.T) {
t.Parallel()
srv, _, url := testServer(t, false, nil)
defer srv.Shutdown()

ui := cli.NewMockUi()
cmd := &AgentInfoCommand{Meta: Meta{Ui: ui}}

code := cmd.Run([]string{"-address=" + url, "-t", "{{.Stats.raft}}"})
if code != 0 {
t.Fatalf("expected exit 0, got: %d", code)
}
if out := ui.OutputWriter.String(); !strings.Contains(out, "last_log_index") {
t.Fatalf("expected raft stats in gotemplate output")
}
}

func TestAgentInfoCommand_Fails(t *testing.T) {
t.Parallel()
ui := cli.NewMockUi()
Expand Down
5 changes: 5 additions & 0 deletions website/content/docs/commands/agent-info.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ capability.

@include 'general_options_no_namespace.mdx'

## Agent Info Options

- `-json` : Output agent info in its JSON format.
- `-t` : Format and display agent info using a Go template.

## Output

Depending on the agent queried, information from different subsystems is
Expand Down

0 comments on commit a3d76c3

Please sign in to comment.