Skip to content

Commit

Permalink
fix: extend empty file
Browse files Browse the repository at this point in the history
  • Loading branch information
fixcik committed May 8, 2024
1 parent a56b910 commit 2b6921a
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 12 deletions.
38 changes: 38 additions & 0 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
name: Release
on:
push:
tags:
- v[0-9]+.*

permissions:
contents: write

jobs:
create-release:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: taiki-e/create-gh-release-action@v1
with:
token: ${{ secrets.GITHUB_TOKEN }}
upload-assets:
needs: create-release
strategy:
matrix:
include:
- target: x86_64-unknown-linux-gnu
os: ubuntu-latest
- target: x86_64-apple-darwin
os: macos-latest
- target: x86_64-pc-windows-msvc
os: windows-latest
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v4
- uses: taiki-e/upload-rust-binary-action@v1
with:
bin: yexp
target: ${{ matrix.target }}
tar: unix
zip: windows
token: ${{ secrets.GITHUB_TOKEN }}
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "yexp"
version = "0.2.1"
version = "0.2.2"
edition = "2021"

license = "GPL-3.0"
Expand Down
25 changes: 19 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
`yexp` is a small tool for expand yaml files


- [Examples](#examples)
- [Installation](#installation)
- [Prebuilt Binaries](#prebuilt-binaries)
- [Via cargo](#via-cargo)
- [Via homebrew](#via-homebrew)

## Examples


`a.yaml`

```yaml
- one
- two
- three
```
`b.yaml`

```yaml
items: !include path/to/a.yaml
```
Expand All @@ -25,23 +27,34 @@ items: !include path/to/a.yaml
```yaml
foo: bar
extend: # <- can be string or sequence of strings
- path/to/b.yaml
- path/to/b.yaml
```

`yexp /path/to/c.yaml` outputs:

```yaml
foo: bar
items:
- one
- two
- three
- one
- two
- three
```

## Installation

### Prebuilt Binaries

Download the latest releases from the [GitHub release page](https://github.com/fixcik/yexp/releases).

### Via cargo

```bash
cargo install yexp
```

### Via homebrew

```bash
brew tap fixcik/tap
brew install yexp
```
18 changes: 14 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,14 @@ fn handle_extends<P: AsRef<Path>>(mut map: Mapping, path: P) -> anyhow::Result<M
for extend_path in s {
let extend_path: PathBuf = serde_yaml::from_value(extend_path)?;
let extend_path = from_relative_path(extend_path, dir)?;
let extended_values = handle_mapping_yaml(extend_path)?;
ext_map = merge_mapping(&extended_values, &ext_map);
let extended_values = handle_yaml(&extend_path)?;
match extended_values {
Value::Mapping(extended_values) => {
ext_map = merge_mapping(&extended_values, &ext_map);
}
Value::Null => {}
_ => bail!("Expected a mapping in {}", extend_path.display()),
}
}
map = merge_mapping(&map, &ext_map);
}
Expand All @@ -74,8 +80,12 @@ fn handle_extends<P: AsRef<Path>>(mut map: Mapping, path: P) -> anyhow::Result<M

fn extend_by<P: AsRef<Path>>(values: Mapping, root_dir: &Path, path: P) -> anyhow::Result<Mapping> {
let path = from_relative_path(path, root_dir)?;
let extended_values = handle_mapping_yaml(path)?;
Ok(merge_mapping(&values, &extended_values))
let extended_values = handle_yaml(&path)?;
match extended_values {
Value::Mapping(extended_values) => Ok(merge_mapping(&values, &extended_values)),
Value::Null => Ok(values),
_ => bail!("Expected a mapping in {}", path.display()),
}
}

fn merge_value(default: &Value, value: &Value) -> Value {
Expand Down

0 comments on commit 2b6921a

Please sign in to comment.