Skip to content

Commit

Permalink
refactor(pp): clean up pretty printing (#866)
Browse files Browse the repository at this point in the history
  • Loading branch information
favonia authored Aug 11, 2024
1 parent 87d93fd commit a9b08e6
Show file tree
Hide file tree
Showing 29 changed files with 318 additions and 281 deletions.
2 changes: 2 additions & 0 deletions .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@ linters-settings:
- (github.com/favonia/cloudflare-ddns/internal/pp.PP).Noticef
- (github.com/favonia/cloudflare-ddns/internal/pp.PP).Warningf
- (github.com/favonia/cloudflare-ddns/internal/pp.PP).Errorf
- (github.com/favonia/cloudflare-ddns/internal/pp.PP).Hintf
- (*github.com/favonia/cloudflare-ddns/internal/mocks.MockPPMockRecorder).Infof
- (*github.com/favonia/cloudflare-ddns/internal/mocks.MockPPMockRecorder).Noticef
- (*github.com/favonia/cloudflare-ddns/internal/mocks.MockPPMockRecorder).Warningf
- (*github.com/favonia/cloudflare-ddns/internal/mocks.MockPPMockRecorder).Errorf
- (*github.com/favonia/cloudflare-ddns/internal/mocks.MockPPMockRecorder).Hintf
revive:
rules:
- name: exported
Expand Down
5 changes: 3 additions & 2 deletions internal/api/cloudflare.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,9 @@ func (h CloudflareHandle) SanityCheck(ctx context.Context, ppfmt pp.PP) bool {
ok = false
goto permanently
default:
ppfmt.Warningf(pp.EmojiImpossible, "The Cloudflare API token is in an undocumented state: %s", res.Status)
ppfmt.Warningf(pp.EmojiImpossible, "Please report the bug at https://github.com/favonia/cloudflare-ddns/issues/new") //nolint:lll
ppfmt.Warningf(pp.EmojiImpossible,
"The Cloudflare API token is in an undocumented state %q; please report this at %s",
res.Status, pp.IssueReportingURL)
goto permanently
}

Expand Down
8 changes: 5 additions & 3 deletions internal/api/cloudflare_records.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ func (h CloudflareHandle) ListZones(ctx context.Context, ppfmt pp.PP, name strin
ppfmt.Infof(pp.EmojiWarning, "Zone %q is %q and thus skipped", name, zone.Status)
// skip these
default:
ppfmt.Warningf(pp.EmojiImpossible, "Zone %q is in an undocumented status %q; please report this at https://github.com/favonia/cloudflare-ddns/issues/new", name, zone.Status) //nolint:lll
ppfmt.Warningf(pp.EmojiImpossible, "Zone %q is in an undocumented status %q; please report this at %s",
name, zone.Status, pp.IssueReportingURL)
ids = append(ids, zone.ID)
}
}
Expand Down Expand Up @@ -89,8 +90,9 @@ zoneSearch:
h.cache.zoneOfDomain.Set(domain.DNSNameASCII(), zones[0], ttlcache.DefaultTTL)
return zones[0], true
default: // len(zones) > 1
ppfmt.Warningf(pp.EmojiImpossible, "Found multiple active zones named %q", zoneName)
ppfmt.Warningf(pp.EmojiImpossible, "Please report this rare situation at https://github.com/favonia/cloudflare-ddns/issues/new") //nolint:lll
ppfmt.Warningf(pp.EmojiImpossible,
"Found multiple active zones named %q (IDs: %s); please report this at %s",
zoneName, pp.EnglishJoin(zones), pp.IssueReportingURL)
return "", false
}
}
Expand Down
24 changes: 10 additions & 14 deletions internal/api/cloudflare_records_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,12 +214,8 @@ func TestZoneOfDomain(t *testing.T) {
gomock.InOrder(
m.EXPECT().Warningf(
pp.EmojiImpossible,
"Found multiple active zones named %q",
"test.org",
),
m.EXPECT().Warningf(
pp.EmojiImpossible,
"Please report this rare situation at https://github.com/favonia/cloudflare-ddns/issues/new",
"Found multiple active zones named %q (IDs: %s); please report this at %s",
"test.org", pp.EnglishJoin(mockIDs("test.org", 0, 1)), pp.IssueReportingURL,
),
)
},
Expand All @@ -232,12 +228,8 @@ func TestZoneOfDomain(t *testing.T) {
gomock.InOrder(
m.EXPECT().Warningf(
pp.EmojiImpossible,
"Found multiple active zones named %q",
"test.org",
),
m.EXPECT().Warningf(
pp.EmojiImpossible,
"Please report this rare situation at https://github.com/favonia/cloudflare-ddns/issues/new",
"Found multiple active zones named %q (IDs: %s); please report this at %s",
"test.org", pp.EnglishJoin(mockIDs("test.org", 0, 1)), pp.IssueReportingURL,
),
)
},
Expand Down Expand Up @@ -281,7 +273,9 @@ func TestZoneOfDomain(t *testing.T) {
1, mockID("test.org", 0), true,
func(m *mocks.MockPP) {
gomock.InOrder(
m.EXPECT().Warningf(pp.EmojiWarning, "Zone %q is %q; your Cloudflare setup is incomplete; some features might not work as expected", "test.org", "initializing"), //nolint:lll
m.EXPECT().Warningf(pp.EmojiWarning,
"Zone %q is %q; your Cloudflare setup is incomplete; some features might not work as expected",
"test.org", "initializing"),
)
},
},
Expand All @@ -290,7 +284,9 @@ func TestZoneOfDomain(t *testing.T) {
map[string][]string{"test.org": {"some-undocumented-status"}},
1, mockID("test.org", 0), true,
func(m *mocks.MockPP) {
m.EXPECT().Warningf(pp.EmojiImpossible, "Zone %q is in an undocumented status %q; please report this at https://github.com/favonia/cloudflare-ddns/issues/new", "test.org", "some-undocumented-status") //nolint:lll
m.EXPECT().Warningf(pp.EmojiImpossible,
"Zone %q is in an undocumented status %q; please report this at %s",
"test.org", "some-undocumented-status", pp.IssueReportingURL)
},
},
} {
Expand Down
5 changes: 3 additions & 2 deletions internal/api/cloudflare_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,8 +265,9 @@ func TestSanityCheckExpiring(t *testing.T) {
true,
func(p *mocks.MockPP) {
gomock.InOrder(
p.EXPECT().Warningf(pp.EmojiImpossible, "The Cloudflare API token is in an undocumented state: %s", "funny"),
p.EXPECT().Warningf(pp.EmojiImpossible, "Please report the bug at https://github.com/favonia/cloudflare-ddns/issues/new"), //nolint:lll
p.EXPECT().Warningf(pp.EmojiImpossible,
"The Cloudflare API token is in an undocumented state %q; please report this at %s",
"funny", pp.IssueReportingURL),
)
},
},
Expand Down
5 changes: 2 additions & 3 deletions internal/api/cloudflare_waf.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,8 @@ func (h CloudflareHandle) ListWAFLists(ctx context.Context, ppfmt pp.PP) (map[st
if l.Kind == cloudflare.ListTypeIP {
if anotherListID, conflicting := lmap[l.Name]; conflicting {
ppfmt.Warningf(pp.EmojiImpossible,
"Found multiple lists named %q (IDs: %s and %s); please report this at "+
"https://github.com/favonia/cloudflare-ddns/issues/new",
l.Name, anotherListID, l.ID)
"Found multiple lists named %q (IDs: %s and %s); please report this at %s",
l.Name, anotherListID, l.ID, pp.IssueReportingURL)
return nil, false
}

Expand Down
8 changes: 4 additions & 4 deletions internal/api/cloudflare_waf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ func TestListWAFLists(t *testing.T) {
false, nil,
func(ppfmt *mocks.MockPP) {
ppfmt.EXPECT().Warningf(pp.EmojiImpossible,
"Found multiple lists named %q (IDs: %s and %s); please report this at https://github.com/favonia/cloudflare-ddns/issues/new", //nolint:lll
"list", mockID("list", 0), mockID("list", 2),
"Found multiple lists named %q (IDs: %s and %s); please report this at %s",
"list", mockID("list", 0), mockID("list", 2), pp.IssueReportingURL,
)
},
},
Expand Down Expand Up @@ -228,8 +228,8 @@ func TestFindWAFList(t *testing.T) {
func(ppfmt *mocks.MockPP) {
gomock.InOrder(
ppfmt.EXPECT().Warningf(pp.EmojiImpossible,
"Found multiple lists named %q (IDs: %s and %s); please report this at https://github.com/favonia/cloudflare-ddns/issues/new", //nolint:lll
"list", mockID("list", 0), mockID("list", 2),
"Found multiple lists named %q (IDs: %s and %s); please report this at %s",
"list", mockID("list", 0), mockID("list", 2), pp.IssueReportingURL,
),
ppfmt.EXPECT().Warningf(pp.EmojiError,
"Failed to find the list %q",
Expand Down
3 changes: 1 addition & 2 deletions internal/config/check_root.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ func CheckRoot(ppfmt pp.PP) {
useDeprecated = true
}
if useDeprecated {
ppfmt.Warningf(pp.EmojiHint,
"See https://github.com/favonia/cloudflare-ddns for the new Docker template")
ppfmt.Hintf(pp.HintUpdateDockerTemplate, "See %s for the new Docker template", pp.ManualURL)
}
}
2 changes: 1 addition & 1 deletion internal/config/check_root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func TestCheckRootWithOldConfigs(t *testing.T) {
calls = append(calls,
mockPP.EXPECT().Warningf(pp.EmojiUserError, "PUID=%s is ignored; use Docker's built-in mechanism to set user ID", "1000"), //nolint:lll
mockPP.EXPECT().Warningf(pp.EmojiUserError, "PGID=%s is ignored; use Docker's built-in mechanism to set group ID", "1000"), //nolint:lll
mockPP.EXPECT().Warningf(pp.EmojiHint, "See https://github.com/favonia/cloudflare-ddns for the new Docker template"),
mockPP.EXPECT().Hintf(pp.HintUpdateDockerTemplate, "See %s for the new Docker template", pp.ManualURL),
)
gomock.InOrder(calls...)

Expand Down
4 changes: 2 additions & 2 deletions internal/config/config_print.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ func (c *Config) Print(ppfmt pp.PP) {
}

ppfmt.Infof(pp.EmojiEnvVars, "Current settings:")
ppfmt = ppfmt.IncIndent()
inner := ppfmt.IncIndent()
ppfmt = ppfmt.Indent()
inner := ppfmt.Indent()

section := func(title string) { ppfmt.Infof(pp.EmojiConfig, title) }
item := func(title string, format string, values ...any) {
Expand Down
12 changes: 6 additions & 6 deletions internal/config/config_print_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ func TestPrintDefault(t *testing.T) {
gomock.InOrder(
mockPP.EXPECT().IsEnabledFor(pp.Info).Return(true),
mockPP.EXPECT().Infof(pp.EmojiEnvVars, "Current settings:"),
mockPP.EXPECT().IncIndent().Return(mockPP),
mockPP.EXPECT().IncIndent().Return(innerMockPP),
mockPP.EXPECT().Indent().Return(mockPP),
mockPP.EXPECT().Indent().Return(innerMockPP),
mockPP.EXPECT().Infof(pp.EmojiConfig, "Domains, IP providers, and WAF lists:"),
printItem(t, innerMockPP, "IPv4-enabled domains:", "(none)"),
printItem(t, innerMockPP, "IPv4 provider:", "cloudflare.trace"),
Expand Down Expand Up @@ -68,8 +68,8 @@ func TestPrintValues(t *testing.T) {
gomock.InOrder(
mockPP.EXPECT().IsEnabledFor(pp.Info).Return(true),
mockPP.EXPECT().Infof(pp.EmojiEnvVars, "Current settings:"),
mockPP.EXPECT().IncIndent().Return(mockPP),
mockPP.EXPECT().IncIndent().Return(innerMockPP),
mockPP.EXPECT().Indent().Return(mockPP),
mockPP.EXPECT().Indent().Return(innerMockPP),
mockPP.EXPECT().Infof(pp.EmojiConfig, "Domains, IP providers, and WAF lists:"),
printItem(t, innerMockPP, "IPv4-enabled domains:", "test4.org, *.test4.org"),
printItem(t, innerMockPP, "IPv4 provider:", "cloudflare.trace"),
Expand Down Expand Up @@ -140,8 +140,8 @@ func TestPrintEmpty(t *testing.T) {
gomock.InOrder(
mockPP.EXPECT().IsEnabledFor(pp.Info).Return(true),
mockPP.EXPECT().Infof(pp.EmojiEnvVars, "Current settings:"),
mockPP.EXPECT().IncIndent().Return(mockPP),
mockPP.EXPECT().IncIndent().Return(innerMockPP),
mockPP.EXPECT().Indent().Return(mockPP),
mockPP.EXPECT().Indent().Return(innerMockPP),
mockPP.EXPECT().Infof(pp.EmojiConfig, "Domains, IP providers, and WAF lists:"),
printItem(t, innerMockPP, "WAF lists:", "(none)"),
mockPP.EXPECT().Infof(pp.EmojiConfig, "Scheduling:"),
Expand Down
13 changes: 7 additions & 6 deletions internal/config/config_read.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
func (c *Config) ReadEnv(ppfmt pp.PP) bool {
if ppfmt.IsEnabledFor(pp.Info) {
ppfmt.Infof(pp.EmojiEnvVars, "Reading settings . . .")
ppfmt = ppfmt.IncIndent()
ppfmt = ppfmt.Indent()
}

if !ReadAuth(ppfmt, &c.Auth) ||
Expand Down Expand Up @@ -50,23 +50,24 @@ func (c *Config) ReadEnv(ppfmt pp.PP) bool {
func (c *Config) Normalize(ppfmt pp.PP) bool {
if ppfmt.IsEnabledFor(pp.Info) {
ppfmt.Infof(pp.EmojiEnvVars, "Checking settings . . .")
ppfmt = ppfmt.IncIndent()
ppfmt = ppfmt.Indent()
}

// Step 1: is there something to do, and do wo have an auth?
if c.Auth == nil {
// if c.Auth == nil, the user should have been warned.
ppfmt.Errorf(pp.EmojiImpossible, "Authorization info is missing, but this should be impossible")
ppfmt.Errorf(pp.EmojiImpossible, "Please report the bug at https://github.com/favonia/cloudflare-ddns/issues/new")
ppfmt.Errorf(pp.EmojiImpossible,
"Authorization info is missing, but this should be impossible; please report this at %s",
pp.IssueReportingURL)
return false
}
if len(c.Domains[ipnet.IP4]) == 0 && len(c.Domains[ipnet.IP6]) == 0 && len(c.WAFLists) == 0 {
ppfmt.Errorf(pp.EmojiUserError, "Nothing was specified in DOMAINS, IP4_DOMAINS, IP6_DOMAINS, or WAF_LISTS")
return false
}
if (len(c.Domains[ipnet.IP4]) > 0 || len(c.Domains[ipnet.IP6]) > 0) && !c.Auth.SupportsRecords() {
ppfmt.Errorf(pp.EmojiImpossible, "CF_API_TOKEN is empty, but the updater should have stopped earlier")
ppfmt.Errorf(pp.EmojiImpossible, "Please report the bug at https://github.com/favonia/cloudflare-ddns/issues/new")
ppfmt.Errorf(pp.EmojiImpossible, "CF_API_TOKEN is empty, but this should be impossible; please report this at %s",
pp.IssueReportingURL)
return false
}
if len(c.WAFLists) > 0 && !c.Auth.SupportsWAFLists() {
Expand Down
Loading

0 comments on commit a9b08e6

Please sign in to comment.