From 4f33d88c5fb7532b8347dee63798829fbc41f314 Mon Sep 17 00:00:00 2001 From: Sam Coe Date: Thu, 2 Nov 2023 14:31:59 +0100 Subject: [PATCH] Set user level config values automatically when setting host level config values --- internal/config/config.go | 5 +++++ internal/config/config_test.go | 41 ++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+) diff --git a/internal/config/config.go b/internal/config/config.go index 3f9e9d4f8..ed82df99f 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -107,7 +107,12 @@ func (c *cfg) Set(hostname, key, value string) { c.cfg.Set([]string{key}, value) return } + c.cfg.Set([]string{hostsKey, hostname, key}, value) + + if user, _ := c.cfg.Get([]string{hostsKey, hostname, userKey}); user != "" { + c.cfg.Set([]string{hostsKey, hostname, usersKey, user, key}, value) + } } func (c *cfg) Write() error { diff --git a/internal/config/config_test.go b/internal/config/config_test.go index 3fd082874..f6832f4a4 100644 --- a/internal/config/config_test.go +++ b/internal/config/config_test.go @@ -163,3 +163,44 @@ func TestFallbackConfig(t *testing.T) { requireKeyWithValue(t, cfg, []string{browserKey}, "") requireNoKey(t, cfg, []string{"unknown"}) } + +func TestSetTopLevelKey(t *testing.T) { + c := newTestConfig() + host := "" + key := "top-level-key" + val := "top-level-value" + c.Set(host, key, val) + requireKeyWithValue(t, c.cfg, []string{key}, val) +} + +func TestSetHostSpecificKey(t *testing.T) { + c := newTestConfig() + host := "github.com" + key := "host-level-key" + val := "host-level-value" + c.Set(host, key, val) + requireKeyWithValue(t, c.cfg, []string{hostsKey, host, key}, val) +} + +func TestSetUserSpecificKey(t *testing.T) { + c := newTestConfig() + host := "github.com" + user := "test-user" + c.cfg.Set([]string{hostsKey, host, userKey}, user) + + key := "host-level-key" + val := "host-level-value" + c.Set(host, key, val) + requireKeyWithValue(t, c.cfg, []string{hostsKey, host, key}, val) + requireKeyWithValue(t, c.cfg, []string{hostsKey, host, usersKey, user, key}, val) +} + +func TestSetUserSpecificKeyNoUserPresent(t *testing.T) { + c := newTestConfig() + host := "github.com" + key := "host-level-key" + val := "host-level-value" + c.Set(host, key, val) + requireKeyWithValue(t, c.cfg, []string{hostsKey, host, key}, val) + requireNoKey(t, c.cfg, []string{hostsKey, host, usersKey}) +}