Skip to content

Commit

Permalink
feat: YAML data source (#266)
Browse files Browse the repository at this point in the history
  • Loading branch information
traut authored Dec 7, 2024
1 parent 2554867 commit cb3ceb4
Show file tree
Hide file tree
Showing 16 changed files with 487 additions and 13 deletions.
4 changes: 2 additions & 2 deletions docs/plugins/builtin/data-sources/csv.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
title: "`csv` data source"
plugin:
name: blackstork/builtin
description: "Loads CSV files with the names that match a provided `glob` pattern or a single file from a provided path"
description: "Loads CSV files with the names that match provided `glob` pattern or a single file from a provided path"
tags: []
version: "v0.4.2"
source_github: "https://github.com/blackstork-io/fabric/tree/main/internal/builtin/"
Expand All @@ -16,7 +16,7 @@ type: docs
{{< plugin-resource-header "blackstork/builtin" "builtin" "v0.4.2" "csv" "data source" >}}

## Description
Loads CSV files with the names that match a provided `glob` pattern or a single file from a provided path.
Loads CSV files with the names that match provided `glob` pattern or a single file from a provided path.

Either `glob` or `path` argument must be set.

Expand Down
4 changes: 2 additions & 2 deletions docs/plugins/builtin/data-sources/json.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
title: "`json` data source"
plugin:
name: blackstork/builtin
description: "Loads JSON files with the names that match a provided `glob` pattern or a single file from a provided `path`value"
description: "Loads JSON files with the names that match provided `glob` pattern or a single file from provided `path`value"
tags: []
version: "v0.4.2"
source_github: "https://github.com/blackstork-io/fabric/tree/main/internal/builtin/"
Expand All @@ -16,7 +16,7 @@ type: docs
{{< plugin-resource-header "blackstork/builtin" "builtin" "v0.4.2" "json" "data source" >}}

## Description
Loads JSON files with the names that match a provided `glob` pattern or a single file from a provided `path`value.
Loads JSON files with the names that match provided `glob` pattern or a single file from provided `path`value.

Either `glob` or `path` argument must be set.

Expand Down
4 changes: 2 additions & 2 deletions docs/plugins/builtin/data-sources/txt.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
title: "`txt` data source"
plugin:
name: blackstork/builtin
description: "Loads TXT files with the names that match a provided `glob` pattern or a single file from a provided path"
description: "Loads TXT files with the names that match provided `glob` pattern or a single file from a provided path"
tags: []
version: "v0.4.2"
source_github: "https://github.com/blackstork-io/fabric/tree/main/internal/builtin/"
Expand All @@ -16,7 +16,7 @@ type: docs
{{< plugin-resource-header "blackstork/builtin" "builtin" "v0.4.2" "txt" "data source" >}}

## Description
Loads TXT files with the names that match a provided `glob` pattern or a single file from a provided path.
Loads TXT files with the names that match provided `glob` pattern or a single file from a provided path.

Either `glob` or `path` argument must be set.

Expand Down
75 changes: 75 additions & 0 deletions docs/plugins/builtin/data-sources/yaml.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
---
title: "`yaml` data source"
plugin:
name: blackstork/builtin
description: "Loads YAML files with the names that match provided `glob` pattern or a single file from provided `path`value"
tags: []
version: "v0.4.2"
source_github: "https://github.com/blackstork-io/fabric/tree/main/internal/builtin/"
resource:
type: data-source
type: docs
---

{{< breadcrumbs 2 >}}

{{< plugin-resource-header "blackstork/builtin" "builtin" "v0.4.2" "yaml" "data source" >}}

## Description
Loads YAML files with the names that match provided `glob` pattern or a single file from provided `path`value.

Either `glob` or `path` argument must be set.

When `path` argument is specified, the data source returns only the content of a file.
When `glob` argument is specified, the data source returns a list of dicts that contain the content of a file and file's metadata. For example:

```json
[
{
"file_path": "path/file-a.yaml",
"file_name": "file-a.yaml",
"content": {
"foo": "bar"
}
},
{
"file_path": "path/file-b.yaml",
"file_name": "file-b.yaml",
"content": [
{"x": "y"}
]
}
]
```

The data source is built-in, which means it's a part of `fabric` binary. It's available out-of-the-box, no installation required.

## Configuration

The data source doesn't support any configuration arguments.

## Usage

The data source supports the following execution arguments:

```hcl
data yaml {
# A glob pattern to select YAML files to read
#
# Optional string.
# For example:
# glob = "path/to/file*.yaml"
#
# Default value:
glob = null
# A file path to a YAML file to read
#
# Optional string.
# For example:
# path = "path/to/file.yaml"
#
# Default value:
path = null
}
```
8 changes: 8 additions & 0 deletions docs/plugins/plugins.json
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,14 @@
"glob",
"path"
]
},
{
"name": "yaml",
"type": "data-source",
"arguments": [
"glob",
"path"
]
}
]
},
Expand Down
2 changes: 1 addition & 1 deletion internal/builtin/data_csv.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func makeCSVDataSource() *plugin.DataSource {
},
},
Doc: `
Loads CSV files with the names that match a provided ` + "`glob`" + ` pattern or a single file from a provided path.
Loads CSV files with the names that match provided ` + "`glob`" + ` pattern or a single file from a provided path.
Either ` + "`glob` or `path` argument must be set." + `
Expand Down
6 changes: 3 additions & 3 deletions internal/builtin/data_json.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func makeJSONDataSource() *plugin.DataSource {
},
},
Doc: `
Loads JSON files with the names that match a provided ` + "`glob`" + ` pattern or a single file from a provided ` + "`path`" + `value.
Loads JSON files with the names that match provided ` + "`glob`" + ` pattern or a single file from provided ` + "`path`" + `value.
Either ` + "`glob`" + ` or ` + "`path`" + ` argument must be set.
Expand Down Expand Up @@ -80,13 +80,13 @@ func fetchJSONData(ctx context.Context, params *plugin.RetrieveDataParams) (plug
)
return nil, diagnostics.Diag{{
Severity: hcl.DiagError,
Summary: "Failed to read a file",
Summary: "Failed to read the file",
Detail: err.Error(),
}}
}
return data, nil
} else if !glob.IsNull() && glob.AsString() != "" {
slog.Debug("Reading the files that match a glob pattern", "glob", glob.AsString())
slog.Debug("Reading the files that match the glob pattern", "glob", glob.AsString())
data, err := readJSONFiles(ctx, glob.AsString())
if err != nil {
slog.Error(
Expand Down
4 changes: 2 additions & 2 deletions internal/builtin/data_json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func Test_fetchJSONData(t *testing.T) {
path: filepath.Join("testdata", "json", "invalid.txt"),
expectedDiags: diagtest.Asserts{{
diagtest.IsError,
diagtest.SummaryEquals("Failed to read a file"),
diagtest.SummaryEquals("Failed to read the file"),
diagtest.DetailEquals("invalid character 'i' looking for beginning of object key string"),
}},
},
Expand All @@ -69,7 +69,7 @@ func Test_fetchJSONData(t *testing.T) {
path: filepath.Join("testdata", "json", "unknown_dir", "does-not-exist.json"),
expectedDiags: diagtest.Asserts{{
diagtest.IsError,
diagtest.SummaryEquals("Failed to read a file"),
diagtest.SummaryEquals("Failed to read the file"),
diagtest.DetailContains("open", "does-not-exist.json"),
}},
},
Expand Down
2 changes: 1 addition & 1 deletion internal/builtin/data_txt.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func makeTXTDataSource() *plugin.DataSource {
},
},
Doc: `
Loads TXT files with the names that match a provided ` + "`glob`" + ` pattern or a single file from a provided path.
Loads TXT files with the names that match provided ` + "`glob`" + ` pattern or a single file from a provided path.
Either ` + "`glob`" + ` or ` + "`path`" + ` argument must be set.
Expand Down
Loading

0 comments on commit cb3ceb4

Please sign in to comment.