Skip to content

Commit

Permalink
v0.0.2: rename external-json to external
Browse files Browse the repository at this point in the history
Fix the docs
Improve error reporting a bit (include stderr in errors)
Update packer-plugin-sdk to v0.4.0
  • Loading branch information
ilyaluk committed Mar 10, 2023
1 parent 062438d commit 244860d
Show file tree
Hide file tree
Showing 14 changed files with 460 additions and 67 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ A plugin for [Packer](https://www.packer.io/) which provides access to external
packer {
required_plugins {
external = {
version = ">= 0.0.1"
version = ">= 0.0.2"
source = "github.com/joomcode/external"
}
}
}
data "external-json" "example" {
data "external" "example" {
program = ["jq", "{ \"foo\": .key1 }"]
query = {
key1 = "val1"
Expand All @@ -26,8 +26,8 @@ data "external-raw" "example" {
}
locals {
json_result = data.external-json.example.result["foo"] # "val1"
raw_result = data.external-raw.example.result # "olleh\n"
external_result = data.external.example.result["foo"] # "val1"
raw_result = data.external-raw.example.result # "olleh\n"
}
```

Expand Down
2 changes: 1 addition & 1 deletion datasource/json/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func (d *Datasource) Execute() (cty.Value, error) {
stderr = string(eerr.Stderr)
}
log.Printf("command failed, err=%v, stdout='%q', stderr='%q'", err, string(resultJson), stderr)
return cty.Value{}, fmt.Errorf("external program run failed: %v", err)
return cty.Value{}, fmt.Errorf("external program run failed: %v, stderr='%q'", err, stderr)
}
log.Println("command result:", string(resultJson))

Expand Down
4 changes: 2 additions & 2 deletions datasource/json/data_acc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
//go:embed test-fixtures/template.pkr.hcl
var testDatasourceHCL2Basic string

func TestAccExternalDatasource(t *testing.T) {
func TestAccExternalJsonDatasource(t *testing.T) {
testCase := &acctest.PluginTestCase{
Name: "external_datasource_basic_test",
Setup: func() error {
Expand All @@ -25,7 +25,7 @@ func TestAccExternalDatasource(t *testing.T) {
return nil
},
Template: testDatasourceHCL2Basic,
Type: "external-json",
Type: "external",
Check: func(buildCommand *exec.Cmd, logfile string) error {
if buildCommand.ProcessState != nil {
if buildCommand.ProcessState.ExitCode() != 0 {
Expand Down
4 changes: 2 additions & 2 deletions datasource/json/test-fixtures/template.pkr.hcl
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
data "external-json" "test" {
data "external" "test" {
program = ["jq", "-r", "{\"my_key1\": \"my_\\(.key1)\"}"]
query = {
key1 = "val1"
}
}

locals {
result = data.external-json.test.result
result = data.external.test.result
}

source "null" "test" {
Expand Down
2 changes: 1 addition & 1 deletion datasource/raw/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func (d *Datasource) Execute() (cty.Value, error) {
stderr = string(eerr.Stderr)
}
log.Printf("command failed, err=%v, stdout='%q', stderr='%q'", err, string(resultRaw), stderr)
return cty.Value{}, fmt.Errorf("external program run failed: %v", err)
return cty.Value{}, fmt.Errorf("external program run failed: %v, stderr='%q'", err, stderr)
}
log.Println("command result:", string(resultRaw))

Expand Down
2 changes: 1 addition & 1 deletion datasource/raw/data_acc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
//go:embed test-fixtures/template.pkr.hcl
var testDatasourceHCL2Basic string

func TestAccExternalDatasource(t *testing.T) {
func TestAccExternalRawDatasource(t *testing.T) {
testCase := &acctest.PluginTestCase{
Name: "external_datasource_basic_test",
Setup: func() error {
Expand Down
4 changes: 2 additions & 2 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ The External plugin allows to communicate with external programs

Contents:
- [index](/packer/plugins/datasources/index.mdx)
- [data source json](/packer/plugins/datasources/json.mdx)
- [data source raw](/packer/plugins/datasources/raw.mdx)
- [data source external](/packer/plugins/datasources/external.mdx)
- [data source external-raw](/packer/plugins/datasources/external-raw.mdx)

14 changes: 7 additions & 7 deletions docs/datasources/json.mdx → docs/datasources/external.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
description: >
The json data source is used to communicate with external commands
using JSON protocol.
page_title: JSON - Data Sources
nav_title: JSON
page_title: External - Data Sources
nav_title: External
---

# External JSON
# External

Type: `external-json`
Type: `external`

The `json` data source allows an external program implementing a specific
The `external` data source allows an external program implementing a specific
protocol (defined below) to act as a data source, exposing arbitrary
data for use elsewhere in the Packer configuration.

Expand All @@ -24,15 +24,15 @@ be used differently) on different operating systems.

### Example Usage
```hcl
data "external-json" "example" {
data "external" "example" {
program = ["jq", "{ \"foo\": .key1 }"]
query = {
key1 = "val1"
}
}
locals {
result = data.external-json.example.result
result = data.external.example.result
}
source "null" "example" {
Expand Down
6 changes: 3 additions & 3 deletions docs/datasources/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ sidebar_title: Overview
The External plugin is able to communicate with external commands.
The following data sources are currently supported:

- [json](/packer/plugins/datasources/external/json) - Communicate with external commands
- [external](/packer/plugins/datasources/external/external) - Communicate with external commands
using JSON protocol.
- [raw](/packer/plugins/datasources/external/raw) - Communicate with external commands
- [external-raw](/packer/plugins/datasources/external/raw) - Communicate with external commands
using plaintext protocol.

## Installation
Expand All @@ -32,7 +32,7 @@ Then, run [`packer init`](https://www.packer.io/docs/commands/init).
packer {
required_plugins {
external = {
version = ">= 0.0.1"
version = ">= 0.0.2"
source = "github.com/joomcode/external"
}
}
Expand Down
17 changes: 10 additions & 7 deletions docs/datasources/raw.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
description: >
The raw data source is used to communicate with external commands
using plaintext protocol.
page_title: Raw - Data Sources
nav_title: Raw
page_title: External Raw - Data Sources
nav_title: External Raw
---

# External Raw

Type: `external-raw`

The `raw` data source allows an external program to act as a data source,
The `external-raw` data source allows an external program to act as a data source,
exposing arbitrary data for use elsewhere in the Packer configuration.

~> **Warning** This mechanism is provided as an "escape hatch" for exceptional
Expand Down Expand Up @@ -49,12 +49,15 @@ build {

## External Program Protocol

The protocol is almost the same as the one used by the
[json](/packer/plugins/datasources/external/json) data source.
However, query and result are plaintext strings instead
of JSON objects. Please refer to the [json](/packer/plugins/datasources/external/json)
The protocol is similar to the one used by the
[external](/packer/plugins/datasources/external/external) data source.
However, query and result are plaintext strings instead of JSON objects.
Refer to the [external](/packer/plugins/datasources/external/external) doc
for more details.

`external-raw` should be used over `external` in cases where the external program
does not support JSON input and/or output.

## Argument Reference

### Required
Expand Down
15 changes: 8 additions & 7 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ go 1.17

require (
github.com/hashicorp/hcl/v2 v2.14.1
github.com/hashicorp/packer-plugin-sdk v0.3.2
github.com/hashicorp/packer-plugin-sdk v0.4.0
github.com/zclconf/go-cty v1.10.0
)

require (
github.com/agext/levenshtein v1.2.3 // indirect
github.com/apparentlymart/go-textseg/v13 v13.0.0 // indirect
github.com/armon/go-metrics v0.3.9 // indirect
github.com/aws/aws-sdk-go v1.40.34 // indirect
github.com/aws/aws-sdk-go v1.44.114 // indirect
github.com/bgentry/go-netrc v0.0.0-20140422174119-9fd32a8b3d3d // indirect
github.com/cenkalti/backoff/v3 v3.2.2 // indirect
github.com/fatih/color v1.12.0 // indirect
Expand All @@ -22,15 +22,15 @@ require (
github.com/hashicorp/consul/api v1.10.1 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
github.com/hashicorp/go-getter/v2 v2.1.0 // indirect
github.com/hashicorp/go-getter/v2 v2.2.0 // indirect
github.com/hashicorp/go-hclog v0.16.2 // indirect
github.com/hashicorp/go-immutable-radix v1.3.1 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/hashicorp/go-retryablehttp v0.7.0 // indirect
github.com/hashicorp/go-rootcerts v1.0.2 // indirect
github.com/hashicorp/go-safetemp v1.0.0 // indirect
github.com/hashicorp/go-sockaddr v1.0.2 // indirect
github.com/hashicorp/go-version v1.3.0 // indirect
github.com/hashicorp/go-version v1.6.0 // indirect
github.com/hashicorp/golang-lru v0.5.4 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/hashicorp/serf v0.9.5 // indirect
Expand All @@ -39,6 +39,7 @@ require (
github.com/hashicorp/yamux v0.0.0-20210826001029-26ff87cf9493 // indirect
github.com/jehiah/go-strftime v0.0.0-20171201141054-1d33003b3869 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/klauspost/compress v1.11.2 // indirect
github.com/mattn/go-colorable v0.1.8 // indirect
github.com/mattn/go-isatty v0.0.13 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
Expand All @@ -52,9 +53,9 @@ require (
github.com/ugorji/go/codec v1.2.6 // indirect
github.com/ulikunitz/xz v0.5.10 // indirect
golang.org/x/crypto v0.0.0-20220517005047-85d78b3ac167 // indirect
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2 // indirect
golang.org/x/sys v0.0.0-20211019181941-9d821ace8654 // indirect
golang.org/x/text v0.3.7 // indirect
golang.org/x/net v0.8.0 // indirect
golang.org/x/sys v0.6.0 // indirect
golang.org/x/text v0.8.0 // indirect
golang.org/x/time v0.0.0-20210723032227-1f47c861a9ac // indirect
gopkg.in/square/go-jose.v2 v2.6.0 // indirect
gopkg.in/yaml.v2 v2.3.0 // indirect
Expand Down
Loading

0 comments on commit 244860d

Please sign in to comment.