2023-03-28 19:12:30 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
2023-08-11 13:12:13 +00:00
|
|
|
// SPDX-License-Identifier: BUSL-1.1
|
2023-03-28 19:12:30 +00:00
|
|
|
|
2019-04-30 23:27:16 +00:00
|
|
|
package list
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
|
2023-04-28 17:51:36 +00:00
|
|
|
"github.com/hashicorp/consul/api"
|
2019-04-30 23:27:16 +00:00
|
|
|
"github.com/hashicorp/consul/command/flags"
|
|
|
|
"github.com/mitchellh/cli"
|
|
|
|
)
|
|
|
|
|
|
|
|
func New(ui cli.Ui) *cmd {
|
|
|
|
c := &cmd{UI: ui}
|
|
|
|
c.init()
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
|
|
|
type cmd struct {
|
|
|
|
UI cli.Ui
|
|
|
|
flags *flag.FlagSet
|
|
|
|
http *flags.HTTPFlags
|
|
|
|
help string
|
|
|
|
|
2023-04-28 17:51:36 +00:00
|
|
|
kind string
|
|
|
|
filter string
|
2019-04-30 23:27:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *cmd) init() {
|
|
|
|
c.flags = flag.NewFlagSet("", flag.ContinueOnError)
|
|
|
|
c.flags.StringVar(&c.kind, "kind", "", "The kind of configurations to list.")
|
2023-04-28 17:51:36 +00:00
|
|
|
c.flags.StringVar(&c.filter, "filter", "", "Filter to use with the request.")
|
2019-04-30 23:27:16 +00:00
|
|
|
c.http = &flags.HTTPFlags{}
|
|
|
|
flags.Merge(c.flags, c.http.ClientFlags())
|
|
|
|
flags.Merge(c.flags, c.http.ServerFlags())
|
2021-07-21 19:45:24 +00:00
|
|
|
flags.Merge(c.flags, c.http.MultiTenancyFlags())
|
2019-04-30 23:27:16 +00:00
|
|
|
c.help = flags.Usage(help, c.flags)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *cmd) Run(args []string) int {
|
|
|
|
if err := c.flags.Parse(args); err != nil {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.kind == "" {
|
|
|
|
c.UI.Error("Must specify the -kind parameter")
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
client, err := c.http.APIClient()
|
|
|
|
if err != nil {
|
|
|
|
c.UI.Error(fmt.Sprintf("Error connect to Consul agent: %s", err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2023-04-28 17:51:36 +00:00
|
|
|
entries, _, err := client.ConfigEntries().List(c.kind, &api.QueryOptions{
|
|
|
|
Filter: c.filter,
|
|
|
|
})
|
2019-04-30 23:27:16 +00:00
|
|
|
if err != nil {
|
|
|
|
c.UI.Error(fmt.Sprintf("Error listing config entries for kind %q: %v", c.kind, err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, entry := range entries {
|
|
|
|
c.UI.Info(entry.GetName())
|
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *cmd) Synopsis() string {
|
|
|
|
return synopsis
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *cmd) Help() string {
|
|
|
|
return flags.Usage(c.help, nil)
|
|
|
|
}
|
|
|
|
|
2021-07-21 19:45:24 +00:00
|
|
|
const (
|
|
|
|
synopsis = "List centralized config entries of a given kind"
|
|
|
|
help = `
|
2019-04-30 23:27:16 +00:00
|
|
|
Usage: consul config list [options] -kind <config kind>
|
|
|
|
|
|
|
|
Lists all of the config entries for a given kind. The -kind parameter
|
|
|
|
is required.
|
|
|
|
|
|
|
|
Example:
|
|
|
|
|
|
|
|
$ consul config list -kind service-defaults
|
|
|
|
|
|
|
|
`
|
2021-07-21 19:45:24 +00:00
|
|
|
)
|