Skip to content
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

feat: add handling of missing files in imports and allows you to define fallbacks #10

Merged
merged 2 commits into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/quality-action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: '1.19'
go-version: '1.23'
- name: Check out code
uses: actions/checkout@v2
- name: Install dependencies
Expand Down
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# v0.0.6-alpha

## Features

- add handling of missing files in imports and allows you to define fallbacks (#9)

## Updates

- update to go v1.23

78 changes: 73 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,11 @@ same as before but in addition with the `<query-parameter>` `exclude=**/*ignore.
</details>


### List of available custom importers and the supported prefixa
### List of available custom importers with the supported prefixa and available query-parameters

| Name | `<importer-prefix>` in `import` path | `<importer-prefix>` in `importstr` path | `<query-parameters>` |
| ---- | --- | --- | ----- |
| `MultiImporter` | any - will address the right importer | any | `logLevel=<info\|debug>`, `importGraph=<filepath>` |
| `MultiImporter` | any - will address the right importer | any | `logLevel=<info\|debug>`, `importGraph=<filepath>`, `onMissingFile=<filepath\|content>` |
| `GlobImporter` | `glob.<?>`, `glob.<?>+`, `glob+` | `glob-str.<?>`, `glob-str.<?>+`, `glob-str+` | `logLevel=<info\|debug>`, `exclude=<glob-pattern>` |

---
Expand Down Expand Up @@ -228,7 +228,11 @@ Code which will be evaluated in jsonnet:

### Logging

Enable/add a [zap.Logger](https://github.com/uber-go/zap) via `<Importer>.Logger()` **per importer** or just use the this method on the `MultiImporter` instance to enable this of all underlying custom importers:
Enable/add a [zap.Logger](https://github.com/uber-go/zap) via `<Importer>.Logger()` **per importer** or just use the this method on the `MultiImporter` instance to enable this of all underlying custom importers.


<details>
<summary><h4>details</h4></summary>

```go
import (
Expand All @@ -253,9 +257,17 @@ local myother_imports = importers + (import 'somethingElse.jsonnet');
...
```

</details>


### Aliases

Add an alias for an importer prefix, like:
Add an alias for an importer prefix.


<details>
<summary><h4>details</h4></summary>

```go
g := NewGlobImporter()
if err := g.SetAliasPrefix("glob", "glob.stem+"); err != nil {
Expand All @@ -266,11 +278,18 @@ Add an alias for an importer prefix, like:

The `SetAliasPrefix()` can be used multiple times, whereby only the last setting for an alias-prefix pair will be used.

</details>


### Import Graph

The `MultiImporter` can detect [import cycles](https://en.wikipedia.org/wiki/Circular_dependency)
and creates an *import graph* in [dot](https://www.graphviz.org/documentation/) format once it found a cycle.


<details>
<summary><h4>details</h4></summary>

In addition such *import graphs* can also be enable independently via the special *config* import inside an `jsonnet` file:

```jsonnet
Expand All @@ -288,9 +307,14 @@ Example image from [testdata/inFileConfigs/importGraph.jsonnet](testdata/inFileC

> the image was created via `dot -Tsvg -O graph.gv` command (ref. [graphviz cli tool](https://graphviz.org/doc/info/command.html))
</details>

### Ignore Import Cycles

To disable the tests and therefore any error handling for *import cycles*, you can use the following config in your *jsonnet* code:
To disable the tests and therefore any error handling for *import cycles*, you can use the following config in your *jsonnet* code.

<details>
<summary><h4>details</h4></summary>

```jsonnet
// set the import graph file name
Expand All @@ -308,6 +332,50 @@ Or directly in your go code via:
m.IgnoreImportCycles()
```

</details>

### Handling Of Missing Files

**(new in v0.0.6-alpha)** (see #9)

With that option you can specify a fallback to another file or a concrete content/string, if the file in the import couldn't be found.

<details>
<summary><h4>details</h4></summary>

#### Inside the **jsonnet** code - with content:

```jsonnet
// set a predefined file content
local importers = import 'config://set?onMissingFile="{}"';
local myother_imports = importers + (import 'possiblyMissingFile.jsonnet');
```

#### Inside the **jsonnet** code - with a different file:

```jsonnet
// set a replacement file
local importers = import 'config://set?onMissingFile=default.jsonnet';
local myother_imports = importers + (import 'possiblyMissingFile.jsonnet');
```

#### Inside the **go** code - with content:

```go
m := NewMultiImporter()
m.OnMissingFile("'{}'") // single qoutes marks a content
```
#### Inside the **go** code - with a different file:

```go
m := NewMultiImporter()
m.OnMissingFile('default.jsonnet')
```

> A more complex example can also be found in [testdata/inFileConfigs/onMissingFile_multi.jsonnet](testdata/inFileConfigs/onMissingFile_multi.jsonnet)
</details>


## Dependencies

Expand Down
4 changes: 2 additions & 2 deletions glob.go
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ func (g *GlobImporter) parse(importedPath string) (string, string, error) {
parsedURL, err := url.Parse(importedPath)
if err != nil {
return "", "",
fmt.Errorf("%w: cannot parse import '%s', error: %s",
fmt.Errorf("%w: cannot parse import '%s', error: %w",
ErrMalformedGlobPattern, importedPath, err)
}

Expand All @@ -379,7 +379,7 @@ func (g *GlobImporter) parse(importedPath string) (string, string, error) {
query, err := url.ParseQuery(parsedURL.RawQuery)
if err != nil {
return "", "",
fmt.Errorf("%w: cannot parse the query inside the import '%s', error: %s",
fmt.Errorf("%w: cannot parse the query inside the import '%s', error: %w",
ErrMalformedGlobPattern, importedPath, err)
}

Expand Down
18 changes: 8 additions & 10 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
module github.com/peterbueschel/jsonnet-custom-importers

go 1.19
go 1.23

require (
github.com/bmatcuk/doublestar/v4 v4.6.0
github.com/dominikbraun/graph v0.20.0
github.com/bmatcuk/doublestar/v4 v4.7.1
github.com/dominikbraun/graph v0.23.0
github.com/google/go-jsonnet v0.20.0
github.com/spf13/afero v1.9.5
github.com/stretchr/testify v1.8.0
go.uber.org/zap v1.24.0
github.com/spf13/afero v1.11.0
github.com/stretchr/testify v1.8.1
go.uber.org/zap v1.27.0
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
go.uber.org/atomic v1.11.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/text v0.9.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
golang.org/x/text v0.20.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
sigs.k8s.io/yaml v1.3.0 // indirect
sigs.k8s.io/yaml v1.4.0 // indirect
)
Loading
Loading