@@ -10,6 +10,7 @@ import (
1010 "regexp"
1111 "strings"
1212
13+ "github.com/coreos/go-semver/semver"
1314 "github.com/spf13/cobra"
1415 "github.com/twpayne/chezmoi/internal/chezmoi"
1516 "golang.org/x/crypto/ssh/terminal"
@@ -34,10 +35,12 @@ type keePassXCAttributeCacheKey struct {
3435}
3536
3637var (
37- keePassXCCache = make (map [string ]map [string ]string )
38- keePassXCAttributeCache = make (map [keePassXCAttributeCacheKey ]string )
39- keePassXCPairRegexp = regexp .MustCompile (`^([^:]+): (.*)$` )
40- keePassXCPassword string
38+ keePassXCVersion * semver.Version
39+ keePassXCCache = make (map [string ]map [string ]string )
40+ keePassXCAttributeCache = make (map [keePassXCAttributeCacheKey ]string )
41+ keePassXCPairRegexp = regexp .MustCompile (`^([^:]+): (.*)$` )
42+ keePassXCPassword string
43+ keePassXCNeedShowProtectedArgVersion = semver.Version {Major : 2 , Minor : 5 , Patch : 1 }
4144)
4245
4346func init () {
@@ -52,6 +55,24 @@ func (c *Config) runKeePassXCCmd(cmd *cobra.Command, args []string) error {
5255 return c .run ("" , c .KeePassXC .Command , args ... )
5356}
5457
58+ func (c * Config ) getKeePassXCVersion () * semver.Version {
59+ if keePassXCVersion != nil {
60+ return keePassXCVersion
61+ }
62+ name := c .KeePassXC .Command
63+ args := []string {"--version" }
64+ cmd := exec .Command (name , args ... )
65+ output , err := c .mutator .IdempotentCmdOutput (cmd )
66+ if err != nil {
67+ panic (fmt .Errorf ("keepassxc: %s %s: %w" , name , chezmoi .ShellQuoteArgs (args ), err ))
68+ }
69+ keePassXCVersion , err = semver .NewVersion (string (bytes .TrimSpace (output )))
70+ if err != nil {
71+ panic (fmt .Errorf ("keepassxc: cannot parse version %q: %w" , output , err ))
72+ }
73+ return keePassXCVersion
74+ }
75+
5576func (c * Config ) keePassXCFunc (entry string ) map [string ]string {
5677 if data , ok := keePassXCCache [entry ]; ok {
5778 return data
@@ -61,6 +82,9 @@ func (c *Config) keePassXCFunc(entry string) map[string]string {
6182 }
6283 name := c .KeePassXC .Command
6384 args := []string {"show" }
85+ if c .getKeePassXCVersion ().Compare (keePassXCNeedShowProtectedArgVersion ) >= 0 {
86+ args = append (args , "--show-protected" )
87+ }
6488 args = append (args , c .KeePassXC .Args ... )
6589 args = append (args , c .KeePassXC .Database , entry )
6690 output , err := c .runKeePassXCCLICommand (name , args )
@@ -88,6 +112,9 @@ func (c *Config) keePassXCAttributeFunc(entry, attribute string) string {
88112 }
89113 name := c .KeePassXC .Command
90114 args := []string {"show" , "--attributes" , attribute , "--quiet" }
115+ if c .getKeePassXCVersion ().Compare (keePassXCNeedShowProtectedArgVersion ) >= 0 {
116+ args = append (args , "--show-protected" )
117+ }
91118 args = append (args , c .KeePassXC .Args ... )
92119 args = append (args , c .KeePassXC .Database , entry )
93120 output , err := c .runKeePassXCCLICommand (name , args )
0 commit comments