fix(pp): print blank lines to separate each round of updating (#958)

This commit is contained in:
favonia
2024-09-30 08:39:55 -05:00
committed by GitHub
parent 5b12a38f8d
commit 0a6c71beeb
8 changed files with 79 additions and 13 deletions

View File

@@ -124,6 +124,9 @@ func realMain() int {
if first && !c.UpdateOnStart {
c.Monitor.Ping(ctx, ppfmt, monitor.NewMessagef(true, "Started (no action)"))
} else {
// Improve readability of the logging by separating each round of checks with blank lines.
ppfmt.BlankLineIfVerbose()
msg := updater.UpdateIPs(ctxWithSignals, ppfmt, c, s)
c.Monitor.Ping(ctx, ppfmt, msg.MonitorMessage)
c.Notifier.Send(ctx, ppfmt, msg.NotifierMessage)

View File

@@ -20,7 +20,7 @@ const (
// HintAuthTokenNewPrefix contains the hint about the transition from
// CF_* to CLOUDFLARE_*.
const HintAuthTokenNewPrefix string = "Cloudflare is transitioning its tools to use the prefix CLOUDFLARE instead of CF. To align with this change, it is recommended to use CLOUDFLARE_API_TOKEN (or CLOUDFLARE_API_TOKEN_FILE) instead of CF_API_TOKEN (or CF_API_TOKEN_FILE) moving forward. All options will be fully supported until version 2.0." //nolint:lll,gosec
const HintAuthTokenNewPrefix string = "Cloudflare is transitioning its tools to use the prefix CLOUDFLARE instead of CF. To align with this change, it is recommended to use CLOUDFLARE_API_TOKEN (or CLOUDFLARE_API_TOKEN_FILE) instead of CF_API_TOKEN (or CF_API_TOKEN_FILE) moving forward. All these options will be fully supported until version 2.0." //nolint:lll,gosec
func readPlainAuthTokens(ppfmt pp.PP) (string, string, bool) {
token1 := Getenv(TokenKey1)

View File

@@ -38,6 +38,42 @@ func (m *MockPP) EXPECT() *MockPPMockRecorder {
return m.recorder
}
// BlankLineIfVerbose mocks base method.
func (m *MockPP) BlankLineIfVerbose() {
m.ctrl.T.Helper()
m.ctrl.Call(m, "BlankLineIfVerbose")
}
// BlankLineIfVerbose indicates an expected call of BlankLineIfVerbose.
func (mr *MockPPMockRecorder) BlankLineIfVerbose() *PPBlankLineIfVerboseCall {
mr.mock.ctrl.T.Helper()
call := mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "BlankLineIfVerbose", reflect.TypeOf((*MockPP)(nil).BlankLineIfVerbose))
return &PPBlankLineIfVerboseCall{Call: call}
}
// PPBlankLineIfVerboseCall wrap *gomock.Call
type PPBlankLineIfVerboseCall struct {
*gomock.Call
}
// Return rewrite *gomock.Call.Return
func (c *PPBlankLineIfVerboseCall) Return() *PPBlankLineIfVerboseCall {
c.Call = c.Call.Return()
return c
}
// Do rewrite *gomock.Call.Do
func (c *PPBlankLineIfVerboseCall) Do(f func()) *PPBlankLineIfVerboseCall {
c.Call = c.Call.Do(f)
return c
}
// DoAndReturn rewrite *gomock.Call.DoAndReturn
func (c *PPBlankLineIfVerboseCall) DoAndReturn(f func()) *PPBlankLineIfVerboseCall {
c.Call = c.Call.DoAndReturn(f)
return c
}
// Hintf mocks base method.
func (m *MockPP) Hintf(arg0 pp.Hint, arg1 string, arg2 ...any) {
m.ctrl.T.Helper()

View File

@@ -3,6 +3,18 @@ package pp
//go:generate mockgen -typed -destination=../mocks/mock_pp.go -package=mocks . PP
// Verbosity is the type of message levels.
type Verbosity int
// Pre-defined verbosity levels. A higher level means "more verbose".
const (
Notice Verbosity = iota // useful additional info
Info // important messages
Quiet Verbosity = Notice
Verbose Verbosity = Info
DefaultVerbosity Verbosity = Verbose
)
// PP is the abstraction of a pretty printer.
type PP interface {
// IsShowing checks whether a message of a certain level will be printed.
@@ -11,6 +23,9 @@ type PP interface {
// Indent returns a new pretty-printer with more indentation.
Indent() PP
// BlankLineIfVerbose prints a blank line at the [Verbose] level
BlankLineIfVerbose()
// Infof formats and prints a message at the info level.
Infof(emoji Emoji, format string, args ...any)

View File

@@ -14,18 +14,6 @@ type formatter struct {
verbosity Verbosity
}
// Verbosity is the type of message levels.
type Verbosity int
// Pre-defined verbosity levels. A higher level means "more verbose".
const (
Notice Verbosity = iota // useful additional info
Info // important messages
Quiet Verbosity = Notice
Verbose Verbosity = Info
DefaultVerbosity Verbosity = Verbose
)
// New creates a new pretty printer.
func New(writer io.Writer, emoji bool, verbosity Verbosity) PP {
return formatter{
@@ -53,6 +41,13 @@ func (f formatter) Indent() PP {
return f
}
// BlankLineIfVerbose prints a blank line.
func (f formatter) BlankLineIfVerbose() {
if f.IsShowing(Verbose) {
fmt.Fprintln(f.writer)
}
}
func (f formatter) output(v Verbosity, emoji Emoji, msg string) {
if !f.IsShowing(v) {
return

View File

@@ -42,14 +42,18 @@ func TestIndent(t *testing.T) {
middle.Noticef(pp.EmojiStar, "message2")
inner := middle.Indent()
outer.Noticef(pp.EmojiStar, "message3")
outer.BlankLineIfVerbose()
inner.Noticef(pp.EmojiStar, "message4")
inner.BlankLineIfVerbose()
middle.Noticef(pp.EmojiStar, "message5")
require.Equal(t,
`🌟 message1
🌟 message2
🌟 message3
🌟 message4
🌟 message5
`,
buf.String())

View File

@@ -30,26 +30,37 @@ func (b QueuedPP) Indent() PP {
return b
}
// BlankLineIfVerbose queues a call to [PP.BlankLineIfVerbose] of the upstream.
func (b QueuedPP) BlankLineIfVerbose() {
// It's important to save the current upstream because [Indent] may change it.
upstream := b.upstream
*b.queue = append(*b.queue, func() { upstream.BlankLineIfVerbose() })
}
// Infof queues a call to [PP.Infof] of the upstream.
func (b QueuedPP) Infof(emoji Emoji, format string, args ...any) {
// It's important to save the current upstream because [Indent] may change it.
upstream := b.upstream
*b.queue = append(*b.queue, func() { upstream.Infof(emoji, format, args...) })
}
// Noticef queues a call to [PP.Noticef] of the upstream.
func (b QueuedPP) Noticef(emoji Emoji, format string, args ...any) {
// It's important to save the current upstream because [Indent] may change it.
upstream := b.upstream
*b.queue = append(*b.queue, func() { upstream.Noticef(emoji, format, args...) })
}
// SuppressHint queues a call to [PP.SuppressHint] of the upstream.
func (b QueuedPP) SuppressHint(hint Hint) {
// It's important to save the current upstream because [Indent] may change it.
upstream := b.upstream
*b.queue = append(*b.queue, func() { upstream.SuppressHint(hint) })
}
// Hintf queues a call to [PP.Hintf] of the upstream.
func (b QueuedPP) Hintf(hint Hint, format string, args ...any) {
// It's important to save the current upstream because [Indent] may change it.
upstream := b.upstream
*b.queue = append(*b.queue, func() { upstream.Hintf(hint, format, args...) })
}

View File

@@ -25,6 +25,7 @@ func TestQueued(t *testing.T) {
}
ppfmt.Noticef(pp.EmojiNotify, "some message")
ppfmt.SuppressHint(pp.HintDetectionTimeouts)
ppfmt.BlankLineIfVerbose()
ppfmt.Hintf(pp.HintIP4DetectionFails, "cannot do IPv4")
queued.Flush()
@@ -36,6 +37,7 @@ func TestQueued(t *testing.T) {
ppfmt.EXPECT().Infof(pp.EmojiBullet, "Test"),
inner.EXPECT().Noticef(pp.EmojiNotify, "some message"),
inner.EXPECT().SuppressHint(pp.HintDetectionTimeouts),
inner.EXPECT().BlankLineIfVerbose(),
inner.EXPECT().Hintf(pp.HintIP4DetectionFails, "cannot do IPv4"),
)
},