Skip to content

Commit

Permalink
Add GetAll getters
Browse files Browse the repository at this point in the history
Signed-off-by: Dominik Schulz <dominik.schulz@gauner.org>
  • Loading branch information
dominikschulz committed Dec 25, 2022
1 parent e3c5e91 commit 122b671
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 14 deletions.
5 changes: 5 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,11 @@ func (c *Config) Get(key string) string {
return c.root.Get(key)
}

// GetAll returns all values for the given key.
func (c *Config) GetAll(key string) []string {
return c.root.GetAll(key)
}

// GetM returns the given key from the mount or the root config if mount is empty.
func (c *Config) GetM(mount, key string) string {
if mount == "" {
Expand Down
24 changes: 24 additions & 0 deletions pkg/gitconfig/configs.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,30 @@ func (c *Configs) Get(key string) string {
return ""
}

// GetAll returns all values for the given key from the first location that is found.
// See the description of Get for more details.
func (c *Configs) GetAll(key string) []string {
for _, cfg := range []*Config{
c.env,
c.worktree,
c.local,
c.global,
c.system,
c.Preset,
} {
if cfg == nil || cfg.vars == nil {
continue
}
if vs, found := cfg.GetAll(key); found {
return vs
}
}

debug.Log("no value for %s found", key)

return nil
}

// GetGlobal specifically ask the per-user (global) config for a key.
func (c *Configs) GetGlobal(key string) string {
if c.global == nil {
Expand Down
4 changes: 1 addition & 3 deletions pkg/gitconfig/gitconfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,9 +298,7 @@ func TestGopass(t *testing.T) {
assert.Equal(t, "false", c.Get("core.pager"))
assert.Equal(t, "true", c.Get("core.notifications"))
assert.Equal(t, "false", c.Get("core.showsafecontent"))
assert.Equal(t, "foo.de", c.Get("domain-alias.foo.com.insteadOf"))
// TODO: support multivars
// foo.de and foo.it should be part of a multi-var get
assert.Equal(t, []string{"foo.de", "foo.it"}, c.GetAll("domain-alias.foo.com.insteadOf"))

assert.Equal(t, "/home/johndoe/.password-store", c.Get("mounts.path"))
assert.Equal(t, "/home/johndoe/.password-store-foo-sub", c.Get("mounts.foo/sub.path"))
Expand Down
24 changes: 13 additions & 11 deletions pkg/pwgen/pwrules/aliases.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,22 +37,24 @@ func AllAliases(ctx context.Context) map[string][]string {
}

func loadCustomAliases(ctx context.Context) map[string][]string {
cfg := config.FromContext(ctx)
customAliases := make(map[string][]string, 128)
for _, k := range set.SortedFiltered(config.FromContext(ctx).Keys(""), func(k string) bool {
return strings.HasPrefix(k, "domain-alias.") && strings.HasSuffix(k, ".insteadOf")
}) {
from := config.String(ctx, k)
to := strings.TrimSuffix(strings.TrimPrefix(k, "domain-alias."), ".insteadOf")
debug.Log("Loading alias: %q -> %q", from, to)
if e, found := customAliases[from]; found {
e = append(e, to)
sort.Strings(e)
customAliases[from] = e

continue
for _, from := range cfg.GetAll(k) {
to := strings.TrimSuffix(strings.TrimPrefix(k, "domain-alias."), ".insteadOf")
debug.Log("Loading alias: %q -> %q", from, to)
if e, found := customAliases[from]; found {
e = append(e, to)
sort.Strings(e)
customAliases[from] = e

continue
}

customAliases[from] = []string{to}
}

customAliases[from] = []string{to}
}

return customAliases
Expand Down

0 comments on commit 122b671

Please sign in to comment.