From 93860966ab73bb43cb02f45da234b71bedc4ed44 Mon Sep 17 00:00:00 2001 From: rsteube Date: Wed, 29 Nov 2023 14:24:04 +0100 Subject: [PATCH] added benthos --- completers/benthos_completer/cmd/blobl.go | 27 +++++ .../benthos_completer/cmd/blobl_server.go | 31 ++++++ completers/benthos_completer/cmd/create.go | 19 ++++ completers/benthos_completer/cmd/echo.go | 18 ++++ completers/benthos_completer/cmd/help.go | 23 ++++ completers/benthos_completer/cmd/lint.go | 25 +++++ completers/benthos_completer/cmd/list.go | 40 +++++++ completers/benthos_completer/cmd/root.go | 41 +++++++ completers/benthos_completer/cmd/streams.go | 24 +++++ completers/benthos_completer/cmd/studio.go | 19 ++++ .../benthos_completer/cmd/studio_pull.go | 23 ++++ .../cmd/studio_syncSchema.go | 20 ++++ completers/benthos_completer/cmd/template.go | 18 ++++ .../benthos_completer/cmd/template_lint.go | 22 ++++ completers/benthos_completer/cmd/test.go | 28 +++++ completers/benthos_completer/main.go | 7 ++ pkg/actions/tools/benthos/component.go | 100 ++++++++++++++++++ 17 files changed, 485 insertions(+) create mode 100644 completers/benthos_completer/cmd/blobl.go create mode 100644 completers/benthos_completer/cmd/blobl_server.go create mode 100644 completers/benthos_completer/cmd/create.go create mode 100644 completers/benthos_completer/cmd/echo.go create mode 100644 completers/benthos_completer/cmd/help.go create mode 100644 completers/benthos_completer/cmd/lint.go create mode 100644 completers/benthos_completer/cmd/list.go create mode 100644 completers/benthos_completer/cmd/root.go create mode 100644 completers/benthos_completer/cmd/streams.go create mode 100644 completers/benthos_completer/cmd/studio.go create mode 100644 completers/benthos_completer/cmd/studio_pull.go create mode 100644 completers/benthos_completer/cmd/studio_syncSchema.go create mode 100644 completers/benthos_completer/cmd/template.go create mode 100644 completers/benthos_completer/cmd/template_lint.go create mode 100644 completers/benthos_completer/cmd/test.go create mode 100644 completers/benthos_completer/main.go create mode 100644 pkg/actions/tools/benthos/component.go diff --git a/completers/benthos_completer/cmd/blobl.go b/completers/benthos_completer/cmd/blobl.go new file mode 100644 index 0000000000..2187516baf --- /dev/null +++ b/completers/benthos_completer/cmd/blobl.go @@ -0,0 +1,27 @@ +package cmd + +import ( + "github.com/rsteube/carapace" + "github.com/spf13/cobra" +) + +var bloblCmd = &cobra.Command{ + Use: "blobl ", + Short: "Execute a Bloblang mapping on documents consumed via stdin", + Run: func(cmd *cobra.Command, args []string) {}, +} + +func init() { + carapace.Gen(bloblCmd).Standalone() + + bloblCmd.Flags().StringP("file", "f", "", "execute a mapping from a file.") + bloblCmd.Flags().String("max-token-length", "", "Set the buffer size for document lines.") + bloblCmd.Flags().BoolP("pretty", "p", false, "pretty-print output.") + bloblCmd.Flags().BoolP("raw", "r", false, "consume raw strings.") + bloblCmd.Flags().StringP("threads", "t", "", "the number of processing threads to use, when >1 ordering is no longer guaranteed.") + rootCmd.AddCommand(bloblCmd) + + carapace.Gen(bloblCmd).FlagCompletion(carapace.ActionMap{ + "file": carapace.ActionFiles(), + }) +} diff --git a/completers/benthos_completer/cmd/blobl_server.go b/completers/benthos_completer/cmd/blobl_server.go new file mode 100644 index 0000000000..26725b6c2a --- /dev/null +++ b/completers/benthos_completer/cmd/blobl_server.go @@ -0,0 +1,31 @@ +package cmd + +import ( + "github.com/rsteube/carapace" + "github.com/rsteube/carapace-bin/pkg/actions/net" + "github.com/spf13/cobra" +) + +var blobl_serverCmd = &cobra.Command{ + Use: "server", + Short: "EXPERIMENTAL: Run a web server that hosts a Bloblang app", + Run: func(cmd *cobra.Command, args []string) {}, +} + +func init() { + carapace.Gen(blobl_serverCmd).Standalone() + + blobl_serverCmd.Flags().String("host", "", "the host to bind to.") + blobl_serverCmd.Flags().StringP("input-file", "i", "", "an optional path to an input file to load as the initial input to the mapping within the app.") + blobl_serverCmd.Flags().StringP("mapping-file", "m", "", "an optional path to a mapping file to load as the initial mapping within the app.") + blobl_serverCmd.Flags().BoolP("no-open", "n", false, "do not open the app in the browser automatically.") + blobl_serverCmd.Flags().StringP("port", "p", "", "the port to bind to.") + blobl_serverCmd.Flags().BoolP("write", "w", false, "when editing a mapping and/or input file write changes made back to the respective source file, if the file does not exist it will be created.") + bloblCmd.AddCommand(blobl_serverCmd) + + carapace.Gen(blobl_serverCmd).FlagCompletion(carapace.ActionMap{ + "input-file": carapace.ActionFiles(), + "mapping-file": carapace.ActionFiles(), + "port": net.ActionPorts(), + }) +} diff --git a/completers/benthos_completer/cmd/create.go b/completers/benthos_completer/cmd/create.go new file mode 100644 index 0000000000..bcd21d255a --- /dev/null +++ b/completers/benthos_completer/cmd/create.go @@ -0,0 +1,19 @@ +package cmd + +import ( + "github.com/rsteube/carapace" + "github.com/spf13/cobra" +) + +var createCmd = &cobra.Command{ + Use: "create", + Short: "Create a new Benthos config", + Run: func(cmd *cobra.Command, args []string) {}, +} + +func init() { + carapace.Gen(createCmd).Standalone() + + createCmd.Flags().BoolP("small", "s", false, "Print only the main components of a Benthos config (input, pipeline, output) and omit all fields marked as advanced.") + rootCmd.AddCommand(createCmd) +} diff --git a/completers/benthos_completer/cmd/echo.go b/completers/benthos_completer/cmd/echo.go new file mode 100644 index 0000000000..b95e8692ce --- /dev/null +++ b/completers/benthos_completer/cmd/echo.go @@ -0,0 +1,18 @@ +package cmd + +import ( + "github.com/rsteube/carapace" + "github.com/spf13/cobra" +) + +var echoCmd = &cobra.Command{ + Use: "echo", + Short: "Parse a config file and echo back a normalised version", + Run: func(cmd *cobra.Command, args []string) {}, +} + +func init() { + carapace.Gen(echoCmd).Standalone() + + rootCmd.AddCommand(echoCmd) +} diff --git a/completers/benthos_completer/cmd/help.go b/completers/benthos_completer/cmd/help.go new file mode 100644 index 0000000000..30291aaabb --- /dev/null +++ b/completers/benthos_completer/cmd/help.go @@ -0,0 +1,23 @@ +package cmd + +import ( + "github.com/rsteube/carapace" + "github.com/spf13/cobra" +) + +var helpCmd = &cobra.Command{ + Use: "help", + Short: "Shows a list of commands or help for one command", + Aliases: []string{"h"}, + Run: func(cmd *cobra.Command, args []string) {}, +} + +func init() { + carapace.Gen(helpCmd).Standalone() + + rootCmd.AddCommand(helpCmd) + + carapace.Gen(helpCmd).PositionalAnyCompletion( + carapace.ActionCommands(rootCmd), + ) +} diff --git a/completers/benthos_completer/cmd/lint.go b/completers/benthos_completer/cmd/lint.go new file mode 100644 index 0000000000..266d2cd8cd --- /dev/null +++ b/completers/benthos_completer/cmd/lint.go @@ -0,0 +1,25 @@ +package cmd + +import ( + "github.com/rsteube/carapace" + "github.com/spf13/cobra" +) + +var lintCmd = &cobra.Command{ + Use: "lint", + Short: "Parse Benthos configs and report any linting errors", + Run: func(cmd *cobra.Command, args []string) {}, +} + +func init() { + carapace.Gen(lintCmd).Standalone() + + lintCmd.Flags().Bool("deprecated", false, "Print linting errors for the presence of deprecated fields.") + lintCmd.Flags().Bool("labels", false, "Print linting errors when components do not have labels.") + lintCmd.Flags().Bool("skip-env-var-check", false, "Do not produce lint errors when environment interpolations exist without defaults within configs but aren't defined.") + rootCmd.AddCommand(lintCmd) + + carapace.Gen(lintCmd).PositionalAnyCompletion( + carapace.ActionFiles(), + ) +} diff --git a/completers/benthos_completer/cmd/list.go b/completers/benthos_completer/cmd/list.go new file mode 100644 index 0000000000..281f23cff4 --- /dev/null +++ b/completers/benthos_completer/cmd/list.go @@ -0,0 +1,40 @@ +package cmd + +import ( + "github.com/rsteube/carapace" + "github.com/spf13/cobra" +) + +var listCmd = &cobra.Command{ + Use: "list", + Short: "List all Benthos component types", + Run: func(cmd *cobra.Command, args []string) {}, +} + +func init() { + carapace.Gen(listCmd).Standalone() + + listCmd.Flags().String("format", "", "Print the component list in a specific format. Options are text, json or cue.") + listCmd.Flags().String("status", "", "Filter the component list to only those matching the given status. Options are stable, beta or experimental.") + rootCmd.AddCommand(listCmd) + + carapace.Gen(listCmd).FlagCompletion(carapace.ActionMap{ + "format": carapace.ActionValues("text", "json", "cue"), + "status": carapace.ActionValues("stable", "beta", "experimental"), + }) + + carapace.Gen(listCmd).PositionalAnyCompletion( + carapace.ActionValues( + "bloblang-functions", + "bloblang-methods", + "buffers", + "caches", + "inputs", + "metrics", + "outputs", + "processors", + "rate-limits", + "tracers", + ).FilterArgs(), + ) +} diff --git a/completers/benthos_completer/cmd/root.go b/completers/benthos_completer/cmd/root.go new file mode 100644 index 0000000000..31d2ffa3af --- /dev/null +++ b/completers/benthos_completer/cmd/root.go @@ -0,0 +1,41 @@ +package cmd + +import ( + "github.com/rsteube/carapace" + "github.com/rsteube/carapace/pkg/style" + "github.com/spf13/cobra" +) + +var rootCmd = &cobra.Command{ + Use: "benthos", + Short: "", + Run: func(cmd *cobra.Command, args []string) {}, +} + +func Execute() error { + return rootCmd.Execute() +} + +func init() { + carapace.Gen(rootCmd).Standalone() + + rootCmd.Flags().Bool("chilled", false, "continue to execute a config containing linter errors") + rootCmd.Flags().StringP("config", "c", "", "a path to a configuration file") + rootCmd.Flags().StringP("env-file", "e", "", "import environment variables from a dotenv file") + rootCmd.Flags().BoolP("help", "h", false, "show help") + rootCmd.Flags().String("log.level", "", "override the configured log level, options are: off, error, warn, info, debug, trace") + rootCmd.Flags().StringP("resources", "r", "", "pull in extra resources from a file, which can be referenced the same as resources defined in the main config, supports glob patterns (requires quotes)") + rootCmd.Flags().StringP("set", "s", "", "set a field (identified by a dot path) in the main configuration file, e.g. `\"metrics.type=prometheus\"`") + rootCmd.Flags().StringP("templates", "t", "", "EXPERIMENTAL: import Benthos templates, supports glob patterns (requires quotes)") + rootCmd.Flags().BoolP("version", "v", false, "display version info, then exit") + rootCmd.Flags().BoolP("watcher", "w", false, "EXPERIMENTAL: watch config files for changes and automatically apply them") + + carapace.Gen(rootCmd).FlagCompletion(carapace.ActionMap{ + "config": carapace.ActionFiles(), + "env-file": carapace.ActionFiles(), + "log.level": carapace.ActionValues("off", "error", "warn", "info", "debug", "trace").StyleF(style.ForLogLevel), + "resources": carapace.ActionValues(), // TODO + "set": carapace.ActionValues(), // TODO + "templates": carapace.ActionValues(), // TODO + }) +} diff --git a/completers/benthos_completer/cmd/streams.go b/completers/benthos_completer/cmd/streams.go new file mode 100644 index 0000000000..f08931daf7 --- /dev/null +++ b/completers/benthos_completer/cmd/streams.go @@ -0,0 +1,24 @@ +package cmd + +import ( + "github.com/rsteube/carapace" + "github.com/spf13/cobra" +) + +var streamsCmd = &cobra.Command{ + Use: "streams", + Short: "Run Benthos in streams mode", + Run: func(cmd *cobra.Command, args []string) {}, +} + +func init() { + carapace.Gen(streamsCmd).Standalone() + + streamsCmd.Flags().Bool("no-api", false, "Disable the HTTP API for streams mode") + streamsCmd.Flags().Bool("prefix-stream-endpoints", false, "Whether HTTP endpoints registered by stream configs should be prefixed with the stream ID") + rootCmd.AddCommand(streamsCmd) + + carapace.Gen(streamsCmd).PositionalAnyCompletion( + carapace.ActionFiles(), + ) +} diff --git a/completers/benthos_completer/cmd/studio.go b/completers/benthos_completer/cmd/studio.go new file mode 100644 index 0000000000..4ca3b57508 --- /dev/null +++ b/completers/benthos_completer/cmd/studio.go @@ -0,0 +1,19 @@ +package cmd + +import ( + "github.com/rsteube/carapace" + "github.com/spf13/cobra" +) + +var studioCmd = &cobra.Command{ + Use: "studio", + Short: "Interact with Benthos studio (https://studio.benthos.dev)", + Run: func(cmd *cobra.Command, args []string) {}, +} + +func init() { + carapace.Gen(studioCmd).Standalone() + + studioCmd.Flags().StringP("endpoint", "e", "", "Specify the URL of the Benthos studio server to connect to.") + rootCmd.AddCommand(studioCmd) +} diff --git a/completers/benthos_completer/cmd/studio_pull.go b/completers/benthos_completer/cmd/studio_pull.go new file mode 100644 index 0000000000..a1fb5ea54d --- /dev/null +++ b/completers/benthos_completer/cmd/studio_pull.go @@ -0,0 +1,23 @@ +package cmd + +import ( + "github.com/rsteube/carapace" + "github.com/spf13/cobra" +) + +var studio_pullCmd = &cobra.Command{ + Use: "pull", + Short: "Run deployments configured within a Benthos Studio session", + Run: func(cmd *cobra.Command, args []string) {}, +} + +func init() { + carapace.Gen(studio_pullCmd).Standalone() + + studio_pullCmd.Flags().String("name", "", "An explicit name to adopt in this instance, used to identify its connection to the session. Each running node must have a unique name, if left unset a name is generated each time the command is run.") + studio_pullCmd.Flags().Bool("send-traces", false, "Whether to send trace data back to Studio during execution. This is opt-in and is used as a way to add trace events to the graph editor for testing and debugging configs. This is a very useful feature but should be used with caution as it exports information about messages passing through the stream.") + studio_pullCmd.Flags().StringP("session", "s", "", "The session ID to synchronise with.") + studio_pullCmd.Flags().String("token", "", "A token for the session, used to authenticate requests. If left blank the environment variable BSTDIO_NODE_TOKEN will be used instead.") + studio_pullCmd.Flags().String("token-secret", "", "A token secret the session, used to authenticate requests. If left blank the environment variable BSTDIO_NODE_SECRET will be used instead.") + studioCmd.AddCommand(studio_pullCmd) +} diff --git a/completers/benthos_completer/cmd/studio_syncSchema.go b/completers/benthos_completer/cmd/studio_syncSchema.go new file mode 100644 index 0000000000..e0a84078e5 --- /dev/null +++ b/completers/benthos_completer/cmd/studio_syncSchema.go @@ -0,0 +1,20 @@ +package cmd + +import ( + "github.com/rsteube/carapace" + "github.com/spf13/cobra" +) + +var studio_syncSchemaCmd = &cobra.Command{ + Use: "sync-schema", + Short: "Synchronizes the schema of this Benthos instance with a studio session", + Run: func(cmd *cobra.Command, args []string) {}, +} + +func init() { + carapace.Gen(studio_syncSchemaCmd).Standalone() + + studio_syncSchemaCmd.Flags().StringP("session", "s", "", "The session ID to synchronize with.") + studio_syncSchemaCmd.Flags().StringP("token", "t", "", "The single use token used to authenticate the request.") + studioCmd.AddCommand(studio_syncSchemaCmd) +} diff --git a/completers/benthos_completer/cmd/template.go b/completers/benthos_completer/cmd/template.go new file mode 100644 index 0000000000..f580788eef --- /dev/null +++ b/completers/benthos_completer/cmd/template.go @@ -0,0 +1,18 @@ +package cmd + +import ( + "github.com/rsteube/carapace" + "github.com/spf13/cobra" +) + +var templateCmd = &cobra.Command{ + Use: "template", + Short: "Interact and generate Benthos templates", + Run: func(cmd *cobra.Command, args []string) {}, +} + +func init() { + carapace.Gen(templateCmd).Standalone() + + rootCmd.AddCommand(templateCmd) +} diff --git a/completers/benthos_completer/cmd/template_lint.go b/completers/benthos_completer/cmd/template_lint.go new file mode 100644 index 0000000000..8651c49f0d --- /dev/null +++ b/completers/benthos_completer/cmd/template_lint.go @@ -0,0 +1,22 @@ +package cmd + +import ( + "github.com/rsteube/carapace" + "github.com/spf13/cobra" +) + +var template_lintCmd = &cobra.Command{ + Use: "lint", + Short: "Parse Benthos templates and report any linting errors", + Run: func(cmd *cobra.Command, args []string) {}, +} + +func init() { + carapace.Gen(template_lintCmd).Standalone() + + templateCmd.AddCommand(template_lintCmd) + + carapace.Gen(template_lintCmd).PositionalAnyCompletion( + carapace.ActionFiles(), + ) +} diff --git a/completers/benthos_completer/cmd/test.go b/completers/benthos_completer/cmd/test.go new file mode 100644 index 0000000000..297343368e --- /dev/null +++ b/completers/benthos_completer/cmd/test.go @@ -0,0 +1,28 @@ +package cmd + +import ( + "github.com/rsteube/carapace" + "github.com/rsteube/carapace/pkg/style" + "github.com/spf13/cobra" +) + +var testCmd = &cobra.Command{ + Use: "test", + Short: "Execute Benthos unit tests", + Run: func(cmd *cobra.Command, args []string) {}, +} + +func init() { + carapace.Gen(testCmd).Standalone() + + testCmd.Flags().String("log", "", "allow components to write logs at a provided level to stdout.") + rootCmd.AddCommand(testCmd) + + carapace.Gen(testCmd).FlagCompletion(carapace.ActionMap{ + "log": carapace.ActionValues("error", "warn", "info", "debug", "trace").StyleF(style.ForLogLevel), + }) + + carapace.Gen(testCmd).PositionalAnyCompletion( + carapace.ActionFiles(), + ) +} diff --git a/completers/benthos_completer/main.go b/completers/benthos_completer/main.go new file mode 100644 index 0000000000..d2d2ccff07 --- /dev/null +++ b/completers/benthos_completer/main.go @@ -0,0 +1,7 @@ +package main + +import "github.com/rsteube/carapace-bin/completers/benthos_completer/cmd" + +func main() { + cmd.Execute() +} diff --git a/pkg/actions/tools/benthos/component.go b/pkg/actions/tools/benthos/component.go new file mode 100644 index 0000000000..2ebe4d6c07 --- /dev/null +++ b/pkg/actions/tools/benthos/component.go @@ -0,0 +1,100 @@ +package benthos + +import ( + "encoding/json" + + "github.com/rsteube/carapace" +) + +type components struct { + BloblangFunctions []string `json:"bloblang-functions"` + BloblangMethods []string `json:"bloblang-methods"` + Buffers []string `json:"buffers"` + Caches []string `json:"caches"` + Inputs []string `json:"inputs"` + Metrics []string `json:"metrics"` + Outputs []string `json:"outputs"` + Processors []string `json:"processors"` + RateLimits []string `json:"rate-limits"` + Tracers []string `json:"tracers"` +} + +func actionComponents(f func(c components) carapace.Action) carapace.Action { + return carapace.ActionExecCommand("benthos", "list", "--format", "json")(func(output []byte) carapace.Action { + var c components + if err := json.Unmarshal(output, &c); err != nil { + return carapace.ActionMessage(err.Error()) + } + return f(c) + }) +} + +// ActionBoblangFunctions completes boblang functions +func ActionBoblangFunctions() carapace.Action { + return actionComponents(func(c components) carapace.Action { + return carapace.ActionValues(c.BloblangFunctions...) + }).Tag("boblang functions") +} + +// ActionBoblangMethods completes boblang methods +func ActionBoblangMethods() carapace.Action { + return actionComponents(func(c components) carapace.Action { + return carapace.ActionValues(c.BloblangMethods...) + }).Tag("boblang methods") +} + +// ActionBuffers completes buffers +func ActionBuffers() carapace.Action { + return actionComponents(func(c components) carapace.Action { + return carapace.ActionValues(c.Buffers...) + }).Tag("buffers") +} + +// ActionCaches completes caches +func ActionCaches() carapace.Action { + return actionComponents(func(c components) carapace.Action { + return carapace.ActionValues(c.Caches...) + }).Tag("caches") +} + +// ActionInputs completes inputs +func ActionInputs() carapace.Action { + return actionComponents(func(c components) carapace.Action { + return carapace.ActionValues(c.Inputs...) + }).Tag("inputs") +} + +// ActionMetrics completes metrics +func ActionMetrics() carapace.Action { + return actionComponents(func(c components) carapace.Action { + return carapace.ActionValues(c.Metrics...) + }).Tag("metrics") +} + +// ActionOutputs completes outputs +func ActionOutputs() carapace.Action { + return actionComponents(func(c components) carapace.Action { + return carapace.ActionValues(c.Outputs...) + }).Tag("outputs") +} + +// ActionProcessors completes processors +func ActionProcessors() carapace.Action { + return actionComponents(func(c components) carapace.Action { + return carapace.ActionValues(c.Processors...) + }).Tag("processors") +} + +// ActionRateLimits completes rate limits +func ActionRateLimits() carapace.Action { + return actionComponents(func(c components) carapace.Action { + return carapace.ActionValues(c.RateLimits...) + }).Tag("rate limits") +} + +// ActionTracers completes tracers +func ActionTracers() carapace.Action { + return actionComponents(func(c components) carapace.Action { + return carapace.ActionValues(c.Tracers...) + }).Tag("tracers") +}