-
Notifications
You must be signed in to change notification settings - Fork 73
Add new local_command data source for running and retrieving data from local executables
#452
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
36 commits
Select commit
Hold shift + click to select a range
bd7aa5b
initial implementation
austinvalle 7025357
update the config directory to work running manually
austinvalle 232b910
add count
austinvalle bbe4230
add test
austinvalle 07aa729
add tests
austinvalle 2653d24
don't encode null arguments
austinvalle b1ca955
add copyright headers
austinvalle 9b226e4
add documentation and example
austinvalle 67c13fe
remove the tests temporarily for the CI
austinvalle f65ffa7
lint
austinvalle 9640fa9
use rc1 for generating docs
austinvalle d87fd02
add changelog and fix tests for windows
austinvalle 2b98309
add vscode gitignore
austinvalle 7e68f1a
refactor
austinvalle 3103be9
first draft of data source
austinvalle 945e12d
comments
austinvalle bcc4334
add stdout tests
austinvalle b9c3f88
refactor
austinvalle c7eb0ec
refactor + additional tests
austinvalle 483a428
add another test
austinvalle e207dfd
remove comment (not needed)
austinvalle 691c1f1
update docs on schema
austinvalle 21dd9c5
add example to docs
austinvalle 3c497b9
add changelog
austinvalle 85affd3
add doc comment
austinvalle 0bb679b
add invalid working directory test
austinvalle 112917d
Merge branch 'main' into av/local-exec-ds
austinvalle cc983c4
adjust OS specific tests
austinvalle b117e90
explicitly use absolute path, since windows is weird lol
austinvalle c5a258e
reverting
austinvalle b75aba5
Fix TF 0.13 errors
austinvalle e3f1139
use pwd command
austinvalle 7a61031
fix windows CI
austinvalle c54c7da
more WSL fixes
austinvalle 93d0089
skip for consistency
austinvalle 68f3a77
reformat comment
austinvalle File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| kind: FEATURES | ||
| body: 'data/local_command: New data source that runs an executable on the local machine and returns the exit code, standard output data, and standard error data.' | ||
| time: 2025-11-06T18:01:54.341138-05:00 | ||
| custom: | ||
| Issue: "452" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| --- | ||
| # generated by https://github.com/hashicorp/terraform-plugin-docs | ||
| page_title: "local_command Data Source - terraform-provider-local" | ||
| subcategory: "" | ||
| description: |- | ||
| Runs an executable on the local machine and returns the exit code, standard output data (stdout), and standard error data (stderr). All environment variables visible to the Terraform process are passed through to the child process. Both stdout and stderr returned by this data source are UTF-8 strings, which can be decoded into Terraform values https://developer.hashicorp.com/terraform/language/expressions/types for use elsewhere in the Terraform configuration. There are built-in decoding functions such as jsondecode https://developer.hashicorp.com/terraform/language/functions/jsondecode or yamldecode https://developer.hashicorp.com/terraform/language/functions/yamldecode, and more specialized decoding functions https://developer.hashicorp.com/terraform/plugin/framework/functions/concepts can be built with a Terraform provider. | ||
| Any non-zero exit code returned by the command will be treated as an error and will return a diagnostic to Terraform containing the stderr message if available. If a non-zero exit code is expected by the command, set allow_non_zero_exit_code to true. | ||
| ~> Warning This mechanism is provided as an "escape hatch" for exceptional situations where a first-class Terraform provider is not more appropriate. Its capabilities are limited in comparison to a true data source, and implementing a data source via a local executable is likely to hurt the portability of your Terraform configuration by creating dependencies on external programs and libraries that may not be available (or may need to be used differently) on different operating systems. | ||
| ~> Warning HCP Terraform and Terraform Enterprise do not guarantee availability of any particular language runtimes or external programs beyond standard shell utilities, so it is not recommended to use this data source within configurations that are applied within either. | ||
| --- | ||
|
|
||
| # local_command (Data Source) | ||
|
|
||
| Runs an executable on the local machine and returns the exit code, standard output data (`stdout`), and standard error data (`stderr`). All environment variables visible to the Terraform process are passed through to the child process. Both `stdout` and `stderr` returned by this data source are UTF-8 strings, which can be decoded into [Terraform values](https://developer.hashicorp.com/terraform/language/expressions/types) for use elsewhere in the Terraform configuration. There are built-in decoding functions such as [`jsondecode`](https://developer.hashicorp.com/terraform/language/functions/jsondecode) or [`yamldecode`](https://developer.hashicorp.com/terraform/language/functions/yamldecode), and more specialized [decoding functions](https://developer.hashicorp.com/terraform/plugin/framework/functions/concepts) can be built with a Terraform provider. | ||
|
|
||
| Any non-zero exit code returned by the command will be treated as an error and will return a diagnostic to Terraform containing the `stderr` message if available. If a non-zero exit code is expected by the command, set `allow_non_zero_exit_code` to `true`. | ||
|
|
||
| ~> **Warning** This mechanism is provided as an "escape hatch" for exceptional situations where a first-class Terraform provider is not more appropriate. Its capabilities are limited in comparison to a true data source, and implementing a data source via a local executable is likely to hurt the portability of your Terraform configuration by creating dependencies on external programs and libraries that may not be available (or may need to be used differently) on different operating systems. | ||
|
|
||
| ~> **Warning** HCP Terraform and Terraform Enterprise do not guarantee availability of any particular language runtimes or external programs beyond standard shell utilities, so it is not recommended to use this data source within configurations that are applied within either. | ||
|
|
||
| ## Example Usage | ||
|
|
||
| ```terraform | ||
| // A toy example using the JSON utility `jq` to process Terraform data | ||
| // https://jqlang.org/ | ||
| data "local_command" "filter_fruit" { | ||
| command = "jq" | ||
| stdin = jsonencode([{ name = "apple" }, { name = "lemon" }, { name = "apricot" }]) | ||
| arguments = [".[:2] | [.[].name]"] # Grab the first two fruit names from the list | ||
| } | ||
|
|
||
| output "fruit_tf" { | ||
| value = jsondecode(data.local_command.filter_fruit.stdout) | ||
| } | ||
|
|
||
| # Outputs: | ||
| # | ||
| # fruit_tf = [ | ||
| # "apple", | ||
| # "lemon", | ||
| # ] | ||
| ``` | ||
|
|
||
| <!-- schema generated by tfplugindocs --> | ||
| ## Schema | ||
|
|
||
| ### Required | ||
|
|
||
| - `command` (String) Executable name to be discovered on the PATH or absolute path to executable. | ||
|
|
||
| ### Optional | ||
|
|
||
| - `allow_non_zero_exit_code` (Boolean) Indicates that the command returning a non-zero exit code should be treated as a successful execution. Further assertions can be made of the `exit_code` value with the [`check` block](https://developer.hashicorp.com/terraform/language/block/check). Defaults to false. | ||
| - `arguments` (List of String) Arguments to be passed to the given command. Any `null` arguments will be removed from the list. | ||
| - `stdin` (String) Data to be passed to the given command's standard input as a UTF-8 string. [Terraform values](https://developer.hashicorp.com/terraform/language/expressions/types) can be encoded by any Terraform encode function, for example, [`jsonencode`](https://developer.hashicorp.com/terraform/language/functions/jsonencode). | ||
| - `working_directory` (String) The directory path where the command should be executed, either an absolute path or relative to the Terraform working directory. If not provided, defaults to the Terraform working directory. | ||
|
|
||
| ### Read-Only | ||
|
|
||
| - `exit_code` (Number) The exit code returned by the command. By default, if the exit code is non-zero, the data source will return a diagnostic to Terraform. If a non-zero exit code is expected by the command, set `allow_non_zero_exit_code` to `true`. | ||
| - `stderr` (String) Data returned from the command's standard error stream. The data is returned directly from the command as a UTF-8 string and will be populated regardless of the exit code returned. | ||
| - `stdout` (String) Data returned from the command's standard output stream. The data is returned directly from the command as a UTF-8 string, which can then be decoded by any Terraform decode function, for example, [`jsondecode`](https://developer.hashicorp.com/terraform/language/functions/jsondecode). |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| // A toy example using the JSON utility `jq` to process Terraform data | ||
| // https://jqlang.org/ | ||
| data "local_command" "filter_fruit" { | ||
| command = "jq" | ||
| stdin = jsonencode([{ name = "apple" }, { name = "lemon" }, { name = "apricot" }]) | ||
| arguments = [".[:2] | [.[].name]"] # Grab the first two fruit names from the list | ||
| } | ||
|
|
||
| output "fruit_tf" { | ||
| value = jsondecode(data.local_command.filter_fruit.stdout) | ||
| } | ||
|
|
||
| # Outputs: | ||
| # | ||
| # fruit_tf = [ | ||
| # "apple", | ||
| # "lemon", | ||
| # ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,227 @@ | ||
| // Copyright (c) HashiCorp, Inc. | ||
| // SPDX-License-Identifier: MPL-2.0 | ||
|
|
||
| package provider | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "context" | ||
| "fmt" | ||
| "os/exec" | ||
| "runtime" | ||
| "strings" | ||
|
|
||
| "github.com/hashicorp/terraform-plugin-framework/datasource" | ||
| "github.com/hashicorp/terraform-plugin-framework/datasource/schema" | ||
| "github.com/hashicorp/terraform-plugin-framework/path" | ||
| "github.com/hashicorp/terraform-plugin-framework/types" | ||
| "github.com/hashicorp/terraform-plugin-log/tflog" | ||
| ) | ||
|
|
||
| var ( | ||
| _ datasource.DataSource = (*localCommandDataSource)(nil) | ||
| ) | ||
|
|
||
| func NewLocalCommandDataSource() datasource.DataSource { | ||
| return &localCommandDataSource{} | ||
| } | ||
|
|
||
| type localCommandDataSource struct{} | ||
|
|
||
| func (a *localCommandDataSource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { | ||
| resp.TypeName = req.ProviderTypeName + "_command" | ||
| } | ||
|
|
||
| func (a *localCommandDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) { | ||
| resp.Schema = schema.Schema{ | ||
| MarkdownDescription: "Runs an executable on the local machine and returns the exit code, standard output data (`stdout`), and standard error data (`stderr`). " + | ||
| "All environment variables visible to the Terraform process are passed through to the child process. Both `stdout` and `stderr` returned by this data source " + | ||
| "are UTF-8 strings, which can be decoded into [Terraform values](https://developer.hashicorp.com/terraform/language/expressions/types) for use elsewhere in the Terraform configuration. " + | ||
| "There are built-in decoding functions such as [`jsondecode`](https://developer.hashicorp.com/terraform/language/functions/jsondecode) or [`yamldecode`](https://developer.hashicorp.com/terraform/language/functions/yamldecode), " + | ||
| "and more specialized [decoding functions](https://developer.hashicorp.com/terraform/plugin/framework/functions/concepts) can be built with a Terraform provider." + | ||
| "\n\n" + | ||
| "Any non-zero exit code returned by the command will be treated as an error and will return a diagnostic to Terraform containing the `stderr` message if available. " + | ||
| "If a non-zero exit code is expected by the command, set `allow_non_zero_exit_code` to `true`." + | ||
| "\n\n" + | ||
| "~> **Warning** This mechanism is provided as an \"escape hatch\" for exceptional situations where a first-class Terraform provider is not more appropriate. " + | ||
| "Its capabilities are limited in comparison to a true data source, and implementing a data source via a local executable is likely to hurt the " + | ||
| "portability of your Terraform configuration by creating dependencies on external programs and libraries that may not be available (or may need to be used differently) " + | ||
| "on different operating systems." + | ||
| "\n\n" + | ||
| "~> **Warning** HCP Terraform and Terraform Enterprise do not guarantee availability of any particular language runtimes or external programs beyond standard shell utilities, " + | ||
| "so it is not recommended to use this data source within configurations that are applied within either.", | ||
| Attributes: map[string]schema.Attribute{ | ||
| "command": schema.StringAttribute{ | ||
| Description: "Executable name to be discovered on the PATH or absolute path to executable.", | ||
| Required: true, | ||
| }, | ||
| "arguments": schema.ListAttribute{ | ||
| MarkdownDescription: "Arguments to be passed to the given command. Any `null` arguments will be removed from the list.", | ||
| ElementType: types.StringType, | ||
| Optional: true, | ||
| }, | ||
| "stdin": schema.StringAttribute{ | ||
| MarkdownDescription: "Data to be passed to the given command's standard input as a UTF-8 string. [Terraform values](https://developer.hashicorp.com/terraform/language/expressions/types) can be encoded " + | ||
| "by any Terraform encode function, for example, [`jsonencode`](https://developer.hashicorp.com/terraform/language/functions/jsonencode).", | ||
| Optional: true, | ||
| }, | ||
| "working_directory": schema.StringAttribute{ | ||
| Description: "The directory path where the command should be executed, either an absolute path or relative to the Terraform working directory. If not provided, defaults to the Terraform working directory.", | ||
| Optional: true, | ||
| }, | ||
| "allow_non_zero_exit_code": schema.BoolAttribute{ | ||
| MarkdownDescription: "Indicates that the command returning a non-zero exit code should be treated as a successful execution. " + | ||
| "Further assertions can be made of the `exit_code` value with the [`check` block](https://developer.hashicorp.com/terraform/language/block/check). Defaults to false.", | ||
| Optional: true, | ||
| }, | ||
| "exit_code": schema.Int64Attribute{ | ||
| MarkdownDescription: "The exit code returned by the command. By default, if the exit code is non-zero, the data source will return a diagnostic to Terraform. " + | ||
| "If a non-zero exit code is expected by the command, set `allow_non_zero_exit_code` to `true`.", | ||
| Computed: true, | ||
| }, | ||
| "stdout": schema.StringAttribute{ | ||
| MarkdownDescription: "Data returned from the command's standard output stream. The data is returned directly from the command as a UTF-8 string, " + | ||
| "which can then be decoded by any Terraform decode function, for example, [`jsondecode`](https://developer.hashicorp.com/terraform/language/functions/jsondecode).", | ||
| Computed: true, | ||
| }, | ||
| "stderr": schema.StringAttribute{ | ||
| Description: "Data returned from the command's standard error stream. The data is returned directly from the command as a UTF-8 string and will be " + | ||
| "populated regardless of the exit code returned.", | ||
| Computed: true, | ||
| }, | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| type localCommandDataSourceModel struct { | ||
| Command types.String `tfsdk:"command"` | ||
| Arguments types.List `tfsdk:"arguments"` | ||
| Stdin types.String `tfsdk:"stdin"` | ||
| WorkingDirectory types.String `tfsdk:"working_directory"` | ||
| AllowNonZeroExitCode types.Bool `tfsdk:"allow_non_zero_exit_code"` | ||
| ExitCode types.Int64 `tfsdk:"exit_code"` | ||
| Stdout types.String `tfsdk:"stdout"` | ||
| Stderr types.String `tfsdk:"stderr"` | ||
| } | ||
|
|
||
| func (a *localCommandDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { | ||
| var state localCommandDataSourceModel | ||
| resp.Diagnostics.Append(req.Config.Get(ctx, &state)...) | ||
| if resp.Diagnostics.HasError() { | ||
| return | ||
| } | ||
|
|
||
| // Prep the command | ||
| command := state.Command.ValueString() | ||
| if _, err := exec.LookPath(command); err != nil { | ||
| resp.Diagnostics.AddAttributeError( | ||
| path.Root("command"), | ||
| "Command Lookup Failed", | ||
| "The data source received an unexpected error while attempting to find the command."+ | ||
| "\n\n"+ | ||
| "The command must be accessible according to the platform where Terraform is running."+ | ||
| "\n\n"+ | ||
| "If the expected command should be automatically found on the platform where Terraform is running, "+ | ||
| "ensure that the command is in an expected directory. On Unix-based platforms, these directories are "+ | ||
| "typically searched based on the '$PATH' environment variable. On Windows-based platforms, these directories "+ | ||
| "are typically searched based on the '%PATH%' environment variable."+ | ||
| "\n\n"+ | ||
| "If the expected command is relative to the Terraform configuration, it is recommended that the command name includes "+ | ||
| "the interpolated value of 'path.module' before the command name to ensure that it is compatible with varying module usage. For example: \"${path.module}/my-command\""+ | ||
| "\n\n"+ | ||
| "The command must also be executable according to the platform where Terraform is running. On Unix-based platforms, the file on the filesystem must have the executable bit set. "+ | ||
| "On Windows-based platforms, no action is typically necessary."+ | ||
| "\n\n"+ | ||
| fmt.Sprintf("Platform: %s\n", runtime.GOOS)+ | ||
| fmt.Sprintf("Command: %s\n", command)+ | ||
| fmt.Sprintf("Error: %s", err), | ||
| ) | ||
| return | ||
| } | ||
|
|
||
| arguments := make([]string, 0) | ||
| for _, element := range state.Arguments.Elements() { | ||
| strElement, ok := element.(types.String) | ||
| // Mirroring the underlying os/exec Command support for args (no nil arguments, but does support empty strings) | ||
| if element.IsNull() || !ok { | ||
| continue | ||
| } | ||
|
|
||
| arguments = append(arguments, strElement.ValueString()) | ||
| } | ||
|
|
||
| cmd := exec.CommandContext(ctx, command, arguments...) | ||
|
|
||
| cmd.Dir = state.WorkingDirectory.ValueString() | ||
austinvalle marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| if !state.Stdin.IsNull() { | ||
| cmd.Stdin = bytes.NewReader([]byte(state.Stdin.ValueString())) | ||
| } | ||
|
|
||
| var stderr strings.Builder | ||
| cmd.Stderr = &stderr | ||
| var stdout strings.Builder | ||
| cmd.Stdout = &stdout | ||
|
|
||
| tflog.Trace(ctx, "Executing local command", map[string]interface{}{"command": cmd.String()}) | ||
|
|
||
| // Run the command | ||
| commandErr := cmd.Run() | ||
| stdoutStr := stdout.String() | ||
| stderrStr := stderr.String() | ||
|
|
||
| if len(stderrStr) > 0 { | ||
| state.Stderr = types.StringValue(stderrStr) | ||
| } | ||
|
|
||
| if len(stdoutStr) > 0 { | ||
| state.Stdout = types.StringValue(stdoutStr) | ||
| } | ||
|
|
||
| // ProcessState will always be populated if the command has been was successfully started (regardless of exit code) | ||
| if cmd.ProcessState != nil { | ||
| exitCode := cmd.ProcessState.ExitCode() | ||
| state.ExitCode = types.Int64Value(int64(exitCode)) | ||
| } | ||
|
|
||
| tflog.Trace(ctx, "Executed local command", map[string]interface{}{"command": cmd.String(), "stdout": stdoutStr, "stderr": stderrStr}) | ||
|
|
||
| // Set all of the data to state | ||
| resp.Diagnostics.Append(resp.State.Set(ctx, state)...) | ||
| if commandErr == nil { | ||
| return | ||
| } | ||
|
|
||
| // If running the command returned an exit error, we need to check and see if we should explicitly raise a diagnostic | ||
| if exitError, ok := commandErr.(*exec.ExitError); ok { | ||
| // We won't return a diagnostic because the command was successfully started and then exited | ||
| // with a non-zero exit code (which the user has indicated they will handle in configuration). | ||
| // | ||
| // All data has already been saved to state, so we just return. | ||
| if state.AllowNonZeroExitCode.ValueBool() { | ||
| return | ||
| } | ||
|
|
||
| resp.Diagnostics.AddAttributeError( | ||
| path.Root("command"), | ||
| "Command Execution Failed", | ||
| "The data source executed the command but received a non-zero exit code. If a non-zero exit code is expected "+ | ||
| "and can be handled in configuration, set \"allow_non_zero_exit_code\" to true."+ | ||
| "\n\n"+ | ||
| fmt.Sprintf("Command: %s\n", cmd.String())+ | ||
| fmt.Sprintf("Command Error: %s\n", stderrStr)+ | ||
| fmt.Sprintf("State: %s", exitError), | ||
| ) | ||
| return | ||
| } | ||
|
|
||
| // We need to raise a diagnostic because the command wasn't successfully started and we have no exit code. | ||
| resp.Diagnostics.AddAttributeError( | ||
| path.Root("command"), | ||
| "Command Execution Failed", | ||
| "The data source received an unexpected error while attempting to execute the command."+ | ||
| "\n\n"+ | ||
| fmt.Sprintf("Command: %s\n", cmd.String())+ | ||
| fmt.Sprintf("State: %s", commandErr), | ||
| ) | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.