Skip to content

Commit

Permalink
Add support for passwordstore
Browse files Browse the repository at this point in the history
For now, this only takes the first line of output from `pass`, which
should be the same behavior as `pass -c`.
  • Loading branch information
johnswanson committed Jan 17, 2019
1 parent 3f93dda commit 290d13e
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
1 change: 1 addition & 0 deletions cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ type Config struct {
Bitwarden bitwardenCommandConfig
LastPass LastPassCommandConfig
Vault vaultCommandConfig
Pass PassCommandConfig
Data map[string]interface{}
funcs template.FuncMap
add addCommandConfig
Expand Down
52 changes: 52 additions & 0 deletions cmd/pass.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package cmd

import (
"fmt"
"os/exec"
"strings"

"github.com/spf13/cobra"
"github.com/twpayne/chezmoi/lib/chezmoi"
vfs "github.com/twpayne/go-vfs"
)

var passCommand = &cobra.Command{
Use: "pass",
Short: "Execute the pass CLI",
RunE: makeRunE(config.runPassCommand),
}

// A PassCommandConfig is a configuration for the pass command.
type PassCommandConfig struct {
Pass string
}

var passCache = make(map[string]string)

func init() {
rootCommand.AddCommand(passCommand)
config.Pass.Pass = "pass"
config.addFunc("pass", config.passFunc)
}

func (c *Config) runPassCommand(fs vfs.FS, args []string) error {
return c.exec(append([]string{c.Pass.Pass}, args...))
}

func (c *Config) passFunc(id string) string {
if s, ok := passCache[id]; ok {
return s
}
name := c.Pass.Pass
args := []string{id}
if c.Verbose {
fmt.Printf("%s %s\n", name, strings.Join(args, " "))
}
output, err := exec.Command(name, args...).Output()
if err != nil {
chezmoi.ReturnTemplateFuncError(fmt.Errorf("pass: %s %s: %v", name, strings.Join(args, " "), err))
}
password := strings.Split(string(output), "\n")[0]
passCache[id] = password
return passCache[id]
}

0 comments on commit 290d13e

Please sign in to comment.