Skip to content
This repository has been archived by the owner on Dec 5, 2024. It is now read-only.

Commit

Permalink
[bugfix] Fix loading of git configs (gopasspw#2849)
Browse files Browse the repository at this point in the history
* [bugfix] Fix loading of git configs

The gitconfig package was incorrectly using gopass specific
locations when trying to load global (per user) git configs.

This change makes it use the correct locations.

Fixes gopasspw#2686

Signed-off-by: Dominik Schulz <[email protected]>

* Fix typo

Signed-off-by: Dominik Schulz <[email protected]>

* Fix linter issues

Signed-off-by: Dominik Schulz <[email protected]>

* Fix failing tests

Those started to fail because we are now correctly reading
global git configs.

Signed-off-by: Dominik Schulz <[email protected]>

---------

Signed-off-by: Dominik Schulz <[email protected]>
  • Loading branch information
dominikschulz authored Mar 29, 2024
1 parent 2adc544 commit 78e7d8e
Show file tree
Hide file tree
Showing 11 changed files with 142 additions and 85 deletions.
5 changes: 2 additions & 3 deletions internal/action/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,11 +179,10 @@ func (s *Action) init(ctx context.Context, alias, path string, keys ...string) e
func (s *Action) printRecipients(ctx context.Context, alias string) {
crypto := s.Store.Crypto(ctx, alias)
for _, recipient := range s.Store.ListRecipients(ctx, alias) {
r := "0x" + recipient
if kl, err := crypto.FindRecipients(ctx, recipient); err == nil && len(kl) > 0 {
r = crypto.FormatKey(ctx, kl[0], "")
recipient = crypto.FormatKey(ctx, kl[0], "")
}
out.Printf(ctx, "📩 "+r)
out.Printf(ctx, "📩 "+recipient)
}
}

Expand Down
1 change: 1 addition & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ const (

func newGitconfig() *gitconfig.Configs {
c := gitconfig.New()
c.Name = "gopass"
c.EnvPrefix = envPrefix
c.GlobalConfig = os.Getenv("GOPASS_CONFIG")
c.SystemConfig = systemConfig
Expand Down
36 changes: 34 additions & 2 deletions pkg/appdir/appdir.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,40 @@ import (
"github.com/gopasspw/gopass/pkg/debug"
)

// Name is used in the final path of the generated path.
var Name = "gopass"
var DefaultAppdir = New("gopass")

// Appdir is a helper struct to generate paths for config, cache and data dirs.
type Appdir struct {
// Name is used in the final path of the generated path.
name string
}

// New returns a new Appdir.
func New(name string) *Appdir {
return &Appdir{
name: name,
}
}

// Name returns the name of the appdir.
func (a *Appdir) Name() string {
return a.name
}

// UserConfig returns the users config dir.
func UserConfig() string {
return DefaultAppdir.UserConfig()
}

// UserCache returns the users cache dir.
func UserCache() string {
return DefaultAppdir.UserCache()
}

// UserData returns the users data dir.
func UserData() string {
return DefaultAppdir.UserData()
}

// UserHome returns the users home dir.
func UserHome() string {
Expand Down
18 changes: 9 additions & 9 deletions pkg/appdir/appdir_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,27 @@ import (
)

// UserConfig returns the users config dir
func UserConfig() string {
func (a *Appdir) UserConfig() string {
if hd := os.Getenv("GOPASS_HOMEDIR"); hd != "" {
return filepath.Join(hd, ".config", Name)
return filepath.Join(hd, ".config", a.name)
}

return filepath.Join(os.Getenv("APPDATA"), Name)
return filepath.Join(os.Getenv("APPDATA"), a.name)
}

// UserCache returns the users cache dir
func UserCache() string {
func (a *Appdir) UserCache() string {
if hd := os.Getenv("GOPASS_HOMEDIR"); hd != "" {
return filepath.Join(hd, ".cache", Name)
return filepath.Join(hd, ".cache", a.name)
}

return filepath.Join(os.Getenv("LOCALAPPDATA"), Name)
return filepath.Join(os.Getenv("LOCALAPPDATA"), a.name)
}

// UserData returns the users data dir
func UserData() string {
func (a *Appdir) UserData() string {
if hd := os.Getenv("GOPASS_HOMEDIR"); hd != "" {
return filepath.Join(hd, ".local", "share", Name)
return filepath.Join(hd, ".local", "share", a.name)
}
return filepath.Join(os.Getenv("LOCALAPPDATA"), Name)
return filepath.Join(os.Getenv("LOCALAPPDATA"), a.name)
}
18 changes: 9 additions & 9 deletions pkg/appdir/appdir_xdg.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,43 +9,43 @@ import (
)

// UserConfig returns the users config dir.
func UserConfig() string {
func (a *Appdir) UserConfig() string {
if hd := os.Getenv("GOPASS_HOMEDIR"); hd != "" {
return filepath.Join(hd, ".config", Name)
return filepath.Join(hd, ".config", a.name)
}

base := os.Getenv("XDG_CONFIG_HOME")
if base == "" {
base = filepath.Join(os.Getenv("HOME"), ".config")
}

return filepath.Join(base, Name)
return filepath.Join(base, a.name)
}

// UserCache returns the users cache dir.
func UserCache() string {
func (a *Appdir) UserCache() string {
if hd := os.Getenv("GOPASS_HOMEDIR"); hd != "" {
return filepath.Join(hd, ".cache", Name)
return filepath.Join(hd, ".cache", a.name)
}

base := os.Getenv("XDG_CACHE_HOME")
if base == "" {
base = filepath.Join(os.Getenv("HOME"), ".cache")
}

return filepath.Join(base, Name)
return filepath.Join(base, a.name)
}

// UserData returns the users data dir.
func UserData() string {
func (a *Appdir) UserData() string {
if hd := os.Getenv("GOPASS_HOMEDIR"); hd != "" {
return filepath.Join(hd, ".local", "share", Name)
return filepath.Join(hd, ".local", "share", a.name)
}

base := os.Getenv("XDG_DATA_HOME")
if base == "" {
base = filepath.Join(os.Getenv("HOME"), ".local", "share")
}

return filepath.Join(base, Name)
return filepath.Join(base, a.name)
}
18 changes: 10 additions & 8 deletions pkg/gitconfig/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,16 +122,16 @@ func (c *Config) Set(key, value string) error {
vs[0] = value
c.vars[key] = vs

debug.Log("set %q to %q", key, value)
// debug.Log("set %q to %q", key, value)

// a new key, insert it into an existing section, if any
if !present {
debug.Log("inserting value")
// debug.Log("inserting value")

return c.insertValue(key, value)
}

debug.Log("updating value")
// debug.Log("updating value")

var updated bool

Expand All @@ -146,7 +146,7 @@ func (c *Config) Set(key, value string) error {
}

func (c *Config) insertValue(key, value string) error {
debug.Log("input (%s: %s): \n--------------\n%s\n--------------\n", key, value, strings.Join(strings.Split("- "+c.raw.String(), "\n"), "\n- "))
// debug.Log("input (%s: %s): \n--------------\n%s\n--------------\n", key, value, strings.Join(strings.Split("- "+c.raw.String(), "\n"), "\n- "))

wSection, wSubsection, wKey := splitKey(key)

Expand Down Expand Up @@ -204,7 +204,7 @@ func (c *Config) insertValue(key, value string) error {
c.raw.WriteString(strings.Join(lines, "\n"))
c.raw.WriteString("\n")

debug.Log("output: \n--------------\n%s\n--------------\n", strings.Join(strings.Split("+ "+c.raw.String(), "\n"), "\n+ "))
// debug.Log("output: \n--------------\n%s\n--------------\n", strings.Join(strings.Split("+ "+c.raw.String(), "\n"), "\n+ "))

return c.flushRaw()
}
Expand All @@ -231,15 +231,15 @@ func parseSectionHeader(line string) (section, subsection string, skip bool) { /
// rewriteRaw is used to rewrite the raw config copy. It is used for set and unset operations
// with different callbacks each.
func (c *Config) rewriteRaw(key, value string, cb parseFunc) error {
debug.Log("input (%s: %s): \n--------------\n%s\n--------------\n", key, value, strings.Join(strings.Split("- "+c.raw.String(), "\n"), "\n- "))
// debug.Log("input (%s: %s): \n--------------\n%s\n--------------\n", key, value, strings.Join(strings.Split("- "+c.raw.String(), "\n"), "\n- "))

lines := parseConfig(strings.NewReader(c.raw.String()), key, value, cb)

c.raw = strings.Builder{}
c.raw.WriteString(strings.Join(lines, "\n"))
c.raw.WriteString("\n")

debug.Log("output: \n--------------\n%s\n--------------\n", strings.Join(strings.Split("+ "+c.raw.String(), "\n"), "\n+ "))
// debug.Log("output: \n--------------\n%s\n--------------\n", strings.Join(strings.Split("+ "+c.raw.String(), "\n"), "\n+ "))

return c.flushRaw()
}
Expand All @@ -255,12 +255,14 @@ func (c *Config) flushRaw() error {
return err
}

debug.Log("writing config to %s: \n--------------\n%s\n--------------", c.path, c.raw.String())
// debug.Log("writing config to %s: \n--------------\n%s\n--------------", c.path, c.raw.String())

if err := os.WriteFile(c.path, []byte(c.raw.String()), 0o600); err != nil {
return fmt.Errorf("failed to write config to %s: %w", c.path, err)
}

debug.Log("wrote config to %s", c.path)

return nil
}

Expand Down
Loading

0 comments on commit 78e7d8e

Please sign in to comment.