From 2f653caddbb9d948110e79988bfb8523fe7cfccc Mon Sep 17 00:00:00 2001 From: favonia Date: Tue, 24 Oct 2023 10:20:31 -0500 Subject: [PATCH] feat(monitor): prioritize error messages (#622) --- internal/updater/updater.go | 20 ++++++++++++++------ internal/updater/updater_test.go | 4 ++-- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/internal/updater/updater.go b/internal/updater/updater.go index 1b178166..5ede3948 100644 --- a/internal/updater/updater.go +++ b/internal/updater/updater.go @@ -110,13 +110,15 @@ func detectIP(ctx context.Context, ppfmt pp.PP, // UpdateIPs detect IP addresses and update DNS records of managed domains. func UpdateIPs(ctx context.Context, ppfmt pp.PP, c *config.Config, s setter.Setter) (bool, string) { allOk := true - var msgs []string + + // [msgs[false]] collects all the error messages and [msgs[true]] collects all the success messages. + msgs := map[bool][]string{} for _, ipNet := range [...]ipnet.Type{ipnet.IP4, ipnet.IP6} { if c.Provider[ipNet] != nil { ip, ok, msg := detectIP(ctx, ppfmt, c, ipNet, c.Use1001) - if msg != "" { - msgs = append(msgs, msg) + if len(msg) != 0 { + msgs[ok] = append(msgs[ok], msg) } if !ok { // We can't detect the new IP address. It's probably better to leave existing IP addresses alone. @@ -125,14 +127,20 @@ func UpdateIPs(ctx context.Context, ppfmt pp.PP, c *config.Config, s setter.Sett } ok, msg = setIP(ctx, ppfmt, c, s, ipNet, ip) - if msg != "" { - msgs = append(msgs, msg) + if len(msg) != 0 { + msgs[ok] = append(msgs[ok], msg) } allOk = allOk && ok } } - return allOk, strings.Join(msgs, "\n") + var allMsg string + if len(msgs[false]) != 0 { + allMsg = strings.Join(msgs[false], "\n") + } else { + allMsg = strings.Join(msgs[true], "\n") + } + return allOk, allMsg } // ClearIPs removes all DNS records of managed domains. diff --git a/internal/updater/updater_test.go b/internal/updater/updater_test.go index 4d3919b6..9b40691e 100644 --- a/internal/updater/updater_test.go +++ b/internal/updater/updater_test.go @@ -160,7 +160,7 @@ func TestUpdateIPs(t *testing.T) { api.TTLAuto, proxiedBoth, false, - "Failed to detect the IPv4 address\nlooking good", + "Failed to detect the IPv4 address", map[ipnet.Type]bool{ipnet.IP4: true, ipnet.IP6: true}, func(m *mocks.MockPP) { gomock.InOrder( @@ -183,7 +183,7 @@ func TestUpdateIPs(t *testing.T) { api.TTLAuto, proxiedNone, false, - "good\nFailed to detect the IPv6 address", + "Failed to detect the IPv6 address", map[ipnet.Type]bool{ipnet.IP4: true, ipnet.IP6: true}, func(m *mocks.MockPP) { gomock.InOrder(