Skip to content

Commit d98f0ca

Browse files
authored
Add new local_command data source for running and retrieving data from local executables (#452)
* initial implementation * update the config directory to work running manually * add count * add test * add tests * don't encode null arguments * add copyright headers * add documentation and example * remove the tests temporarily for the CI * lint * use rc1 for generating docs * add changelog and fix tests for windows * add vscode gitignore * refactor * first draft of data source * comments * add stdout tests * refactor * refactor + additional tests * add another test * remove comment (not needed) * update docs on schema * add example to docs * add changelog * add doc comment * add invalid working directory test * adjust OS specific tests * explicitly use absolute path, since windows is weird lol * reverting * Fix TF 0.13 errors * use pwd command * fix windows CI * more WSL fixes * skip for consistency * reformat comment
1 parent 5010019 commit d98f0ca

File tree

7 files changed

+807
-0
lines changed

7 files changed

+807
-0
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
kind: FEATURES
2+
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.'
3+
time: 2025-11-06T18:01:54.341138-05:00
4+
custom:
5+
Issue: "452"

.github/workflows/test.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@ jobs:
6868
terraform: ${{ fromJSON(vars.TF_VERSIONS_PROTOCOL_V5) }}
6969

7070
steps:
71+
# https://github.com/actions/runner-images/issues/7443
72+
- name: Install yq (windows only)
73+
if: "matrix.os == 'windows-latest'"
74+
run: choco install yq
7175

7276
- uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
7377
- uses: actions/setup-go@44694675825211faa026b3c33043df3e48a5fa00 # v6.0.0

docs/data-sources/command.md

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
---
2+
# generated by https://github.com/hashicorp/terraform-plugin-docs
3+
page_title: "local_command Data Source - terraform-provider-local"
4+
subcategory: ""
5+
description: |-
6+
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.
7+
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.
8+
~> 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.
9+
~> 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.
10+
---
11+
12+
# local_command (Data Source)
13+
14+
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.
15+
16+
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`.
17+
18+
~> **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.
19+
20+
~> **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.
21+
22+
## Example Usage
23+
24+
```terraform
25+
// A toy example using the JSON utility `jq` to process Terraform data
26+
// https://jqlang.org/
27+
data "local_command" "filter_fruit" {
28+
command = "jq"
29+
stdin = jsonencode([{ name = "apple" }, { name = "lemon" }, { name = "apricot" }])
30+
arguments = [".[:2] | [.[].name]"] # Grab the first two fruit names from the list
31+
}
32+
33+
output "fruit_tf" {
34+
value = jsondecode(data.local_command.filter_fruit.stdout)
35+
}
36+
37+
# Outputs:
38+
#
39+
# fruit_tf = [
40+
# "apple",
41+
# "lemon",
42+
# ]
43+
```
44+
45+
<!-- schema generated by tfplugindocs -->
46+
## Schema
47+
48+
### Required
49+
50+
- `command` (String) Executable name to be discovered on the PATH or absolute path to executable.
51+
52+
### Optional
53+
54+
- `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.
55+
- `arguments` (List of String) Arguments to be passed to the given command. Any `null` arguments will be removed from the list.
56+
- `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).
57+
- `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.
58+
59+
### Read-Only
60+
61+
- `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`.
62+
- `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.
63+
- `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).
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// A toy example using the JSON utility `jq` to process Terraform data
2+
// https://jqlang.org/
3+
data "local_command" "filter_fruit" {
4+
command = "jq"
5+
stdin = jsonencode([{ name = "apple" }, { name = "lemon" }, { name = "apricot" }])
6+
arguments = [".[:2] | [.[].name]"] # Grab the first two fruit names from the list
7+
}
8+
9+
output "fruit_tf" {
10+
value = jsondecode(data.local_command.filter_fruit.stdout)
11+
}
12+
13+
# Outputs:
14+
#
15+
# fruit_tf = [
16+
# "apple",
17+
# "lemon",
18+
# ]
Lines changed: 227 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,227 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: MPL-2.0
3+
4+
package provider
5+
6+
import (
7+
"bytes"
8+
"context"
9+
"fmt"
10+
"os/exec"
11+
"runtime"
12+
"strings"
13+
14+
"github.com/hashicorp/terraform-plugin-framework/datasource"
15+
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
16+
"github.com/hashicorp/terraform-plugin-framework/path"
17+
"github.com/hashicorp/terraform-plugin-framework/types"
18+
"github.com/hashicorp/terraform-plugin-log/tflog"
19+
)
20+
21+
var (
22+
_ datasource.DataSource = (*localCommandDataSource)(nil)
23+
)
24+
25+
func NewLocalCommandDataSource() datasource.DataSource {
26+
return &localCommandDataSource{}
27+
}
28+
29+
type localCommandDataSource struct{}
30+
31+
func (a *localCommandDataSource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
32+
resp.TypeName = req.ProviderTypeName + "_command"
33+
}
34+
35+
func (a *localCommandDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
36+
resp.Schema = schema.Schema{
37+
MarkdownDescription: "Runs an executable on the local machine and returns the exit code, standard output data (`stdout`), and standard error data (`stderr`). " +
38+
"All environment variables visible to the Terraform process are passed through to the child process. Both `stdout` and `stderr` returned by this data source " +
39+
"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. " +
40+
"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), " +
41+
"and more specialized [decoding functions](https://developer.hashicorp.com/terraform/plugin/framework/functions/concepts) can be built with a Terraform provider." +
42+
"\n\n" +
43+
"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. " +
44+
"If a non-zero exit code is expected by the command, set `allow_non_zero_exit_code` to `true`." +
45+
"\n\n" +
46+
"~> **Warning** This mechanism is provided as an \"escape hatch\" for exceptional situations where a first-class Terraform provider is not more appropriate. " +
47+
"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 " +
48+
"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) " +
49+
"on different operating systems." +
50+
"\n\n" +
51+
"~> **Warning** HCP Terraform and Terraform Enterprise do not guarantee availability of any particular language runtimes or external programs beyond standard shell utilities, " +
52+
"so it is not recommended to use this data source within configurations that are applied within either.",
53+
Attributes: map[string]schema.Attribute{
54+
"command": schema.StringAttribute{
55+
Description: "Executable name to be discovered on the PATH or absolute path to executable.",
56+
Required: true,
57+
},
58+
"arguments": schema.ListAttribute{
59+
MarkdownDescription: "Arguments to be passed to the given command. Any `null` arguments will be removed from the list.",
60+
ElementType: types.StringType,
61+
Optional: true,
62+
},
63+
"stdin": schema.StringAttribute{
64+
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 " +
65+
"by any Terraform encode function, for example, [`jsonencode`](https://developer.hashicorp.com/terraform/language/functions/jsonencode).",
66+
Optional: true,
67+
},
68+
"working_directory": schema.StringAttribute{
69+
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.",
70+
Optional: true,
71+
},
72+
"allow_non_zero_exit_code": schema.BoolAttribute{
73+
MarkdownDescription: "Indicates that the command returning a non-zero exit code should be treated as a successful execution. " +
74+
"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.",
75+
Optional: true,
76+
},
77+
"exit_code": schema.Int64Attribute{
78+
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. " +
79+
"If a non-zero exit code is expected by the command, set `allow_non_zero_exit_code` to `true`.",
80+
Computed: true,
81+
},
82+
"stdout": schema.StringAttribute{
83+
MarkdownDescription: "Data returned from the command's standard output stream. The data is returned directly from the command as a UTF-8 string, " +
84+
"which can then be decoded by any Terraform decode function, for example, [`jsondecode`](https://developer.hashicorp.com/terraform/language/functions/jsondecode).",
85+
Computed: true,
86+
},
87+
"stderr": schema.StringAttribute{
88+
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 " +
89+
"populated regardless of the exit code returned.",
90+
Computed: true,
91+
},
92+
},
93+
}
94+
}
95+
96+
type localCommandDataSourceModel struct {
97+
Command types.String `tfsdk:"command"`
98+
Arguments types.List `tfsdk:"arguments"`
99+
Stdin types.String `tfsdk:"stdin"`
100+
WorkingDirectory types.String `tfsdk:"working_directory"`
101+
AllowNonZeroExitCode types.Bool `tfsdk:"allow_non_zero_exit_code"`
102+
ExitCode types.Int64 `tfsdk:"exit_code"`
103+
Stdout types.String `tfsdk:"stdout"`
104+
Stderr types.String `tfsdk:"stderr"`
105+
}
106+
107+
func (a *localCommandDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
108+
var state localCommandDataSourceModel
109+
resp.Diagnostics.Append(req.Config.Get(ctx, &state)...)
110+
if resp.Diagnostics.HasError() {
111+
return
112+
}
113+
114+
// Prep the command
115+
command := state.Command.ValueString()
116+
if _, err := exec.LookPath(command); err != nil {
117+
resp.Diagnostics.AddAttributeError(
118+
path.Root("command"),
119+
"Command Lookup Failed",
120+
"The data source received an unexpected error while attempting to find the command."+
121+
"\n\n"+
122+
"The command must be accessible according to the platform where Terraform is running."+
123+
"\n\n"+
124+
"If the expected command should be automatically found on the platform where Terraform is running, "+
125+
"ensure that the command is in an expected directory. On Unix-based platforms, these directories are "+
126+
"typically searched based on the '$PATH' environment variable. On Windows-based platforms, these directories "+
127+
"are typically searched based on the '%PATH%' environment variable."+
128+
"\n\n"+
129+
"If the expected command is relative to the Terraform configuration, it is recommended that the command name includes "+
130+
"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\""+
131+
"\n\n"+
132+
"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. "+
133+
"On Windows-based platforms, no action is typically necessary."+
134+
"\n\n"+
135+
fmt.Sprintf("Platform: %s\n", runtime.GOOS)+
136+
fmt.Sprintf("Command: %s\n", command)+
137+
fmt.Sprintf("Error: %s", err),
138+
)
139+
return
140+
}
141+
142+
arguments := make([]string, 0)
143+
for _, element := range state.Arguments.Elements() {
144+
strElement, ok := element.(types.String)
145+
// Mirroring the underlying os/exec Command support for args (no nil arguments, but does support empty strings)
146+
if element.IsNull() || !ok {
147+
continue
148+
}
149+
150+
arguments = append(arguments, strElement.ValueString())
151+
}
152+
153+
cmd := exec.CommandContext(ctx, command, arguments...)
154+
155+
cmd.Dir = state.WorkingDirectory.ValueString()
156+
157+
if !state.Stdin.IsNull() {
158+
cmd.Stdin = bytes.NewReader([]byte(state.Stdin.ValueString()))
159+
}
160+
161+
var stderr strings.Builder
162+
cmd.Stderr = &stderr
163+
var stdout strings.Builder
164+
cmd.Stdout = &stdout
165+
166+
tflog.Trace(ctx, "Executing local command", map[string]interface{}{"command": cmd.String()})
167+
168+
// Run the command
169+
commandErr := cmd.Run()
170+
stdoutStr := stdout.String()
171+
stderrStr := stderr.String()
172+
173+
if len(stderrStr) > 0 {
174+
state.Stderr = types.StringValue(stderrStr)
175+
}
176+
177+
if len(stdoutStr) > 0 {
178+
state.Stdout = types.StringValue(stdoutStr)
179+
}
180+
181+
// ProcessState will always be populated if the command has been was successfully started (regardless of exit code)
182+
if cmd.ProcessState != nil {
183+
exitCode := cmd.ProcessState.ExitCode()
184+
state.ExitCode = types.Int64Value(int64(exitCode))
185+
}
186+
187+
tflog.Trace(ctx, "Executed local command", map[string]interface{}{"command": cmd.String(), "stdout": stdoutStr, "stderr": stderrStr})
188+
189+
// Set all of the data to state
190+
resp.Diagnostics.Append(resp.State.Set(ctx, state)...)
191+
if commandErr == nil {
192+
return
193+
}
194+
195+
// If running the command returned an exit error, we need to check and see if we should explicitly raise a diagnostic
196+
if exitError, ok := commandErr.(*exec.ExitError); ok {
197+
// We won't return a diagnostic because the command was successfully started and then exited
198+
// with a non-zero exit code (which the user has indicated they will handle in configuration).
199+
//
200+
// All data has already been saved to state, so we just return.
201+
if state.AllowNonZeroExitCode.ValueBool() {
202+
return
203+
}
204+
205+
resp.Diagnostics.AddAttributeError(
206+
path.Root("command"),
207+
"Command Execution Failed",
208+
"The data source executed the command but received a non-zero exit code. If a non-zero exit code is expected "+
209+
"and can be handled in configuration, set \"allow_non_zero_exit_code\" to true."+
210+
"\n\n"+
211+
fmt.Sprintf("Command: %s\n", cmd.String())+
212+
fmt.Sprintf("Command Error: %s\n", stderrStr)+
213+
fmt.Sprintf("State: %s", exitError),
214+
)
215+
return
216+
}
217+
218+
// We need to raise a diagnostic because the command wasn't successfully started and we have no exit code.
219+
resp.Diagnostics.AddAttributeError(
220+
path.Root("command"),
221+
"Command Execution Failed",
222+
"The data source received an unexpected error while attempting to execute the command."+
223+
"\n\n"+
224+
fmt.Sprintf("Command: %s\n", cmd.String())+
225+
fmt.Sprintf("State: %s", commandErr),
226+
)
227+
}

0 commit comments

Comments
 (0)