Skip to content

Commit

Permalink
feat: Extend rbw and rbwFields template funcs to take extra args
Browse files Browse the repository at this point in the history
  • Loading branch information
twpayne committed Dec 21, 2023
1 parent 54dce1b commit d99e8bd
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# `rbw` [*arg*...]
# `rbw` *name* [*arg*...]

`rbw` returns structured data retrieved from [Bitwarden](https://bitwarden.com)
using [`rbw`](https://github.com/doy/rbw). *arg*s are passed to `rbw get --raw`
and the output is parsed as JSON.
using [`rbw`](https://github.com/doy/rbw). *name* is passed to `rbw get --raw`,
along with any extra *arg*s, and the output is parsed as JSON.

The output from `rbw get --raw` is cached so calling `rbw` multiple times with
the same arguments will only invoke `rbw` once.
Expand All @@ -11,5 +11,5 @@ the same arguments will only invoke `rbw` once.

```
username = {{ (rbw "test-entry").data.username }}
password = {{ (rbw "test-entry").data.password }}
password = {{ (rbw "test-entry" "--folder" "my-folder").data.password }}
```
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# `rbwFields` *name*
# `rbwFields` *name* [*arg*...]

`rbw` returns structured data retrieved from [Bitwarden](https://bitwarden.com)
using [`rbw`](https://github.com/doy/rbw). *arg*s are passed to `rbw get --raw`
and the output is parsed as JSON, and the elements of `fields` are returned as a dict
indexed by each field's `name`.
using [`rbw`](https://github.com/doy/rbw). *name* is passed to `rbw get --raw`,
along with any extra *arg*s, the output is parsed as JSON, and the elements
of `fields` are returned as a dict indexed by each field's `name`.

The output from `rbw get --raw` is cached so calling `rbwFields` multiple times with
the same arguments will only invoke `rbwFields` once.
Expand All @@ -12,4 +12,5 @@ the same arguments will only invoke `rbwFields` once.

```
{{ (rbwFields "item").name.value }}
{{ (rbwFields "item" "--folder" "my-folder").name.value }}
```
8 changes: 4 additions & 4 deletions internal/cmd/rbwtemplatefuncs.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ type rbwConfig struct {

var rbwMinVersion = semver.Version{Major: 1, Minor: 7, Patch: 0}

func (c *Config) rbwFieldsTemplateFunc(name string) map[string]any {
args := []string{"get", "--raw", name}
func (c *Config) rbwFieldsTemplateFunc(name string, extraArgs ...string) map[string]any {
args := append([]string{"get", "--raw", name}, extraArgs...)
output, err := c.rbwOutput(args)
if err != nil {
panic(err)
Expand All @@ -39,8 +39,8 @@ func (c *Config) rbwFieldsTemplateFunc(name string) map[string]any {
return result
}

func (c *Config) rbwTemplateFunc(name string) map[string]any {
args := []string{"get", "--raw", name}
func (c *Config) rbwTemplateFunc(name string, extraArgs ...string) map[string]any {
args := append([]string{"get", "--raw", name}, extraArgs...)
output, err := c.rbwOutput(args)
if err != nil {
panic(err)
Expand Down
71 changes: 71 additions & 0 deletions internal/cmd/testdata/scripts/rbw.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,18 @@
exec chezmoi execute-template '{{ (rbw "test-entry").data.password }}'
stdout ^hunter2$

# test rbw template function with extra args
exec chezmoi execute-template '{{ (rbw "test-entry" "--folder" "my-folder").data.password }}'
stdout ^correcthorsebatterystaple$

# test rbwFields template function
exec chezmoi execute-template '{{ (rbwFields "test-entry").something.value }}'
stdout ^secret$

# test rbwFields template function with extra args
exec chezmoi execute-template '{{ (rbwFields "test-entry" "--folder" "my-folder").something.value }}'
stdout ^enigma$

-- bin/rbw --
#!/bin/sh

Expand Down Expand Up @@ -44,6 +52,39 @@ case "$*" in
}
]
}
EOF
;;
"get --raw test-entry --folder my-folder")
cat <<EOF
{
"id": "adf723e1-ab03-4ff3-81aa-f5f3c2b68a5f",
"folder": null,
"name": "test-entry",
"data": {
"username": "foo",
"password": "correcthorsebatterystaple",
"totp": null,
"uris": [
{
"uri": "example.com",
"match_type": null
}
]
},
"fields": [
{
"name": "something",
"value": "enigma"
}
],
"notes": "blah",
"history": [
{
"last_used_date": "2022-08-18T23:24:47.994Z",
"password": "hunter2"
}
]
}
EOF
;;
*)
Expand Down Expand Up @@ -82,6 +123,36 @@ IF "%*" == "get --raw test-entry" (
echo. }
echo. ]
echo.}
) ELSE IF "%*" == "get --raw test-entry --folder my-folder" (
echo.{
echo. "id": "adf723e1-ab03-4ff3-81aa-f5f3c2b68a5f",
echo. "folder": null,
echo. "name": "test-entry",
echo. "data": {
echo. "username": "foo",
echo. "password": "correcthorsebatterystaple",
echo. "totp": null,
echo. "uris": [
echo. {
echo. "uri": "example.com",
echo. "match_type": null
echo. }
echo. ]
echo. },
echo. "fields": [
echo. {
echo. "name": "something",
echo. "value": "enigma"
echo. }
echo. ],
echo. "notes": "blah",
echo. "history": [
echo. {
echo. "last_used_date": "2022-08-18T23:24:47.994Z",
echo. "password": "hunter2"
echo. }
echo. ]
echo.}
) ELSE (
exit /b 1
)

0 comments on commit d99e8bd

Please sign in to comment.