Skip to content

Commit 074e300

Browse files
authored
Format code with golines (#475)
1 parent 523e068 commit 074e300

15 files changed

+173
-71
lines changed

.toolbox.mk

+18
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ $(TB_LOCALBIN):
1111
## Tool Binaries
1212
TB_DEEPCOPY_GEN ?= $(TB_LOCALBIN)/deepcopy-gen
1313
TB_GINKGO ?= $(TB_LOCALBIN)/ginkgo
14+
TB_GOFUMPT ?= $(TB_LOCALBIN)/gofumpt
1415
TB_GOLANGCI_LINT ?= $(TB_LOCALBIN)/golangci-lint
16+
TB_GOLINES ?= $(TB_LOCALBIN)/golines
1517
TB_GORELEASER ?= $(TB_LOCALBIN)/goreleaser
1618
TB_MOCKGEN ?= $(TB_LOCALBIN)/mockgen
1719
TB_OAPI_CODEGEN ?= $(TB_LOCALBIN)/oapi-codegen
@@ -20,8 +22,12 @@ TB_SEMVER ?= $(TB_LOCALBIN)/semver
2022
## Tool Versions
2123
# renovate: packageName=k8s.io/code-generator/cmd/deepcopy-gen
2224
TB_DEEPCOPY_GEN_VERSION ?= v0.32.0
25+
# renovate: packageName=mvdan.cc/gofumpt
26+
TB_GOFUMPT_VERSION ?= v0.7.0
2327
# renovate: packageName=github.com/golangci/golangci-lint/cmd/golangci-lint
2428
TB_GOLANGCI_LINT_VERSION ?= v1.62.2
29+
# renovate: packageName=github.com/segmentio/golines
30+
TB_GOLINES_VERSION ?= v0.12.2
2531
# renovate: packageName=github.com/goreleaser/goreleaser/v2
2632
TB_GORELEASER_VERSION ?= v2.5.0
2733
# renovate: packageName=go.uber.org/mock/mockgen
@@ -40,10 +46,18 @@ $(TB_DEEPCOPY_GEN): $(TB_LOCALBIN)
4046
tb.ginkgo: $(TB_GINKGO) ## Download ginkgo locally if necessary.
4147
$(TB_GINKGO): $(TB_LOCALBIN)
4248
test -s $(TB_LOCALBIN)/ginkgo || GOBIN=$(TB_LOCALBIN) go install github.com/onsi/ginkgo/v2/ginkgo
49+
.PHONY: tb.gofumpt
50+
tb.gofumpt: $(TB_GOFUMPT) ## Download gofumpt locally if necessary.
51+
$(TB_GOFUMPT): $(TB_LOCALBIN)
52+
test -s $(TB_LOCALBIN)/gofumpt || GOBIN=$(TB_LOCALBIN) go install mvdan.cc/gofumpt@$(TB_GOFUMPT_VERSION)
4353
.PHONY: tb.golangci-lint
4454
tb.golangci-lint: $(TB_GOLANGCI_LINT) ## Download golangci-lint locally if necessary.
4555
$(TB_GOLANGCI_LINT): $(TB_LOCALBIN)
4656
test -s $(TB_LOCALBIN)/golangci-lint || GOBIN=$(TB_LOCALBIN) go install github.com/golangci/golangci-lint/cmd/golangci-lint@$(TB_GOLANGCI_LINT_VERSION)
57+
.PHONY: tb.golines
58+
tb.golines: $(TB_GOLINES) ## Download golines locally if necessary.
59+
$(TB_GOLINES): $(TB_LOCALBIN)
60+
test -s $(TB_LOCALBIN)/golines || GOBIN=$(TB_LOCALBIN) go install github.com/segmentio/golines@$(TB_GOLINES_VERSION)
4761
.PHONY: tb.goreleaser
4862
tb.goreleaser: $(TB_GORELEASER) ## Download goreleaser locally if necessary.
4963
$(TB_GORELEASER): $(TB_LOCALBIN)
@@ -67,7 +81,9 @@ tb.reset:
6781
@rm -f \
6882
$(TB_LOCALBIN)/deepcopy-gen \
6983
$(TB_LOCALBIN)/ginkgo \
84+
$(TB_LOCALBIN)/gofumpt \
7085
$(TB_LOCALBIN)/golangci-lint \
86+
$(TB_LOCALBIN)/golines \
7187
$(TB_LOCALBIN)/goreleaser \
7288
$(TB_LOCALBIN)/mockgen \
7389
$(TB_LOCALBIN)/oapi-codegen \
@@ -78,7 +94,9 @@ tb.reset:
7894
tb.update: tb.reset
7995
toolbox makefile --renovate -f $(TB_LOCALDIR)/Makefile \
8096
k8s.io/code-generator/cmd/[email protected]/kubernetes/code-generator \
97+
mvdan.cc/[email protected]/mvdan/gofumpt \
8198
github.com/golangci/golangci-lint/cmd/golangci-lint \
99+
github.com/segmentio/golines \
82100
github.com/goreleaser/goreleaser/v2 \
83101
go.uber.org/mock/[email protected]/uber-go/mock \
84102
github.com/oapi-codegen/oapi-codegen/v2/cmd/oapi-codegen \

Makefile

+3
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ generate: tb.deepcopy-gen
1414
@touch ./tmp/deepcopy-gen-boilerplate.go.txt
1515
$(TB_DEEPCOPY_GEN) --go-header-file ./tmp/deepcopy-gen-boilerplate.go.txt --bounding-dirs ./pkg/types
1616

17+
fmt: tb.golines tb.gofumpt
18+
$(TB_GOLINES) --base-formatter="$(TB_GOFUMPT)" --max-len=120 --write-output .
19+
1720
# Run tests
1821
test: generate lint test-ci
1922

cmd/run.go

+10-5
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ func init() {
5050
doCmd.PersistentFlags().Bool(config.FlagContinueOnError, false, "If enabled, the synchronisation task "+
5151
"will not fail on single errors, but will log the errors and continue.")
5252

53-
doCmd.PersistentFlags().Int(config.FlagApiPort, 8080, "Sync API Port, the API endpoint will be started to enable remote triggering; if 0 port API is disabled.")
53+
doCmd.PersistentFlags().
54+
Int(config.FlagApiPort, 8080, "Sync API Port, the API endpoint will be started to enable remote triggering; if 0 port API is disabled.")
5455
doCmd.PersistentFlags().String(config.FlagApiUsername, "", "Sync API username")
5556
doCmd.PersistentFlags().String(config.FlagApiPassword, "", "Sync API password")
5657
doCmd.PersistentFlags().String(config.FlagApiDarkMode, "", "API UI in dark mode")
@@ -70,20 +71,24 @@ func init() {
7071
doCmd.PersistentFlags().Bool(config.FlagFeatureFilters, true, "Enable filters sync feature")
7172

7273
doCmd.PersistentFlags().String(config.FlagOriginURL, "", "Origin instance url")
73-
doCmd.PersistentFlags().String(config.FlagOriginWebURL, "", "Origin instance web url used in the web interface (default: <origin-url>)")
74+
doCmd.PersistentFlags().
75+
String(config.FlagOriginWebURL, "", "Origin instance web url used in the web interface (default: <origin-url>)")
7476
doCmd.PersistentFlags().String(config.FlagOriginApiPath, "/control", "Origin instance API path")
7577
doCmd.PersistentFlags().String(config.FlagOriginUsername, "", "Origin instance username")
7678
doCmd.PersistentFlags().String(config.FlagOriginPassword, "", "Origin instance password")
7779
doCmd.PersistentFlags().String(config.FlagOriginCookie, "", "If Set, uses a cookie for authentication")
7880
doCmd.PersistentFlags().Bool(config.FlagOriginISV, false, "Enable Origin instance InsecureSkipVerify")
7981

8082
doCmd.PersistentFlags().String(config.FlagReplicaURL, "", "Replica instance url")
81-
doCmd.PersistentFlags().String(config.FlagReplicaWebURL, "", "Replica instance web url used in the web interface (default: <replica-url>)")
83+
doCmd.PersistentFlags().
84+
String(config.FlagReplicaWebURL, "", "Replica instance web url used in the web interface (default: <replica-url>)")
8285
doCmd.PersistentFlags().String(config.FlagReplicaApiPath, "/control", "Replica instance API path")
8386
doCmd.PersistentFlags().String(config.FlagReplicaUsername, "", "Replica instance username")
8487
doCmd.PersistentFlags().String(config.FlagReplicaPassword, "", "Replica instance password")
8588
doCmd.PersistentFlags().String(config.FlagReplicaCookie, "", "If Set, uses a cookie for authentication")
8689
doCmd.PersistentFlags().Bool(config.FlagReplicaISV, false, "Enable Replica instance InsecureSkipVerify")
87-
doCmd.PersistentFlags().Bool(config.FlagReplicaAutoSetup, false, "Enable automatic setup of new AdguardHome instances. This replaces the setup wizard.")
88-
doCmd.PersistentFlags().String(config.FlagReplicaInterfaceName, "", "Optional change the interface name of the replica if it differs from the master")
90+
doCmd.PersistentFlags().
91+
Bool(config.FlagReplicaAutoSetup, false, "Enable automatic setup of new AdguardHome instances. This replaces the setup wizard.")
92+
doCmd.PersistentFlags().
93+
String(config.FlagReplicaInterfaceName, "", "Optional change the interface name of the replica if it differs from the master")
8994
}

openapi/main.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ func main() {
2020
}
2121
log.Printf("Patching schema version %s\n", version)
2222

23-
resp, err := http.Get(fmt.Sprintf("https://raw.githubusercontent.com/AdguardTeam/AdGuardHome/%s/openapi/openapi.yaml", version))
23+
resp, err := http.Get(
24+
fmt.Sprintf("https://raw.githubusercontent.com/AdguardTeam/AdGuardHome/%s/openapi/openapi.yaml", version),
25+
)
2426
if err != nil {
2527
log.Fatalln(err)
2628
}

pkg/client/client.go

+14-4
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,10 @@ func (cl *client) Stats() (*model.Stats, error) {
166166

167167
func (cl *client) QueryLog(limit int) (*model.QueryLog, error) {
168168
ql := &model.QueryLog{}
169-
err := cl.doGet(cl.client.R().EnableTrace().SetResult(ql), fmt.Sprintf(`querylog?limit=%d&response_status="all"`, limit))
169+
err := cl.doGet(
170+
cl.client.R().EnableTrace().SetResult(ql),
171+
fmt.Sprintf(`querylog?limit=%d&response_status="all"`, limit),
172+
)
170173
return ql, err
171174
}
172175

@@ -260,7 +263,10 @@ func (cl *client) UpdateFilter(whitelist bool, f model.Filter) error {
260263

261264
func (cl *client) RefreshFilters(whitelist bool) error {
262265
cl.log.With("whitelist", whitelist).Info("Refresh filter")
263-
return cl.doPost(cl.client.R().EnableTrace().SetBody(&model.FilterRefreshRequest{Whitelist: utils.Ptr(whitelist)}), "/filtering/refresh")
266+
return cl.doPost(
267+
cl.client.R().EnableTrace().SetBody(&model.FilterRefreshRequest{Whitelist: utils.Ptr(whitelist)}),
268+
"/filtering/refresh",
269+
)
264270
}
265271

266272
func (cl *client) ToggleProtection(enable bool) error {
@@ -309,7 +315,10 @@ func (cl *client) AddClient(client *model.Client) error {
309315

310316
func (cl *client) UpdateClient(client *model.Client) error {
311317
cl.log.With("name", *client.Name).Info("Update client settings")
312-
return cl.doPost(cl.client.R().EnableTrace().SetBody(&model.ClientUpdate{Name: client.Name, Data: client}), "/clients/update")
318+
return cl.doPost(
319+
cl.client.R().EnableTrace().SetBody(&model.ClientUpdate{Name: client.Name, Data: client}),
320+
"/clients/update",
321+
)
313322
}
314323

315324
func (cl *client) DeleteClient(client *model.Client) error {
@@ -324,7 +333,8 @@ func (cl *client) QueryLogConfig() (*model.QueryLogConfigWithIgnored, error) {
324333
}
325334

326335
func (cl *client) SetQueryLogConfig(qlc *model.QueryLogConfigWithIgnored) error {
327-
cl.log.With("enabled", *qlc.Enabled, "interval", *qlc.Interval, "anonymizeClientIP", *qlc.AnonymizeClientIp).Info("Set query log config")
336+
cl.log.With("enabled", *qlc.Enabled, "interval", *qlc.Interval, "anonymizeClientIP", *qlc.AnonymizeClientIp).
337+
Info("Set query log config")
328338
return cl.doPut(cl.client.R().EnableTrace().SetBody(qlc), "/querylog/config/update")
329339
}
330340

pkg/client/client_test.go

+12-2
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,14 @@ var _ = Describe("Client", func() {
137137

138138
Context("Setup", func() {
139139
It("should add setup the instance", func() {
140-
ts, cl = ClientPost("/install/configure", fmt.Sprintf(`{"web":{"ip":"0.0.0.0","port":3000,"status":"","can_autofix":false},"dns":{"ip":"0.0.0.0","port":53,"status":"","can_autofix":false},"username":"%s","password":"%s"}`, username, password))
140+
ts, cl = ClientPost(
141+
"/install/configure",
142+
fmt.Sprintf(
143+
`{"web":{"ip":"0.0.0.0","port":3000,"status":"","can_autofix":false},"dns":{"ip":"0.0.0.0","port":53,"status":"","can_autofix":false},"username":"%s","password":"%s"}`,
144+
username,
145+
password,
146+
),
147+
)
141148
err := cl.Setup()
142149
Ω(err).ShouldNot(HaveOccurred())
143150
})
@@ -303,7 +310,10 @@ var _ = Describe("Client", func() {
303310
Ω(*qlc.Interval).Should(Equal(model.QueryLogConfigInterval(90)))
304311
})
305312
It("should set QueryLogConfig", func() {
306-
ts, cl = ClientPut("/querylog/config/update", `{"anonymize_client_ip":true,"enabled":true,"interval":123,"ignored":["foo.bar"]}`)
313+
ts, cl = ClientPut(
314+
"/querylog/config/update",
315+
`{"anonymize_client_ip":true,"enabled":true,"interval":123,"ignored":["foo.bar"]}`,
316+
)
307317

308318
var interval model.QueryLogConfigInterval = 123
309319
err := cl.SetQueryLogConfig(&model.QueryLogConfigWithIgnored{

pkg/client/model/model-functions.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,9 @@ func (c *DNSConfig) Sanitize(l *zap.SugaredLogger) {
442442
// https://github.com/AdguardTeam/AdGuardHome/issues/6820
443443
if c.UsePrivatePtrResolvers != nil && *c.UsePrivatePtrResolvers &&
444444
(c.LocalPtrUpstreams == nil || len(*c.LocalPtrUpstreams) == 0) {
445-
l.Warn("disabling replica 'Use private reverse DNS resolvers' as no 'Private reverse DNS servers' are configured on origin")
445+
l.Warn(
446+
"disabling replica 'Use private reverse DNS resolvers' as no 'Private reverse DNS servers' are configured on origin",
447+
)
446448
c.UsePrivatePtrResolvers = utils.Ptr(false)
447449
}
448450
}

pkg/client/model/model_test.go

+12-3
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,10 @@ var _ = Describe("Types", func() {
242242
})
243243
It("should return 3 one replicas if urls are different", func() {
244244
cfg.Replica = &types.AdGuardInstance{URL: url, APIPath: apiPath}
245-
cfg.Replicas = []types.AdGuardInstance{{URL: url + "1", APIPath: apiPath}, {URL: url, APIPath: apiPath + "1"}}
245+
cfg.Replicas = []types.AdGuardInstance{
246+
{URL: url + "1", APIPath: apiPath},
247+
{URL: url, APIPath: apiPath + "1"},
248+
}
246249
r := cfg.UniqueReplicas()
247250
Ω(r).Should(HaveLen(3))
248251
})
@@ -311,8 +314,14 @@ var _ = Describe("Types", func() {
311314
cl2 *model.Client
312315
)
313316
BeforeEach(func() {
314-
cl1 = &model.Client{Name: utils.Ptr("foo"), BlockedServicesSchedule: &model.Schedule{TimeZone: utils.Ptr("UTC")}}
315-
cl2 = &model.Client{Name: utils.Ptr("foo"), BlockedServicesSchedule: &model.Schedule{TimeZone: utils.Ptr("Local")}}
317+
cl1 = &model.Client{
318+
Name: utils.Ptr("foo"),
319+
BlockedServicesSchedule: &model.Schedule{TimeZone: utils.Ptr("UTC")},
320+
}
321+
cl2 = &model.Client{
322+
Name: utils.Ptr("foo"),
323+
BlockedServicesSchedule: &model.Schedule{TimeZone: utils.Ptr("Local")},
324+
}
316325
})
317326

318327
It("should equal if only timezone differs on empty blocked service schedule", func() {

pkg/sync/action-general.go

+10-2
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,8 @@ var (
105105
return ac.client.SetCustomRules(ac.origin.filters.UserRules)
106106
}
107107

108-
if !utils.PtrEquals(ac.origin.filters.Enabled, rf.Enabled) || !utils.PtrEquals(ac.origin.filters.Interval, rf.Interval) {
108+
if !utils.PtrEquals(ac.origin.filters.Enabled, rf.Enabled) ||
109+
!utils.PtrEquals(ac.origin.filters.Interval, rf.Interval) {
109110
return ac.client.ToggleFiltering(*ac.origin.filters.Enabled, *ac.origin.filters.Interval)
110111
}
111112
return nil
@@ -236,7 +237,14 @@ var (
236237
}
237238
)
238239

239-
func syncFilterType(rl *zap.SugaredLogger, of *[]model.Filter, rFilters *[]model.Filter, whitelist bool, replica client.Client, continueOnError bool) error {
240+
func syncFilterType(
241+
rl *zap.SugaredLogger,
242+
of *[]model.Filter,
243+
rFilters *[]model.Filter,
244+
whitelist bool,
245+
replica client.Client,
246+
continueOnError bool,
247+
) error {
240248
fa, fu, fd := model.MergeFilters(rFilters, of)
241249

242250
for _, f := range fd {

pkg/sync/http.go

+17-8
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,23 @@ func (w *worker) handleRoot(c *gin.Context) {
4242
"Build": version.Build,
4343
"SyncStatus": w.status(),
4444
"Stats": map[string]interface{}{
45-
"Labels": getLast24Hours(),
46-
"DNS": dns,
47-
"Blocked": blocked,
48-
"BlockedPercentage": fmt.Sprintf("%.2f", (float64(*total.NumBlockedFiltering)*100.0)/float64(*total.NumDnsQueries)),
49-
"Malware": malware,
50-
"MalwarePercentage": fmt.Sprintf("%.2f", (float64(*total.NumReplacedSafebrowsing)*100.0)/float64(*total.NumDnsQueries)),
51-
"Adult": adult,
52-
"AdultPercentage": fmt.Sprintf("%.2f", (float64(*total.NumReplacedParental)*100.0)/float64(*total.NumDnsQueries)),
45+
"Labels": getLast24Hours(),
46+
"DNS": dns,
47+
"Blocked": blocked,
48+
"BlockedPercentage": fmt.Sprintf(
49+
"%.2f",
50+
(float64(*total.NumBlockedFiltering)*100.0)/float64(*total.NumDnsQueries),
51+
),
52+
"Malware": malware,
53+
"MalwarePercentage": fmt.Sprintf(
54+
"%.2f",
55+
(float64(*total.NumReplacedSafebrowsing)*100.0)/float64(*total.NumDnsQueries),
56+
),
57+
"Adult": adult,
58+
"AdultPercentage": fmt.Sprintf(
59+
"%.2f",
60+
(float64(*total.NumReplacedParental)*100.0)/float64(*total.NumDnsQueries),
61+
),
5362

5463
"TotalDNS": total.NumDnsQueries,
5564
"TotalBlocked": total.NumBlockedFiltering,

pkg/sync/stats.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,13 @@ func safeStats(stats *[]int) []int {
7272
return *stats
7373
}
7474

75-
func graphLines(t *model.Stats, s metrics.OverallStats, baseColor []int, altColors [][]int, dataCB func(s *model.Stats) []int) []line {
75+
func graphLines(
76+
t *model.Stats,
77+
s metrics.OverallStats,
78+
baseColor []int,
79+
altColors [][]int,
80+
dataCB func(s *model.Stats) []int,
81+
) []line {
7682
g := &graph{
7783
total: line{
7884
Fill: true,

pkg/sync/sync.go

+11-4
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,8 @@ func (w *worker) sync() {
172172
}
173173

174174
if versions.IsNewerThan(versions.MinAgh, o.status.Version) {
175-
sl.With("error", err, "version", o.status.Version).Errorf("Origin AdGuard Home version must be >= %s", versions.MinAgh)
175+
sl.With("error", err, "version", o.status.Version).
176+
Errorf("Origin AdGuard Home version must be >= %s", versions.MinAgh)
176177
return
177178
}
178179

@@ -280,12 +281,14 @@ func (w *worker) syncTo(l *zap.SugaredLogger, o *origin, replica types.AdGuardIn
280281
rl.With("version", replicaStatus.Version).Info("Connected to replica")
281282

282283
if versions.IsNewerThan(versions.MinAgh, replicaStatus.Version) {
283-
rl.With("error", err, "version", replicaStatus.Version).Errorf("Replica AdGuard Home version must be >= %s", versions.MinAgh)
284+
rl.With("error", err, "version", replicaStatus.Version).
285+
Errorf("Replica AdGuard Home version must be >= %s", versions.MinAgh)
284286
return
285287
}
286288

287289
if o.status.Version != replicaStatus.Version {
288-
rl.With("originVersion", o.status.Version, "replicaVersion", replicaStatus.Version).Warn("Versions do not match")
290+
rl.With("originVersion", o.status.Version, "replicaVersion", replicaStatus.Version).
291+
Warn("Versions do not match")
289292
}
290293

291294
ac := &actionContext{
@@ -308,7 +311,11 @@ func (w *worker) syncTo(l *zap.SugaredLogger, o *origin, replica types.AdGuardIn
308311
rl.Info("Sync done")
309312
}
310313

311-
func (w *worker) statusWithSetup(rl *zap.SugaredLogger, replica types.AdGuardInstance, rc client.Client) (*model.ServerStatus, error) {
314+
func (w *worker) statusWithSetup(
315+
rl *zap.SugaredLogger,
316+
replica types.AdGuardInstance,
317+
rc client.Client,
318+
) (*model.ServerStatus, error) {
312319
rs, err := rc.Status()
313320
if err != nil {
314321
if replica.AutoSetup && errors.Is(err, client.ErrSetupNeeded) {

0 commit comments

Comments
 (0)