feat(cli): Add plugins subcommand to list available and deprecated (#13787)
This commit is contained in:
parent
fb45a1d98a
commit
1e42262184
|
|
@ -300,7 +300,7 @@ issues:
|
|||
linters:
|
||||
- govet
|
||||
|
||||
- path: cmd/telegraf/(main|printer).go
|
||||
- path: cmd/telegraf/(main|printer|cmd_plugins).go
|
||||
text: "Error return value of `outputBuffer.Write` is not checked" #errcheck
|
||||
|
||||
- path: _test\.go
|
||||
|
|
|
|||
|
|
@ -0,0 +1,149 @@
|
|||
// Command handling for configuration "plugins" command
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"github.com/influxdata/telegraf/plugins/aggregators"
|
||||
"github.com/influxdata/telegraf/plugins/inputs"
|
||||
"github.com/influxdata/telegraf/plugins/outputs"
|
||||
"github.com/influxdata/telegraf/plugins/processors"
|
||||
"github.com/influxdata/telegraf/plugins/secretstores"
|
||||
"github.com/urfave/cli/v2"
|
||||
)
|
||||
|
||||
func pluginNames[M ~map[string]V, V any](m M, prefix string) []byte {
|
||||
names := make([]string, 0, len(m))
|
||||
for k := range m {
|
||||
names = append(names, fmt.Sprintf("%s.%s\n", prefix, k))
|
||||
}
|
||||
sort.Strings(names)
|
||||
return []byte(strings.Join(names, ""))
|
||||
}
|
||||
|
||||
func getPluginCommands(outputBuffer io.Writer) []*cli.Command {
|
||||
return []*cli.Command{
|
||||
{
|
||||
Name: "plugins",
|
||||
Usage: "commands for printing available plugins",
|
||||
Flags: []cli.Flag{
|
||||
&cli.BoolFlag{
|
||||
Name: "deprecated",
|
||||
Usage: "print only deprecated plugins",
|
||||
},
|
||||
},
|
||||
Action: func(cCtx *cli.Context) error {
|
||||
if cCtx.Bool("deprecated") {
|
||||
outputBuffer.Write(pluginNames(inputs.Deprecations, "inputs"))
|
||||
outputBuffer.Write(pluginNames(outputs.Deprecations, "outputs"))
|
||||
outputBuffer.Write(pluginNames(processors.Deprecations, "processors"))
|
||||
outputBuffer.Write(pluginNames(aggregators.Deprecations, "aggregators"))
|
||||
outputBuffer.Write(pluginNames(secretstores.Deprecations, "secretstores"))
|
||||
} else {
|
||||
outputBuffer.Write(pluginNames(inputs.Inputs, "inputs"))
|
||||
outputBuffer.Write(pluginNames(outputs.Outputs, "outputs"))
|
||||
outputBuffer.Write(pluginNames(processors.Processors, "processors"))
|
||||
outputBuffer.Write(pluginNames(aggregators.Aggregators, "aggregators"))
|
||||
outputBuffer.Write(pluginNames(secretstores.SecretStores, "secretstores"))
|
||||
}
|
||||
|
||||
return nil
|
||||
},
|
||||
Subcommands: []*cli.Command{
|
||||
{
|
||||
Name: "inputs",
|
||||
Usage: "Print available input plugins",
|
||||
Flags: []cli.Flag{
|
||||
&cli.BoolFlag{
|
||||
Name: "deprecated",
|
||||
Usage: "print only deprecated plugins",
|
||||
},
|
||||
},
|
||||
Action: func(cCtx *cli.Context) error {
|
||||
if cCtx.Bool("deprecated") {
|
||||
outputBuffer.Write(pluginNames(inputs.Deprecations, "inputs"))
|
||||
} else {
|
||||
outputBuffer.Write(pluginNames(inputs.Inputs, "inputs"))
|
||||
}
|
||||
return nil
|
||||
},
|
||||
},
|
||||
{
|
||||
Name: "outputs",
|
||||
Usage: "Print available output plugins",
|
||||
Flags: []cli.Flag{
|
||||
&cli.BoolFlag{
|
||||
Name: "deprecated",
|
||||
Usage: "print only deprecated plugins",
|
||||
},
|
||||
},
|
||||
Action: func(cCtx *cli.Context) error {
|
||||
if cCtx.Bool("deprecated") {
|
||||
outputBuffer.Write(pluginNames(outputs.Deprecations, "outputs"))
|
||||
} else {
|
||||
outputBuffer.Write(pluginNames(outputs.Outputs, "outputs"))
|
||||
}
|
||||
return nil
|
||||
},
|
||||
},
|
||||
{
|
||||
Name: "processors",
|
||||
Usage: "Print available processor plugins",
|
||||
Flags: []cli.Flag{
|
||||
&cli.BoolFlag{
|
||||
Name: "deprecated",
|
||||
Usage: "print only deprecated plugins",
|
||||
},
|
||||
},
|
||||
Action: func(cCtx *cli.Context) error {
|
||||
if cCtx.Bool("deprecated") {
|
||||
outputBuffer.Write(pluginNames(processors.Deprecations, "processors"))
|
||||
} else {
|
||||
outputBuffer.Write(pluginNames(processors.Processors, "processors"))
|
||||
}
|
||||
return nil
|
||||
},
|
||||
},
|
||||
{
|
||||
Name: "aggregators",
|
||||
Usage: "Print available aggregator plugins",
|
||||
Flags: []cli.Flag{
|
||||
&cli.BoolFlag{
|
||||
Name: "deprecated",
|
||||
Usage: "print only deprecated plugins",
|
||||
},
|
||||
},
|
||||
Action: func(cCtx *cli.Context) error {
|
||||
if cCtx.Bool("deprecated") {
|
||||
outputBuffer.Write(pluginNames(aggregators.Deprecations, "aggregators"))
|
||||
} else {
|
||||
outputBuffer.Write(pluginNames(aggregators.Aggregators, "aggregators"))
|
||||
}
|
||||
return nil
|
||||
},
|
||||
},
|
||||
{
|
||||
Name: "secretstores",
|
||||
Usage: "Print available secretstore plugins",
|
||||
Flags: []cli.Flag{
|
||||
&cli.BoolFlag{
|
||||
Name: "deprecated",
|
||||
Usage: "print only deprecated plugins",
|
||||
},
|
||||
},
|
||||
Action: func(cCtx *cli.Context) error {
|
||||
if cCtx.Bool("deprecated") {
|
||||
outputBuffer.Write(pluginNames(secretstores.Deprecations, "secretstores"))
|
||||
} else {
|
||||
outputBuffer.Write(pluginNames(secretstores.SecretStores, "secretstores"))
|
||||
}
|
||||
return nil
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
|
@ -170,6 +170,7 @@ func runApp(args []string, outputBuffer io.Writer, pprof Server, c TelegrafConfi
|
|||
return nil
|
||||
// print available output plugins
|
||||
case cCtx.Bool("output-list"):
|
||||
outputBuffer.Write([]byte("DEPRECATED: use telegraf plugins outputs\n"))
|
||||
outputBuffer.Write([]byte("Available Output Plugins:\n"))
|
||||
names := make([]string, 0, len(outputs.Outputs))
|
||||
for k := range outputs.Outputs {
|
||||
|
|
@ -182,6 +183,7 @@ func runApp(args []string, outputBuffer io.Writer, pprof Server, c TelegrafConfi
|
|||
return nil
|
||||
// print available input plugins
|
||||
case cCtx.Bool("input-list"):
|
||||
outputBuffer.Write([]byte("DEPRECATED: use telegraf plugins inputs\n"))
|
||||
outputBuffer.Write([]byte("Available Input Plugins:\n"))
|
||||
names := make([]string, 0, len(inputs.Inputs))
|
||||
for k := range inputs.Inputs {
|
||||
|
|
@ -250,6 +252,7 @@ func runApp(args []string, outputBuffer io.Writer, pprof Server, c TelegrafConfi
|
|||
getConfigCommands(pluginFilterFlags, outputBuffer),
|
||||
getSecretStoreCommands(m)...,
|
||||
)
|
||||
commands = append(commands, getPluginCommands(outputBuffer)...)
|
||||
|
||||
app := &cli.App{
|
||||
Name: "Telegraf",
|
||||
|
|
|
|||
|
|
@ -200,7 +200,8 @@ func TestInputListFlag(t *testing.T) {
|
|||
}
|
||||
err := runApp(args, buf, NewMockServer(), NewMockConfig(buf), NewMockTelegraf())
|
||||
require.NoError(t, err)
|
||||
expectedOutput := `Available Input Plugins:
|
||||
expectedOutput := `DEPRECATED: use telegraf plugins inputs
|
||||
Available Input Plugins:
|
||||
test
|
||||
`
|
||||
require.Equal(t, expectedOutput, buf.String())
|
||||
|
|
@ -217,7 +218,8 @@ func TestOutputListFlag(t *testing.T) {
|
|||
}
|
||||
err := runApp(args, buf, NewMockServer(), NewMockConfig(buf), NewMockTelegraf())
|
||||
require.NoError(t, err)
|
||||
expectedOutput := `Available Output Plugins:
|
||||
expectedOutput := `DEPRECATED: use telegraf plugins outputs
|
||||
Available Output Plugins:
|
||||
test
|
||||
`
|
||||
require.Equal(t, expectedOutput, buf.String())
|
||||
|
|
|
|||
Loading…
Reference in New Issue