Skip to content

Commit

Permalink
Merge pull request #936 from zb140/1password-cache-arg
Browse files Browse the repository at this point in the history
Opt in to data caching provided by newer versions of 1password-cli
  • Loading branch information
twpayne authored Nov 20, 2020
2 parents 41daae4 + 6a13e71 commit 4fd06fa
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 3 deletions.
3 changes: 2 additions & 1 deletion cmd/docs.gen.go

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

32 changes: 31 additions & 1 deletion cmd/secretonepassword.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package cmd

import (
"bytes"
"encoding/json"
"fmt"
"os"
"os/exec"
"strings"

"github.com/coreos/go-semver/semver"
"github.com/spf13/cobra"

"github.com/twpayne/chezmoi/internal/chezmoi"
Expand All @@ -21,24 +23,52 @@ var onepasswordCmd = &cobra.Command{

type onepasswordCmdConfig struct {
Command string
Cache bool
}

var onepasswordOutputCache = make(map[string][]byte)
var (
onepasswordVersion *semver.Version
onepasswordOutputCache = make(map[string][]byte)
onepasswordCacheArgVersion = semver.Version{Major: 1, Minor: 8, Patch: 0}
)

func init() {
config.Onepassword.Command = "op"
config.Onepassword.Cache = true
config.addTemplateFunc("onepassword", config.onepasswordFunc)
config.addTemplateFunc("onepasswordDocument", config.onepasswordDocumentFunc)
config.addTemplateFunc("onepasswordDetailsFields", config.onepasswordDetailsFieldsFunc)

secretCmd.AddCommand(onepasswordCmd)
}

func (c *Config) getOnepasswordVersion() *semver.Version {
if onepasswordVersion != nil {
return onepasswordVersion
}
name := c.Onepassword.Command
args := []string{"--version"}
cmd := exec.Command(name, args...)
output, err := c.mutator.IdempotentCmdOutput(cmd)
if err != nil {
panic(fmt.Errorf("%s %s: %w", name, chezmoi.ShellQuoteArgs(args), err))
}
onepasswordVersion, err = semver.NewVersion(string(bytes.TrimSpace(output)))
if err != nil {
panic(fmt.Errorf("cannot parse version %q: %w", output, err))
}
return onepasswordVersion
}

func (c *Config) runOnepasswordCmd(cmd *cobra.Command, args []string) error {
return c.run("", c.Onepassword.Command, args...)
}

func (c *Config) onepasswordOutput(args []string) []byte {
if c.Onepassword.Cache && c.getOnepasswordVersion().Compare(onepasswordCacheArgVersion) >= 0 {
args = append(args, "--cache")
}

key := strings.Join(args, "\x00")
if output, ok := onepasswordOutputCache[key]; ok {
return output
Expand Down
3 changes: 2 additions & 1 deletion docs/REFERENCE.md
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,8 @@ The following configuration variables are available:
| `lastpass` | `command` | string | `lpass` | Lastpass CLI command |
| `merge` | `args` | []string | *none* | Extra args to 3-way merge command |
| | `command` | string | `vimdiff` | 3-way merge command |
| `onepassword` | `command` | string | `op` | 1Password CLI command |
| `onepassword` | `cache` | bool | `true` | Enable optional caching provided by `op` |
| | `command` | string | `op` | 1Password CLI command |
| `pass` | `command` | string | `pass` | Pass CLI command |
| `sourceVCS` | `autoCommit` | bool | `false` | Commit changes to the source state after any change |
| | `autoPush` | bool | `false` | Push changes to the source state after any change |
Expand Down

0 comments on commit 4fd06fa

Please sign in to comment.