Skip to content

Commit

Permalink
fix: make the quiet mode quieter (#415)
Browse files Browse the repository at this point in the history
* style: hide more stuff during the quiet mode

* style: omit the redundant "deleting all managed records"

* style: redesign the output of privilege dropping and config errors

* style: change parsing errors

* test(config): test ReadLinuxID

* test(config): add more test cases
  • Loading branch information
favonia authored Mar 13, 2023
1 parent 9ddc185 commit 92a4462
Show file tree
Hide file tree
Showing 16 changed files with 415 additions and 303 deletions.
4 changes: 2 additions & 2 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,9 @@ services:
# Restart the updater after reboot
cap_add:
- SETUID
# Capability to change UID
# Capability to change user ID
- SETGID
# Capability to change GID
# Capability to change group ID
cap_drop:
- all
# Drop all other capabilities
Expand Down
22 changes: 10 additions & 12 deletions cmd/ddns/ddns.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ func initConfig(ctx context.Context, ppfmt pp.PP) (*config.Config, setter.Setter

func stopUpdating(ctx context.Context, ppfmt pp.PP, c *config.Config, s setter.Setter) {
if c.DeleteOnStop {
ppfmt.Noticef(pp.EmojiClearRecord, "Deleting all managed records . . .")
if ok, msg := updater.ClearIPs(ctx, ppfmt, c, s); ok {
c.Monitor.Log(ctx, ppfmt, msg)
} else {
Expand All @@ -71,19 +70,18 @@ func main() {
func realMain() int { //nolint:funlen
ppfmt := pp.New(os.Stdout)
if !config.ReadEmoji("EMOJI", &ppfmt) || !config.ReadQuiet("QUIET", &ppfmt) {
ppfmt.Noticef(pp.EmojiUserError, "Bye!")
ppfmt.Infof(pp.EmojiUserError, "Bye!")
return 1
}
if !ppfmt.IsEnabledFor(pp.Info) {
ppfmt.Noticef(pp.EmojiMute, "Quiet mode enabled")
}

// Show the name and the version of the updater
ppfmt.Noticef(pp.EmojiStar, formatName())
ppfmt.Infof(pp.EmojiStar, formatName())

// Drop the superuser privilege
droproot.DropPriviledges(ppfmt)
droproot.PrintPriviledges(ppfmt)
if !droproot.DropPrivileges(ppfmt) {
ppfmt.Infof(pp.EmojiBye, "Bye!")
return 1
}

// Catch signals SIGINT and SIGTERM
sig := signal.Setup()
Expand All @@ -99,7 +97,7 @@ func realMain() int { //nolint:funlen
// Bail out now if initConfig failed
if !configOk {
c.Monitor.ExitStatus(ctx, ppfmt, 1, "Config errors")
ppfmt.Noticef(pp.EmojiBye, "Bye!")
ppfmt.Infof(pp.EmojiBye, "Bye!")
return 1
}

Expand All @@ -123,7 +121,7 @@ func realMain() int { //nolint:funlen

// Maybe the cron was disabled?
if c.UpdateCron == nil {
ppfmt.Noticef(pp.EmojiBye, "Bye!")
ppfmt.Infof(pp.EmojiBye, "Bye!")
return 0
}

Expand All @@ -132,7 +130,7 @@ func realMain() int { //nolint:funlen
ppfmt.Errorf(pp.EmojiUserError, "No scheduled updates in near future")
stopUpdating(ctx, ppfmt, c, s)
c.Monitor.ExitStatus(ctx, ppfmt, 1, "No scheduled updates")
ppfmt.Noticef(pp.EmojiBye, "Bye!")
ppfmt.Infof(pp.EmojiBye, "Bye!")
return 1
}

Expand All @@ -144,7 +142,7 @@ func realMain() int { //nolint:funlen
if !sig.Sleep(ppfmt, interval) {
stopUpdating(ctx, ppfmt, c, s)
c.Monitor.ExitStatus(ctx, ppfmt, 0, "Terminated")
ppfmt.Noticef(pp.EmojiBye, "Bye!")
ppfmt.Infof(pp.EmojiBye, "Bye!")
return 0
}
} // mainLoop
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ require (
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
golang.org/x/exp v0.0.0-20230310171629-522b1b587ee0 // indirect
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c // indirect
golang.org/x/text v0.8.0 // indirect
golang.org/x/time v0.3.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1
go.uber.org/goleak v1.1.10 h1:z+mqJhf6ss6BSfSM671tgKyZBFPTTJM+HLxnhPC3wu0=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/exp v0.0.0-20230310171629-522b1b587ee0 h1:LGJsf5LRplCck6jUCH3dBL2dmycNruWNF5xugkSlfXw=
golang.org/x/exp v0.0.0-20230310171629-522b1b587ee0/go.mod h1:CxIveKay+FTh1D0yPZemJVgC/95VzuuOLq5Qi4xnoYc=
golang.org/x/lint v0.0.0-20201208152925-83fdc39ff7b5 h1:2M3HP5CCK1Si9FQhwnzYhXdG6DXeebvUHFpre8QvbyI=
golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
Expand Down
24 changes: 4 additions & 20 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"strings"
"time"

"golang.org/x/exp/slices"

"github.com/favonia/cloudflare-ddns/internal/api"
"github.com/favonia/cloudflare-ddns/internal/cron"
"github.com/favonia/cloudflare-ddns/internal/domain"
Expand Down Expand Up @@ -114,25 +116,7 @@ func ReadAuth(ppfmt pp.PP, field *api.Auth) bool {
// returning true if elements are already distinct.
func deduplicate(list []domain.Domain) []domain.Domain {
domain.SortDomains(list)

if len(list) == 0 {
return list
}

j := 0
for i := range list {
if i == 0 || list[j] == list[i] {
continue
}
j++
list[j] = list[i]
}

if len(list) == j+1 {
return list
}

return list[:j+1]
return slices.Compact(list)
}

// ReadDomainMap reads environment variables DOMAINS, IP4_DOMAINS, and IP6_DOMAINS
Expand Down Expand Up @@ -363,7 +347,7 @@ func (c *Config) NormalizeConfig(ppfmt pp.PP) bool {
}

// fill in proxyMap
proxiedPred, ok := domainexp.ParseExpression(ppfmt, c.ProxiedTemplate)
proxiedPred, ok := domainexp.ParseExpression(ppfmt, "PROXIED", c.ProxiedTemplate)
if !ok {
return false
}
Expand Down
19 changes: 14 additions & 5 deletions internal/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ func TestReadProviderMap(t *testing.T) {
},
false,
func(m *mocks.MockPP) {
m.EXPECT().Errorf(pp.EmojiUserError, "Failed to parse %q: not a valid provider", "flare")
m.EXPECT().Errorf(pp.EmojiUserError, "%s (%q) is not a valid provider", "IP4_PROVIDER", "flare")
},
},
} {
Expand All @@ -245,7 +245,7 @@ func TestReadProviderMap(t *testing.T) {
}
}

//nolint:paralleltest // environment vars are global
//nolint:paralleltest,funlen // environment vars are global
func TestReadDomainMap(t *testing.T) {
for name, tc := range map[string]struct {
domains string
Expand Down Expand Up @@ -282,6 +282,14 @@ func TestReadDomainMap(t *testing.T) {
true,
nil,
},
"ill-formed": {
" ", " ", "*.*", nil, false,
func(m *mocks.MockPP) {
m.EXPECT().Errorf(pp.EmojiUserError,
"%s (%q) contains an ill-formed domain %q: %v",
"IP6_DOMAINS", "*.*", "*.*", gomock.Any())
},
},
} {
tc := tc
t.Run(name, func(t *testing.T) {
Expand Down Expand Up @@ -530,6 +538,7 @@ func TestReadEnvEmpty(t *testing.T) {
func TestNormalizeConfig(t *testing.T) {
t.Parallel()

keyProxied := "PROXIED"
var empty config.Config

for name, tc := range map[string]struct {
Expand Down Expand Up @@ -719,7 +728,7 @@ func TestNormalizeConfig(t *testing.T) {
m.EXPECT().IsEnabledFor(pp.Info).Return(true),
m.EXPECT().Infof(pp.EmojiEnvVars, "Checking settings . . ."),
m.EXPECT().IncIndent().Return(m),
m.EXPECT().Errorf(pp.EmojiUserError, "Failed to parse %q: wanted a boolean expression; got %q", `range`, `range`),
m.EXPECT().Errorf(pp.EmojiUserError, "%s (%q) is not a boolean expression: got unexpected token %q", keyProxied, `range`, `range`), //nolint:lll
)
},
},
Expand All @@ -740,7 +749,7 @@ func TestNormalizeConfig(t *testing.T) {
m.EXPECT().IsEnabledFor(pp.Info).Return(true),
m.EXPECT().Infof(pp.EmojiEnvVars, "Checking settings . . ."),
m.EXPECT().IncIndent().Return(m),
m.EXPECT().Errorf(pp.EmojiUserError, "Failed to parse %q: wanted a boolean expression; got %q", "999", "999"),
m.EXPECT().Errorf(pp.EmojiUserError, "%s (%q) is not a boolean expression: got unexpected token %q", keyProxied, `999`, `999`), //nolint:lll
)
},
},
Expand All @@ -761,7 +770,7 @@ func TestNormalizeConfig(t *testing.T) {
m.EXPECT().IsEnabledFor(pp.Info).Return(true),
m.EXPECT().Infof(pp.EmojiEnvVars, "Checking settings . . ."),
m.EXPECT().IncIndent().Return(m),
m.EXPECT().Errorf(pp.EmojiUserError, `Failed to parse %q: wanted %q; reached end of string`, `is(12345`, ")"),
m.EXPECT().Errorf(pp.EmojiUserError, `%s (%q) is missing %q at the end`, keyProxied, `is(12345`, ")"),
)
},
},
Expand Down
Loading

0 comments on commit 92a4462

Please sign in to comment.