diff --git a/.golangci.yml b/.golangci.yml index bd81d919b..a046a3fcb 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -8,7 +8,7 @@ linters-settings: default-signifies-exhaustive: true forbidigo: forbid: - - 'fmt\.Print.*' # Prevent debug logging + - 'fmt\.Print.*(# Avoid debug logging)?' gci: local-prefixes: github.com/obolnetwork/charon gocritic: diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 8478d957a..331c2e94b 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -59,3 +59,9 @@ repos: language: script entry: .pre-commit/run_buf.sh types: [ file, proto ] + - id: run-regexp + name: run-regexp + language: script + entry: .pre-commit/run_regexp.sh + types: [ file, go ] + exclude: "_test.go" diff --git a/.pre-commit/run_regexp.sh b/.pre-commit/run_regexp.sh new file mode 100755 index 000000000..e30d808dd --- /dev/null +++ b/.pre-commit/run_regexp.sh @@ -0,0 +1,12 @@ +#!/usr/bin/env bash + +FILES=$@ + +function check() { + grep -HnE "$2" $FILES && printf "\n❌ Regexp check failed: %s\n\n" "$1" +} + +check 'Log messages must be capitalised' 'log\.(Error|Warn|Info|Debug)\(ctx, "[[:lower:]]' && exit 1 +check 'Error messages must not be capitalised' 'errors\.(New|Wrap)\((err, )?"[[:upper:]]' && exit 1 + +true diff --git a/cmd/enr.go b/cmd/enr.go index fb34c767e..8ff46dd70 100644 --- a/cmd/enr.go +++ b/cmd/enr.go @@ -53,7 +53,7 @@ func newEnrCmd(runFunc func(io.Writer, p2p.Config, string) error) *cobra.Command func runNewENR(w io.Writer, config p2p.Config, dataDir string) error { key, err := p2p.LoadPrivKey(dataDir) if errors.Is(err, fs.ErrNotExist) { - return errors.New("ENR private key not found. If this is your first time running this client, create one with `charon create enr`.", z.Str("enr_path", p2p.KeyPath(dataDir))) //nolint:revive + return errors.New("private key not found. If this is your first time running this client, create one with `charon create enr`.", z.Str("enr_path", p2p.KeyPath(dataDir))) //nolint:revive } else if err != nil { return err } diff --git a/cmd/enr_internal_test.go b/cmd/enr_internal_test.go index 3227a0a39..edd45ac61 100644 --- a/cmd/enr_internal_test.go +++ b/cmd/enr_internal_test.go @@ -32,6 +32,6 @@ func TestRunNewEnr(t *testing.T) { require.NoError(t, err) got := runNewENR(io.Discard, p2p.Config{}, temp) - expected := errors.New("ENR private key not found. If this is your first time running this client, create one with `charon create enr`.", z.Str("enr_path", p2p.KeyPath(temp))) + expected := errors.New("private key not found. If this is your first time running this client, create one with `charon create enr`.", z.Str("enr_path", p2p.KeyPath(temp))) require.Equal(t, expected.Error(), got.Error()) } diff --git a/core/parsigex/parsigex.go b/core/parsigex/parsigex.go index 677c65864..53180a7fa 100644 --- a/core/parsigex/parsigex.go +++ b/core/parsigex/parsigex.go @@ -66,13 +66,13 @@ func (m *ParSigEx) handle(s network.Stream) { b, err := io.ReadAll(s) if err != nil { - log.Error(ctx, "read proto bytes", err) + log.Error(ctx, "Read proto bytes", err) return } var pb pbv1.ParSigExMsg if err := proto.Unmarshal(b, &pb); err != nil { - log.Error(ctx, "unmarshal parsigex proto", err) + log.Error(ctx, "Unmarshal parsigex proto", err) return } @@ -87,7 +87,7 @@ func (m *ParSigEx) handle(s network.Stream) { for _, sub := range m.subs { err := sub(ctx, duty, set) if err != nil { - log.Error(ctx, "subscribe error", err) + log.Error(ctx, "Subscribe error", err) } } } diff --git a/core/scheduler/scheduler.go b/core/scheduler/scheduler.go index 9d551e5b2..c8f0abe00 100644 --- a/core/scheduler/scheduler.go +++ b/core/scheduler/scheduler.go @@ -519,7 +519,7 @@ func waitChainStart(ctx context.Context, eth2Cl eth2Provider, clock clockwork.Cl for { genesis, err := eth2Cl.GenesisTime(ctx) if err != nil { - log.Error(ctx, "failure getting genesis time", err) + log.Error(ctx, "Failure getting genesis time", err) clock.Sleep(time.Second * 5) // TODO(corver): Improve backoff continue @@ -544,7 +544,7 @@ func waitBeaconSync(ctx context.Context, eth2Cl eth2Provider, clock clockwork.Cl for { state, err := eth2Cl.NodeSyncing(ctx) if err != nil { - log.Error(ctx, "failure getting sync state", err) + log.Error(ctx, "Failure getting sync state", err) clock.Sleep(time.Second * 5) // TODO(corver): Improve backoff continue diff --git a/testutil/beaconmock/options.go b/testutil/beaconmock/options.go index 44e4fe022..c13b9b667 100644 --- a/testutil/beaconmock/options.go +++ b/testutil/beaconmock/options.go @@ -127,7 +127,7 @@ func WithValidatorSet(set ValidatorSet) Option { if ok { resp[val.Index] = cloneValidator(val) } else { - log.Debug(ctx, "pubkey not found") + log.Debug(ctx, "Pubkey not found") } } @@ -141,7 +141,7 @@ func WithValidatorSet(set ValidatorSet) Option { if ok { resp[index] = cloneValidator(val) } else { - log.Debug(ctx, "index not found") + log.Debug(ctx, "Index not found") } } diff --git a/testutil/compose/compose/main.go b/testutil/compose/compose/main.go index cc0c7702f..cc24dbf2f 100644 --- a/testutil/compose/compose/main.go +++ b/testutil/compose/compose/main.go @@ -214,7 +214,7 @@ func newAutoCmd(tmplCallback func(data *compose.TmplData)) *cobra.Command { fail = true } if fail { - return errors.New("Alerts detected") + return errors.New("alerts detected") } return nil @@ -236,22 +236,6 @@ func printDockerCompose(ctx context.Context, dir string) error { return errors.Wrap(err, "exec cat docker-compose.yml") } - return ls(ctx, dir) -} - -// printDockerCompose prints the docker-compose.yml file to stdout. -func ls(ctx context.Context, dir string) error { - log.Info(ctx, "ls dir") - cmd := exec.CommandContext(ctx, "ls", "-la") - cmd.Dir = dir - cmd.Stdout = os.Stdout - cmd.Stderr = os.Stderr - - err := cmd.Run() - if err != nil { - return errors.Wrap(err, "exec ls") - } - return nil } diff --git a/testutil/validatormock/validatormock.go b/testutil/validatormock/validatormock.go index 3ef21b6ae..c97b721b1 100644 --- a/testutil/validatormock/validatormock.go +++ b/testutil/validatormock/validatormock.go @@ -372,7 +372,7 @@ func getBlock(endpoint string, base string) (io.Reader, error) { statusFamily := res.StatusCode / 100 if statusFamily != 2 { - return nil, errors.New("GET failed", z.Int("status", res.StatusCode), z.Str("data", string(data))) + return nil, errors.New("get failed", z.Int("status", res.StatusCode), z.Str("data", string(data))) } return bytes.NewReader(data), nil