Repository: favonia/cloudflare-ddns
Pull request #789 (2024-07-07 13:28:40)

refactor(updater): make hint names constants

codecov[bot]: ## [Codecov](https://app.codecov.io/gh/favonia/cloudflare-ddns/pull/789?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=favonia) Report
All modified and coverable lines are covered by tests :white_check_mark:
> Project coverage is 90.40%. Comparing base [(`0983b06`)](https://app.codecov.io/gh/favonia/cloudflare-ddns/commit/0983b06b4b308be5e0bfd16f2b101114d9008d56?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=favonia) to head [(`fa3b2af`)](https://app.codecov.io/gh/favonia/cloudflare-ddns/commit/fa3b2aface5de01c8e7aaa192bbcf0ae8f9cfe81?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=favonia).

<details><summary>Additional details and impacted files</summary>


```diff
@@           Coverage Diff           @@
##             main     #789   +/-   ##
=======================================
  Coverage   90.40%   90.40%           
=======================================
  Files          52       52           
  Lines        2199     2199           
=======================================
  Hits         1988     1988           
  Misses        200      200           
  Partials       11       11           
```

| [Flag](https://app.codecov.io/gh/favonia/cloudflare-ddns/pull/789/flags?src=pr&el=flags&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=favonia) | Coverage Δ | |
|---|---|---|
| [unittests](https://app.codecov.io/gh/favonia/cloudflare-ddns/pull/789/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=favonia) | `90.40% <100.00%> (ø)` | |

Flags with carried forward coverage won't be shown. [Click here](https://docs.codecov.io/docs/carryforward-flags?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=favonia#carryforward-flags-in-the-pull-request-comment) to find out more.

</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/favonia/cloudflare-ddns/pull/789?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=favonia).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=favonia).


Patch:
diff --git a/internal/updater/updater.go b/internal/updater/updater.go
index 2cd95a0..594226e 100644
--- a/internal/updater/updater.go
+++ b/internal/updater/updater.go
@@ -15,6 +15,13 @@ import (
 	"github.com/favonia/cloudflare-ddns/internal/setter"
 )
 
+const (
+	HintIP4DetectionFails string = "detect-ip4-fail"
+	HintIP6DetectionFails string = "detect-ip6-fail"
+	HintDetectionTimeouts string = "detect-timeout"
+	HintUpdateTimeouts    string = "update-timeout"
+)
+
 // ShouldDisplayHints determines whether help messages should be displayed.
 // The help messages are to help beginners detect possible misconfiguration.
 // These messages should be displayed at most once, and thus the value of this map
@@ -22,16 +29,16 @@ import (
 //
 //nolint:gochecknoglobals
 var ShouldDisplayHints = map[string]bool{
-	"detect-ip4-fail": true,
-	"detect-ip6-fail": true,
-	"detect-timeout":  true,
-	"update-timeout":  true,
+	HintIP4DetectionFails: true,
+	HintIP6DetectionFails: true,
+	HintDetectionTimeouts: true,
+	HintUpdateTimeouts:    true,
 }
 
 func getHintIDForDetection(ipNet ipnet.Type) string {
 	return map[ipnet.Type]string{
-		ipnet.IP4: "detect-ip4-fail",
-		ipnet.IP6: "detect-ip6-fail",
+		ipnet.IP4: HintIP4DetectionFails,
+		ipnet.IP6: HintIP6DetectionFails,
 	}[ipNet]
 }
 
@@ -63,12 +70,12 @@ func setIP(ctx context.Context, ppfmt pp.PP,
 		resp := s.Set(ctx, ppfmt, domain, ipNet, ip, c.TTL, getProxied(ppfmt, c, domain), c.RecordComment)
 		resps.Register(resp, domain)
 		if resp == setter.ResponseFailed {
-			if ShouldDisplayHints["update-timeout"] && errors.Is(context.Cause(ctx), errTimeout) {
+			if ShouldDisplayHints[HintUpdateTimeouts] && errors.Is(context.Cause(ctx), errTimeout) {
 				ppfmt.Infof(pp.EmojiHint,
 					"If your network is experiencing high latency, consider increasing UPDATE_TIMEOUT=%v",
 					c.UpdateTimeout,
 				)
-				ShouldDisplayHints["update-timeout"] = false
+				ShouldDisplayHints[HintUpdateTimeouts] = false
 			}
 		}
 	}
@@ -89,12 +96,12 @@ func deleteIP(
 		resp := s.Delete(ctx, ppfmt, domain, ipNet)
 		resps.Register(resp, domain)
 		if resp == setter.ResponseFailed {
-			if ShouldDisplayHints["update-timeout"] && errors.Is(context.Cause(ctx), errTimeout) {
+			if ShouldDisplayHints[HintUpdateTimeouts] && errors.Is(context.Cause(ctx), errTimeout) {
 				ppfmt.Infof(pp.EmojiHint,
 					"If your network is experiencing high latency, consider increasing UPDATE_TIMEOUT=%v",
 					c.UpdateTimeout,
 				)
-				ShouldDisplayHints["update-timeout"] = false
+				ShouldDisplayHints[HintUpdateTimeouts] = false
 			}
 		}
 	}
@@ -114,12 +121,12 @@ func detectIP(ctx context.Context, ppfmt pp.PP,
 	} else {
 		ppfmt.Errorf(pp.EmojiError, "Failed to detect the %s address", ipNet.Describe())
 
-		if ShouldDisplayHints["detect-timeout"] && errors.Is(context.Cause(ctx), errTimeout) {
+		if ShouldDisplayHints[HintDetectionTimeouts] && errors.Is(context.Cause(ctx), errTimeout) {
 			ppfmt.Infof(pp.EmojiHint,
 				"If your network is experiencing high latency, consider increasing DETECTION_TIMEOUT=%v",
 				c.DetectionTimeout,
 			)
-			ShouldDisplayHints["detect-timeout"] = false
+			ShouldDisplayHints[HintDetectionTimeouts] = false
 		} else if ShouldDisplayHints[getHintIDForDetection(ipNet)] {
 			switch ipNet {
 			case ipnet.IP6:


Test patch:
diff --git a/internal/updater/updater_test.go b/internal/updater/updater_test.go
index d9c8f25..6e08069 100644
--- a/internal/updater/updater_test.go
+++ b/internal/updater/updater_test.go
@@ -342,7 +342,11 @@ func TestUpdateIPsHints(t *testing.T) {
 		prepareMockSetter   func(ppfmt pp.PP, m *mocks.MockSetter)
 	}{
 		"ip6fails/again": {
-			map[string]bool{"detect-ip4-fail": true, "detect-ip6-fail": false, "update-timeout": true},
+			map[string]bool{
+				updater.HintIP4DetectionFails: true,
+				updater.HintIP6DetectionFails: false,
+				updater.HintUpdateTimeouts:    true,
+			},
 			false,
 			[]string{"Failed to detect IPv6 address"},
 			[]string{"Failed to detect the IPv6 address."},