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

added --patch-string updated readme accordingly #42

Merged
merged 2 commits into from
Mar 27, 2023
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
52 changes: 28 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,10 @@ processed in is as follows:
1. _`--stdin`_: One or more `---` separated yaml files read from `stdin`.
1. _`--var`_: One or more path overrides of the form `/foo="bar"`. Key is a
path, an value is json/yaml encoded.
1. _`--patch`_: One or more rfc 6902 json/yaml patches to apply to the result
of merging all the config sources.
1. _`--patch`_: One or more rfc 6902 json/yaml patch files to apply to the
result of merging all the config sources.
1. _`--patch-string`_: One or more rfc 6902 json/yaml patches to apply to the
result of merging all the config sources.

All of these categories of input will be appended to each other and the _last
defined value of any key will take precedence_. For example:
Expand All @@ -59,7 +61,8 @@ clconf \
--yaml-base64 "$G_YML_B64" \
--yaml-base64 "$H_YML_B64" \
--var '/foo="bar"' \
--patch '[{"op": "replace", "path": "/foo", "value": "baz"}]' \
--patch patch.json \
--patch-string '[{"op": "replace", "path": "/foo", "value": "baz"}]' \
<<<"---\nfoo: baz"
```

Expand All @@ -75,6 +78,7 @@ Would be processed in the following order:
1. `H_YML_B64`
1. `stdin`
1. `/foo="bar"`
1. `patch.json`
1. `[{"op": "replace", "path": "/foo", "value": "baz"}]`

## Use Cases
Expand Down Expand Up @@ -241,12 +245,12 @@ db:

Then with:

```yaml
```bash
clconf \
--yaml /etc/myapp/config.yml \
--yaml /etc/myapp/secrets.yml \
getv \
> /app/config/application.yml
--yaml /etc/myapp/config.yml \
--yaml /etc/myapp/secrets.yml \
getv \
> /app/config/application.yml
```

You would have a file containing:
Expand Down Expand Up @@ -297,13 +301,13 @@ Then add your secrets:

```bash
clconf \
--secret-keyring testdata/test.secring.gpg \
--yaml C:/Temp/config.yml \
csetv /db/username dbuser
--secret-keyring testdata/test.secring.gpg \
--yaml C:/Temp/config.yml \
csetv /db/username dbuser
clconf \
--secret-keyring testdata/test.secring.gpg \
--yaml C:/Temp/config.yml \
csetv /db/password dbpass
--secret-keyring testdata/test.secring.gpg \
--yaml C:/Temp/config.yml \
csetv /db/password dbpass
```

Which would result in something safe to commit with your source code:
Expand All @@ -319,23 +323,23 @@ These values can be decrypted using:

```bash
clconf \
--secret-keyring testdata/test.secring.gpg \
--yaml C:/Temp/config.yml \
cgetv /db/username
--secret-keyring testdata/test.secring.gpg \
--yaml C:/Temp/config.yml \
cgetv /db/username
clconf \
--secret-keyring testdata/test.secring.gpg \
--yaml C:/Temp/config.yml \
cgetv /db/password
--secret-keyring testdata/test.secring.gpg \
--yaml C:/Temp/config.yml \
cgetv /db/password
```

Or in conjunction with templates

```bash
clconf \
--secret-keyring testdata/test.secring.gpg \
--yaml C:/Temp/config.yml \
getv / \
--template-string '{{ cgetv "/db/username" }}:{{ cgetv "/db/password" }}'
--secret-keyring testdata/test.secring.gpg \
--yaml C:/Temp/config.yml \
getv / \
--template-string '{{ cgetv "/db/username" }}:{{ cgetv "/db/password" }}'
```

### Templating
Expand Down
17 changes: 12 additions & 5 deletions pkg/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type rootContext struct {
yaml []string
yamlBase64 []string
patch []string
patchStrings []string
}

func (c *rootContext) getPath(valuePath string) string {
Expand All @@ -42,10 +43,11 @@ func (c *rootContext) getValue(path string) (interface{}, error) {
path = c.getPath(path)

confSources := conf.ConfSources{
Files: c.yaml,
Patches: c.patch,
Overrides: c.yamlBase64,
Environment: !c.ignoreEnv,
Files: c.yaml,
Patches: c.patch,
PatchStrings: c.patchStrings,
Overrides: c.yamlBase64,
Environment: !c.ignoreEnv,
}
if c.stdin {
confSources.Stream = os.Stdin
Expand Down Expand Up @@ -134,7 +136,12 @@ the order of precedence from least to greatest is:
&c.patch,
"patch",
nil,
"json patches to apply after the merge")
"files containing json patches to apply after the merge")
cmd.PersistentFlags().StringArrayVar(
&c.patchStrings,
"patch-string",
nil,
"strings containing json patches to apply after the merge")
cmd.PersistentFlags().BoolVarP(
&pipe,
"pipe",
Expand Down
12 changes: 12 additions & 0 deletions pkg/cmd/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,18 @@ func Example_patch() {
// foo: baz
}

func Example_patch_string() {
_ = newCmdWithYaml(
`foo: bar`,
"--patch-string",
`[{"op":"replace","path":"/foo","value":"baz"}]`,
"getv",
"/",
).Execute()
// Output:
// foo: baz
}

func Example_preserveListOrderInRange() {
yaml := `
a_list:
Expand Down
13 changes: 13 additions & 0 deletions pkg/conf/conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ type ConfSources struct { //nolint:revive
// Patches are files containing JSON 6902 patches to apply after the merge
// is complete
Patches []string
// PatchStrings are strings containing JSON 6902 patches to apply after the
// merge is complete
PatchStrings []string
// An optional (can be nil) stream to read raw yaml (potentially multiple
// inline documents)
Stream io.Reader
Expand Down Expand Up @@ -121,6 +124,16 @@ func (s ConfSources) loadInterface(settable bool) (interface{}, string, error) {
}
}

if len(s.PatchStrings) > 0 {
if settable {
return nil, "", errors.New("patch string not allowed when settable")
}
merged, err = yamljson.PatchFromStrings(merged, s.PatchStrings...)
if err != nil {
return nil, "", fmt.Errorf("patch string: %w", err)
}
}

if settable {
return merged, files[0], nil
}
Expand Down