# When adding new tidiers, consider what labels it should have. If it's fast, set labels like this:
#
#     labels = [ "default", "fast-tidy" ]
#
# "Fast" here means anything less than 0.1s. These tidiers are intended to be invoked frequently,
# for example from editors on save. Slower tidiers should only be invoked manually, either by
# running the relevant mage command or by explicitly running `precious` from the CLI.

exclude = [
  ".ai-plans/**/*",
  "vendor/**/*",
]

[commands.golangci-lint]
type = "both"
include = "**/*.go"
# Based on my local testing, this seemed like a reasonable value for this option.
invoke.per-dir-or-once = 7
working-dir = "root"
path-args = "dir"
cmd = [
    "$PRECIOUS_ROOT/dev-bin/golangci-lint",
    "run",
    "--allow-parallel-runners",
    "--build-tags", "failpoints",
    "--color", "always",
    "--timeout", "5m",
]
tidy-flags = "--fix"
env = { "FAIL_ON_WARNINGS" = "1" }
ok-exit-codes = [0]
lint-failure-exit-codes = [1]

[commands.goimports]
type = "tidy"
include = "**/*.go"
cmd = [ "$PRECIOUS_ROOT/dev-bin/goimports", "-w" ]
ok-exit-codes = [0]
labels = [ "default", "fast-tidy" ]

[commands.golines]
type = "both"
include = "**/*.go"
invoke  = "once"
working-dir = "root"
path-args = "file"
# This wrapper will tidy or lint based on the presence of the "--lint" flag.
cmd = "$PRECIOUS_ROOT/etc/golines-wrapper.sh"
lint-flags = "--lint"
ok-exit-codes = [0]
lint-failure-exit-codes = [1]

# We could run this via golangci-lint but we want to exclude more directories
# for gosec than we do for other linting. There doesn't seem to be a way to
# configure these excludes when running gosec as part of golangci-lint.
[commands.gosec]
type = "lint"
include = "**/*.go"
exclude = [
    "buildscript/**/*.go",
    "common/testutil/**/*.go",
    "release/**/*.go",
    "vendor/**/*.go",
]
invoke.per-dir-or-once = 7
working-dir = "root"
path-args = "dir"
# This wrapper lets us control the output with an env var so we can generate a
# SARIF report in Evergreen but use the default format for linting.
cmd = [
    "$PRECIOUS_ROOT/etc/gosec-wrapper.sh",
    # This rule complains about reading or writing to paths based on user input,
    # but most of the tools exist for the purpose of reading and writing from/to
    # user-provided paths.
    "-exclude", "G304",
    "-severity", "high",
    "-terse",
]
ok_exit_codes = [0]
lint_failure_exit_codes = [1]

[commands.prettier-yaml]
type = "both"
invoke = "once"
working-dir = "root"
path-args = "file"
include = [ "**/*.yml", "**/*.yaml" ]
cmd = "$PRECIOUS_ROOT/node_modules/.bin/prettier"
lint-flags = "--check"
tidy-flags = "--write"
ok-exit-codes = [0]
lint-failure-exit-codes = [1]
ignore-stderr = ".*"
labels = [ "default", "fast-tidy" ]

[commands.prettier-md]
type = "both"
invoke = "once"
working-dir = "root"
path-args = "file"
include = [ "*.md" ]
exclude = [ "docs/superpowers/**/*.md" ]
cmd = [
    "$PRECIOUS_ROOT/node_modules/.bin/prettier",
    "--print-width", "100",
    "--prose-wrap", "always",
]
lint-flags = "--check"
tidy-flags = "--write"
ok-exit-codes = [0]
lint-failure-exit-codes = [1]
ignore-stderr = ".*"
labels = [ "default", "fast-tidy" ]

# It'd be nice to use prettier to tidy the JS code too, but it seems to make
# changes that cause it to fail to parse with the versions of the Mongo shell
# we use.

[commands.eslint]
type = "both"
invoke = "once"
working-dir = "root"
path-args = "file"
include = "test/qa-tests/jstests/**/*.js"
cmd = "$PRECIOUS_ROOT/node_modules/.bin/eslint"
tidy-flags = "--fix"
ok-exit-codes = [0]
lint-failure-exit-codes = [1]

[commands.github-codeowners]
type = "lint"
invoke = "once"
path-args = "none"
# We need to run this whenever new files are added to make sure they have an owner, so it's easiest
# to just apply this linter to all files.
include = "**/*"
cmd = [ "$PRECIOUS_ROOT/etc/github-codeowners-wrapper.sh" ]
ok_exit_codes = 0
lint_failure_exit_codes = 1
labels = [ "default", "fast-tidy" ]
