Skip to content

Commit

Permalink
Add gopassRaw template function
Browse files Browse the repository at this point in the history
  • Loading branch information
twpayne committed Jun 28, 2021
1 parent 745e198 commit 0f8a0aa
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 6 deletions.
18 changes: 14 additions & 4 deletions docs/REFERENCE.md
Expand Up @@ -92,6 +92,7 @@ Manage your dotfiles securely across multiple machines.
* [`bitwardenFields` [*arg*...]](#bitwardenfields-arg)
* [`gitHubKeys` *user*](#githubkeys-user)
* [`gopass` *gopass-name*](#gopass-gopass-name)
* [`gopassRaw` *gopass-name*](#gopassraw-gopass-name)
* [`include` *filename*](#include-filename)
* [`ioreg`](#ioreg)
* [`joinPath` *element*...](#joinpath-element)
Expand Down Expand Up @@ -1568,17 +1569,26 @@ token](https://docs.github.com/en/github/authenticating-to-github/creating-a-per
### `gopass` *gopass-name*

`gopass` returns passwords stored in [gopass](https://www.gopass.pw/) using the
gopass CLI (`gopass`). *gopass-name* is passed to `gopass show <gopass-name>`
and first line of the output of `gopass` is returned with the trailing newline
stripped. The output from `gopass` is cached so calling `gopass` multiple times
with the same *gopass-name* will only invoke `gopass` once.
gopass CLI (`gopass`). *gopass-name* is passed to `gopass show --password
<gopass-name>` and the first line of the output of `gopass` is returned with the
trailing newline stripped. The output from `gopass` is cached so calling
`gopass` multiple times with the same *gopass-name* will only invoke `gopass`
once.

#### `gopass` examples

```
{{ gopass "<pass-name>" }}
```

### `gopassRaw` *gopass-name*

`gopass` returns passwords stored in [gopass](https://www.gopass.pw/) using the
gopass CLI (`gopass`). *gopass-name* is passed to `gopass show <gopass-name>`
and output of `gopass` is returned. The output from `gopassRaw` is cached so
calling `gopassRaw` multiple times with the same *gopass-name* will only invoke
`gopass` once.

### `include` *filename*

`include` returns the literal contents of the file named `*filename*`. Relative
Expand Down
1 change: 1 addition & 0 deletions internal/cmd/config.go
Expand Up @@ -335,6 +335,7 @@ func newConfig(options ...configOption) (*Config, error) {
"bitwardenFields": c.bitwardenFieldsTemplateFunc,
"gitHubKeys": c.gitHubKeysTemplateFunc,
"gopass": c.gopassTemplateFunc,
"gopassRaw": c.gopassRawTemplateFunc,
"include": c.includeTemplateFunc,
"ioreg": c.ioregTemplateFunc,
"joinPath": c.joinPathTemplateFunc,
Expand Down
34 changes: 32 additions & 2 deletions internal/cmd/gopasstemplatefuncs.go
Expand Up @@ -24,6 +24,7 @@ type gopassConfig struct {
Command string
versionOK bool
cache map[string]string
rawCache map[string][]byte
}

func (c *Config) gopassOutput(args ...string) ([]byte, error) {
Expand All @@ -38,6 +39,34 @@ func (c *Config) gopassOutput(args ...string) ([]byte, error) {
return output, nil
}

func (c *Config) gopassRawTemplateFunc(id string) string {
if !c.Gopass.versionOK {
if err := c.gopassVersionCheck(); err != nil {
returnTemplateError(err)
return ""
}
c.Gopass.versionOK = true
}

if output, ok := c.Gopass.rawCache[id]; ok {
return string(output)
}

args := []string{"show", id}
output, err := c.gopassOutput(args...)
if err != nil {
returnTemplateError(fmt.Errorf("%s %s: %w", c.Gopass.Command, chezmoi.ShellQuoteArgs(args), err))
return ""
}

if c.Gopass.rawCache == nil {
c.Gopass.rawCache = make(map[string][]byte)
}
c.Gopass.rawCache[id] = output

return string(output)
}

func (c *Config) gopassTemplateFunc(id string) string {
if !c.Gopass.versionOK {
if err := c.gopassVersionCheck(); err != nil {
Expand All @@ -47,8 +76,8 @@ func (c *Config) gopassTemplateFunc(id string) string {
c.Gopass.versionOK = true
}

if s, ok := c.Gopass.cache[id]; ok {
return s
if password, ok := c.Gopass.cache[id]; ok {
return password
}

args := []string{"show", "--password", id}
Expand All @@ -69,6 +98,7 @@ func (c *Config) gopassTemplateFunc(id string) string {
c.Gopass.cache = make(map[string]string)
}
c.Gopass.cache[id] = password

return password
}

Expand Down
15 changes: 15 additions & 0 deletions internal/cmd/testdata/scripts/gopass.txt
Expand Up @@ -5,13 +5,23 @@
chezmoi execute-template '{{ gopass "misc/example.com" }}'
stdout examplepassword

# test gopass template function
chezmoi execute-template '{{ gopassRaw "misc/example.com" }}'
stdout 'Secret: misc/example\.com'

-- bin/gopass --
#!/bin/sh

case "$*" in
"--version")
echo "gopass 1.10.1 go1.15 linux amd64"
;;
"show misc/example.com")
echo "Secret: misc/example.com"
echo
echo "examplepassword"
echo "key: value"
;;
"show --password misc/example.com")
echo "examplepassword"
;;
Expand All @@ -23,6 +33,11 @@ esac
@echo off
IF "%*" == "--version" (
echo "gopass 1.10.1 go1.15 windows amd64"
) ELSE IF "%*" == "show misc/example.com" (
echo "Secret: misc/example.com"
echo
echo "examplepassword"
echo "key: value"
) ELSE IF "%*" == "show --password misc/example.com" (
echo | set /p=examplepassword
exit /b 0
Expand Down

0 comments on commit 0f8a0aa

Please sign in to comment.