codeveto 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- codeveto/__init__.py +3 -0
- codeveto/__main__.py +8 -0
- codeveto/ai/__init__.py +19 -0
- codeveto/ai/harness.py +303 -0
- codeveto/ai/packets.py +855 -0
- codeveto/ai/policy.py +222 -0
- codeveto/ai/prompt.py +101 -0
- codeveto/ai/providers/__init__.py +178 -0
- codeveto/ai/providers/claude.py +247 -0
- codeveto/ai/providers/openai_compat.py +246 -0
- codeveto/ai/runner.py +293 -0
- codeveto/ai/verify.py +324 -0
- codeveto/anchor.py +211 -0
- codeveto/cli.py +247 -0
- codeveto/cli_explain.py +49 -0
- codeveto/cli_hook.py +31 -0
- codeveto/cli_init.py +254 -0
- codeveto/cli_mcp.py +29 -0
- codeveto/cli_packets.py +35 -0
- codeveto/cli_publish.py +29 -0
- codeveto/cli_review.py +362 -0
- codeveto/cli_rules.py +78 -0
- codeveto/cli_verify.py +47 -0
- codeveto/config.py +1229 -0
- codeveto/data/README.md +16 -0
- codeveto/data/ai-wire.schema.json +53 -0
- codeveto/data/prompts/system.md +57 -0
- codeveto/data/report-v1.schema.json +268 -0
- codeveto/data/templates/forgejo-codeveto.yml +104 -0
- codeveto/data/templates/github-codeveto.yml +18 -0
- codeveto/embedded.py +311 -0
- codeveto/engine.py +1570 -0
- codeveto/git.py +1549 -0
- codeveto/globs.py +348 -0
- codeveto/integrations/__init__.py +1 -0
- codeveto/integrations/hooks.py +327 -0
- codeveto/integrations/mcp.py +770 -0
- codeveto/integrations/mcp_ai.py +77 -0
- codeveto/integrations/prepush.py +222 -0
- codeveto/jsonsafe.py +60 -0
- codeveto/languages.py +539 -0
- codeveto/lexers.py +2560 -0
- codeveto/model.py +372 -0
- codeveto/publish/__init__.py +28 -0
- codeveto/publish/action.py +330 -0
- codeveto/publish/common.py +84 -0
- codeveto/publish/diff.py +208 -0
- codeveto/publish/event.py +241 -0
- codeveto/publish/forgejo.py +184 -0
- codeveto/publish/github.py +380 -0
- codeveto/publish/http.py +263 -0
- codeveto/publish/pull.py +218 -0
- codeveto/publish/untrusted.py +110 -0
- codeveto/py.typed +0 -0
- codeveto/redact.py +113 -0
- codeveto/render/__init__.py +64 -0
- codeveto/render/agent.py +43 -0
- codeveto/render/github.py +101 -0
- codeveto/render/json.py +23 -0
- codeveto/render/markdown.py +208 -0
- codeveto/render/safe.py +249 -0
- codeveto/render/sarif.py +217 -0
- codeveto/render/text.py +210 -0
- codeveto/report.py +252 -0
- codeveto/rules/__init__.py +84 -0
- codeveto/rules/agent.py +2972 -0
- codeveto/rules/base.py +1026 -0
- codeveto/rules/ci.py +1708 -0
- codeveto/rules/config.py +493 -0
- codeveto/rules/deps.py +1693 -0
- codeveto/rules/docker.py +479 -0
- codeveto/rules/examples/__init__.py +148 -0
- codeveto/rules/examples/agent.py +839 -0
- codeveto/rules/examples/ci.py +1046 -0
- codeveto/rules/examples/config.py +191 -0
- codeveto/rules/examples/deps.py +369 -0
- codeveto/rules/examples/docker.py +157 -0
- codeveto/rules/examples/go.py +241 -0
- codeveto/rules/examples/hygiene.py +466 -0
- codeveto/rules/examples/js.py +462 -0
- codeveto/rules/examples/python.py +595 -0
- codeveto/rules/examples/rust.py +268 -0
- codeveto/rules/examples/secrets.py +561 -0
- codeveto/rules/examples/security.py +608 -0
- codeveto/rules/examples/shell.py +174 -0
- codeveto/rules/examples/sql.py +194 -0
- codeveto/rules/examples/tests.py +497 -0
- codeveto/rules/go.py +328 -0
- codeveto/rules/hygiene.py +1094 -0
- codeveto/rules/js.py +768 -0
- codeveto/rules/python.py +1642 -0
- codeveto/rules/rust.py +209 -0
- codeveto/rules/secrets.py +1335 -0
- codeveto/rules/security.py +3680 -0
- codeveto/rules/shell.py +867 -0
- codeveto/rules/sql.py +629 -0
- codeveto/rules/tests.py +1404 -0
- codeveto/schema.py +384 -0
- codeveto/statements.py +607 -0
- codeveto/worktree.py +1176 -0
- codeveto/yamlish.py +1179 -0
- codeveto-0.1.0.dist-info/METADATA +253 -0
- codeveto-0.1.0.dist-info/RECORD +106 -0
- codeveto-0.1.0.dist-info/WHEEL +4 -0
- codeveto-0.1.0.dist-info/entry_points.txt +2 -0
- codeveto-0.1.0.dist-info/licenses/LICENSE +21 -0
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
"""Examples for the docker pack."""
|
|
2
|
+
|
|
3
|
+
from codeveto.rules.base import bad, good
|
|
4
|
+
|
|
5
|
+
# Secret-shaped values are assembled from fragments, never written whole.
|
|
6
|
+
DB_PASSWORD = "hunter" + "2-" + "prod-7731"
|
|
7
|
+
NPM_TOKEN = "npm" + "_" + "k3Yv9TqZ" + "x81LmWq0"
|
|
8
|
+
CHECKSUM = "sha256:" + "24454f830cdb571e2c4ad15481119c43b3cafd48dd869a9b2945d1036d1dc68d"
|
|
9
|
+
|
|
10
|
+
EXAMPLES = {
|
|
11
|
+
"docker/secret-in-env": [
|
|
12
|
+
bad(
|
|
13
|
+
"Dockerfile",
|
|
14
|
+
"FROM node:22.11-slim\n"
|
|
15
|
+
f"ARG NPM_TOKEN={NPM_TOKEN} # expect\n"
|
|
16
|
+
"RUN npm ci\n"
|
|
17
|
+
"USER node\n",
|
|
18
|
+
note="ARG defaults are kept in the image history.",
|
|
19
|
+
),
|
|
20
|
+
bad(
|
|
21
|
+
"deploy/Containerfile",
|
|
22
|
+
"FROM postgres:17.2\n"
|
|
23
|
+
"ENV POSTGRES_DB=app \\\n"
|
|
24
|
+
f' POSTGRES_PASSWORD="{DB_PASSWORD}" # expect\n'
|
|
25
|
+
"ENV DJANGO_SECRET_KEY " + "dj" + "-insecure-" + "9f2kq" + " # expect\n"
|
|
26
|
+
"USER postgres\n",
|
|
27
|
+
note="Both ENV forms, inside a continuation and the legacy space form.",
|
|
28
|
+
),
|
|
29
|
+
good(
|
|
30
|
+
"Dockerfile",
|
|
31
|
+
"FROM node:22.11-slim\n"
|
|
32
|
+
"ARG NPM_TOKEN\n"
|
|
33
|
+
"ENV API_KEY=${API_KEY} DB_PASSWORD=\n"
|
|
34
|
+
"ENV DB_PASSWORD_FILE=/run/secrets/db PASSWORD_MIN_LENGTH=12\n"
|
|
35
|
+
"ENV GPG_KEY=A035C8C19219BA821ECEA86B64E628F8D684696D\n"
|
|
36
|
+
"RUN --mount=type=secret,id=npm npm ci\n"
|
|
37
|
+
"USER node\n",
|
|
38
|
+
note="No default, references, empty values, *_FILE paths, a non-secret name and "
|
|
39
|
+
"a public key fingerprint (GPG_KEY) are fine.",
|
|
40
|
+
),
|
|
41
|
+
good(
|
|
42
|
+
"docker/dev.Dockerfile",
|
|
43
|
+
"FROM postgres:17.2\n"
|
|
44
|
+
"ENV POSTGRES_PASSWORD=changeme ADMIN_TOKEN=<set-at-runtime>\n"
|
|
45
|
+
"ENV AUTH_SECRET=${AUTH_SECRET:-}\n"
|
|
46
|
+
"USER postgres\n",
|
|
47
|
+
note="Obvious placeholders are not secrets.",
|
|
48
|
+
),
|
|
49
|
+
],
|
|
50
|
+
"docker/latest-tag": [
|
|
51
|
+
bad(
|
|
52
|
+
"Dockerfile",
|
|
53
|
+
"FROM golang AS build\n"
|
|
54
|
+
"RUN go build -o /out/app ./cmd/app\n"
|
|
55
|
+
"FROM alpine:latest\n"
|
|
56
|
+
"COPY --from=build /out/app /app\n"
|
|
57
|
+
"USER nobody\n",
|
|
58
|
+
expect=[("Dockerfile", 1, "new"), ("Dockerfile", 3, "new")],
|
|
59
|
+
note="No tag means :latest; both stages float.",
|
|
60
|
+
),
|
|
61
|
+
bad(
|
|
62
|
+
"Containerfile",
|
|
63
|
+
"FROM --platform=$BUILDPLATFORM registry.example.com:5000/team/base\n"
|
|
64
|
+
"USER app\n",
|
|
65
|
+
expect=[("Containerfile", 1, "new")],
|
|
66
|
+
note="A registry port is not a tag.",
|
|
67
|
+
),
|
|
68
|
+
good(
|
|
69
|
+
"Dockerfile",
|
|
70
|
+
"ARG BASE=python:3.13-slim\n"
|
|
71
|
+
"FROM golang:1.23.4 AS build\n"
|
|
72
|
+
"FROM ${BASE} AS runtime\n"
|
|
73
|
+
"FROM build AS test\n"
|
|
74
|
+
"FROM scratch\n"
|
|
75
|
+
"COPY --from=build /out/app /app\n"
|
|
76
|
+
"USER 65532\n",
|
|
77
|
+
note="Version tags, ARG-built names, earlier stages and scratch.",
|
|
78
|
+
),
|
|
79
|
+
good(
|
|
80
|
+
"Dockerfile",
|
|
81
|
+
"FROM alpine@sha256:"
|
|
82
|
+
+ "c5b1261d6d3e43071626931fc004f70149baeba2c8ec672bd4f27761f8e1ad6b"
|
|
83
|
+
+ "\n"
|
|
84
|
+
"USER nobody\n",
|
|
85
|
+
note="A digest pins the image even without a tag.",
|
|
86
|
+
),
|
|
87
|
+
],
|
|
88
|
+
"docker/root-user": [
|
|
89
|
+
bad(
|
|
90
|
+
"Dockerfile",
|
|
91
|
+
"FROM python:3.13-slim\n"
|
|
92
|
+
"COPY . /app\n"
|
|
93
|
+
'CMD ["python", "/app/main.py"]\n',
|
|
94
|
+
expect=[("Dockerfile", 1, "new")],
|
|
95
|
+
note="A new Dockerfile without USER runs its process as root.",
|
|
96
|
+
),
|
|
97
|
+
bad(
|
|
98
|
+
"Dockerfile",
|
|
99
|
+
"FROM python:3.13-slim\n"
|
|
100
|
+
"RUN useradd --system app\n"
|
|
101
|
+
"USER root # expect\n"
|
|
102
|
+
'CMD ["python", "-m", "app"]\n',
|
|
103
|
+
old="FROM python:3.13-slim\n"
|
|
104
|
+
"RUN useradd --system app\n"
|
|
105
|
+
"USER app\n"
|
|
106
|
+
'CMD ["python", "-m", "app"]\n',
|
|
107
|
+
note="Switching back to root in the final stage.",
|
|
108
|
+
),
|
|
109
|
+
good(
|
|
110
|
+
"Dockerfile",
|
|
111
|
+
"FROM golang:1.23.4 AS build\n"
|
|
112
|
+
"RUN go build -o /out/app ./cmd/app\n"
|
|
113
|
+
"FROM gcr.io/distroless/static-debian12:nonroot\n"
|
|
114
|
+
"COPY --from=build /out/app /app\n"
|
|
115
|
+
'ENTRYPOINT ["/app"]\n',
|
|
116
|
+
note="Only the final stage counts, and a nonroot base image runs unprivileged.",
|
|
117
|
+
),
|
|
118
|
+
good(
|
|
119
|
+
"Dockerfile",
|
|
120
|
+
"FROM python:3.13-slim\n"
|
|
121
|
+
'CMD ["python", "-m", "app", "--debug"]\n',
|
|
122
|
+
old="FROM python:3.13-slim\n" 'CMD ["python", "-m", "app"]\n',
|
|
123
|
+
note="It already ran as root before the change: existing debt is not reported.",
|
|
124
|
+
),
|
|
125
|
+
],
|
|
126
|
+
"docker/add-remote-url": [
|
|
127
|
+
bad(
|
|
128
|
+
"Dockerfile",
|
|
129
|
+
"FROM debian:12.5\n"
|
|
130
|
+
"ADD https://example.com/tool-1.2.tar.gz /opt/ # expect\n"
|
|
131
|
+
"USER nobody\n",
|
|
132
|
+
),
|
|
133
|
+
bad(
|
|
134
|
+
"Dockerfile",
|
|
135
|
+
"FROM debian:12.5\n"
|
|
136
|
+
'ADD --chown=app:app ["http://example.com/config.json", "/etc/app/"] # expect\n'
|
|
137
|
+
"USER nobody\n",
|
|
138
|
+
note="JSON form and other flags.",
|
|
139
|
+
),
|
|
140
|
+
good(
|
|
141
|
+
"Dockerfile",
|
|
142
|
+
"FROM debian:12.5\n"
|
|
143
|
+
f"ADD --checksum={CHECKSUM} https://example.com/tool-1.2.tar.gz /opt/\n"
|
|
144
|
+
"COPY https-proxy.conf /etc/\n"
|
|
145
|
+
"ADD vendor.tar.gz /opt/vendor/\n"
|
|
146
|
+
"USER nobody\n",
|
|
147
|
+
note="A checksum pins the content; local files are fine.",
|
|
148
|
+
),
|
|
149
|
+
good(
|
|
150
|
+
"Dockerfile",
|
|
151
|
+
"FROM debian:12.5\n"
|
|
152
|
+
"RUN curl -fsSLo /tmp/tool.tgz https://example.com/tool.tgz \\\n"
|
|
153
|
+
' && echo "${TOOL_SHA256} /tmp/tool.tgz" | sha256sum -c -\n'
|
|
154
|
+
"USER nobody\n",
|
|
155
|
+
),
|
|
156
|
+
],
|
|
157
|
+
}
|
|
@@ -0,0 +1,241 @@
|
|
|
1
|
+
"""Examples for the go pack."""
|
|
2
|
+
|
|
3
|
+
from codeveto.rules.base import bad, good
|
|
4
|
+
|
|
5
|
+
EXAMPLES = {
|
|
6
|
+
"go/defer-in-loop": [
|
|
7
|
+
bad(
|
|
8
|
+
"internal/archive/merge.go",
|
|
9
|
+
"package archive\n"
|
|
10
|
+
"\n"
|
|
11
|
+
'import "os"\n'
|
|
12
|
+
"\n"
|
|
13
|
+
"func Merge(paths []string) error {\n"
|
|
14
|
+
"\tfor _, p := range paths {\n"
|
|
15
|
+
"\t\tf, err := os.Open(p)\n"
|
|
16
|
+
"\t\tif err != nil {\n"
|
|
17
|
+
"\t\t\treturn err\n"
|
|
18
|
+
"\t\t}\n"
|
|
19
|
+
"\t\tdefer f.Close() // expect\n"
|
|
20
|
+
"\t}\n"
|
|
21
|
+
"\treturn nil\n"
|
|
22
|
+
"}\n",
|
|
23
|
+
note="Every file stays open until Merge returns, not until the iteration ends.",
|
|
24
|
+
),
|
|
25
|
+
bad(
|
|
26
|
+
"internal/queue/worker.go",
|
|
27
|
+
"package queue\n"
|
|
28
|
+
"\n"
|
|
29
|
+
"func (w *Worker) Run() {\n"
|
|
30
|
+
"outer:\n"
|
|
31
|
+
"\tfor {\n"
|
|
32
|
+
"\t\tselect {\n"
|
|
33
|
+
"\t\tcase job := <-w.jobs:\n"
|
|
34
|
+
"\t\t\tw.mu.Lock()\n"
|
|
35
|
+
"\t\t\tdefer w.mu.Unlock() // expect\n"
|
|
36
|
+
"\t\t\tw.handle(job)\n"
|
|
37
|
+
"\t\tcase <-w.done:\n"
|
|
38
|
+
"\t\t\tbreak outer\n"
|
|
39
|
+
"\t\t}\n"
|
|
40
|
+
"\t}\n"
|
|
41
|
+
"}\n",
|
|
42
|
+
note="A defer inside select inside a loop still waits for the function to return.",
|
|
43
|
+
),
|
|
44
|
+
good(
|
|
45
|
+
"internal/archive/merge.go",
|
|
46
|
+
"package archive\n"
|
|
47
|
+
"\n"
|
|
48
|
+
'import "os"\n'
|
|
49
|
+
"\n"
|
|
50
|
+
"func Merge(paths []string) error {\n"
|
|
51
|
+
"\tfor _, p := range paths {\n"
|
|
52
|
+
"\t\tif err := func() error {\n"
|
|
53
|
+
"\t\t\tf, err := os.Open(p)\n"
|
|
54
|
+
"\t\t\tif err != nil {\n"
|
|
55
|
+
"\t\t\t\treturn err\n"
|
|
56
|
+
"\t\t\t}\n"
|
|
57
|
+
"\t\t\tdefer f.Close()\n"
|
|
58
|
+
"\t\t\treturn nil\n"
|
|
59
|
+
"\t\t}(); err != nil {\n"
|
|
60
|
+
"\t\t\treturn err\n"
|
|
61
|
+
"\t\t}\n"
|
|
62
|
+
"\t}\n"
|
|
63
|
+
"\treturn nil\n"
|
|
64
|
+
"}\n",
|
|
65
|
+
note="The usual fix: a function literal per iteration, so the defer runs each time.",
|
|
66
|
+
),
|
|
67
|
+
good(
|
|
68
|
+
"internal/store/store.go",
|
|
69
|
+
"package store\n"
|
|
70
|
+
"\n"
|
|
71
|
+
"// Close releases the lock; do not defer inside a for loop.\n"
|
|
72
|
+
"func (s *Store) Close() {\n"
|
|
73
|
+
"\ts.mu.Lock()\n"
|
|
74
|
+
"\tdefer s.mu.Unlock()\n"
|
|
75
|
+
'\tmsg := "for { defer x() }"\n'
|
|
76
|
+
"\tfor _, c := range s.conns {\n"
|
|
77
|
+
"\t\tc.Close()\n"
|
|
78
|
+
"\t}\n"
|
|
79
|
+
"\t_ = msg\n"
|
|
80
|
+
"}\n",
|
|
81
|
+
note="A defer before the loop, and the words in a comment or string, are fine.",
|
|
82
|
+
),
|
|
83
|
+
],
|
|
84
|
+
"go/panic": [
|
|
85
|
+
bad(
|
|
86
|
+
"pkg/config/config.go",
|
|
87
|
+
"package config\n"
|
|
88
|
+
"\n"
|
|
89
|
+
"func Port(env map[string]string) int {\n"
|
|
90
|
+
'\tv, ok := env["PORT"]\n'
|
|
91
|
+
"\tif !ok {\n"
|
|
92
|
+
'\t\tpanic("PORT not set") // expect\n'
|
|
93
|
+
"\t}\n"
|
|
94
|
+
"\treturn parse(v)\n"
|
|
95
|
+
"}\n",
|
|
96
|
+
),
|
|
97
|
+
bad(
|
|
98
|
+
"pkg/cache/cache.go",
|
|
99
|
+
"package cache\n"
|
|
100
|
+
"\n"
|
|
101
|
+
"func (c *Cache) Start() {\n"
|
|
102
|
+
"\tgo func() {\n"
|
|
103
|
+
"\t\tif err := c.loop(); err != nil {\n"
|
|
104
|
+
"\t\t\tpanic(err) // expect\n"
|
|
105
|
+
"\t\t}\n"
|
|
106
|
+
"\t}()\n"
|
|
107
|
+
"}\n",
|
|
108
|
+
note="A panic in a goroutine of a library crashes the whole program.",
|
|
109
|
+
),
|
|
110
|
+
good(
|
|
111
|
+
"cmd/server/main.go",
|
|
112
|
+
"package main\n"
|
|
113
|
+
"\n"
|
|
114
|
+
"func main() {\n"
|
|
115
|
+
"\tcfg, err := load()\n"
|
|
116
|
+
"\tif err != nil {\n"
|
|
117
|
+
"\t\tpanic(err)\n"
|
|
118
|
+
"\t}\n"
|
|
119
|
+
"\trun(cfg)\n"
|
|
120
|
+
"}\n",
|
|
121
|
+
note="A program may stop itself: package main is not reported.",
|
|
122
|
+
),
|
|
123
|
+
good(
|
|
124
|
+
"pkg/route/route.go",
|
|
125
|
+
"package route\n"
|
|
126
|
+
"\n"
|
|
127
|
+
'import "regexp"\n'
|
|
128
|
+
"\n"
|
|
129
|
+
"func MustCompile(pattern string) *Route {\n"
|
|
130
|
+
"\tr, err := Compile(pattern)\n"
|
|
131
|
+
"\tif err != nil {\n"
|
|
132
|
+
"\t\tpanic(err)\n"
|
|
133
|
+
"\t}\n"
|
|
134
|
+
"\treturn r\n"
|
|
135
|
+
"}\n"
|
|
136
|
+
"\n"
|
|
137
|
+
"func init() {\n"
|
|
138
|
+
'\tif regexp.QuoteMeta("a") != "a" {\n'
|
|
139
|
+
'\t\tpanic("broken regexp package")\n'
|
|
140
|
+
"\t}\n"
|
|
141
|
+
"}\n",
|
|
142
|
+
note="Must... functions panic by contract, and init has no caller to return to.",
|
|
143
|
+
),
|
|
144
|
+
],
|
|
145
|
+
"go/ignored-error": [
|
|
146
|
+
bad(
|
|
147
|
+
"pkg/report/write.go",
|
|
148
|
+
"package report\n"
|
|
149
|
+
"\n"
|
|
150
|
+
"func Save(path string, data []byte) {\n"
|
|
151
|
+
"\t_ = os.WriteFile(path, data, 0o644) // expect\n"
|
|
152
|
+
"}\n",
|
|
153
|
+
),
|
|
154
|
+
bad(
|
|
155
|
+
"pkg/limits/limits.go",
|
|
156
|
+
"package limits\n"
|
|
157
|
+
"\n"
|
|
158
|
+
"func FromEnv() int {\n"
|
|
159
|
+
'\tn, _ := strconv.Atoi(os.Getenv("MAX_CONNS")) // expect\n'
|
|
160
|
+
"\treturn n\n"
|
|
161
|
+
"}\n",
|
|
162
|
+
note="A malformed value silently becomes 0.",
|
|
163
|
+
),
|
|
164
|
+
good(
|
|
165
|
+
"pkg/limits/limits.go",
|
|
166
|
+
"package limits\n"
|
|
167
|
+
"\n"
|
|
168
|
+
"func FromEnv(cache *sync.Map, m map[string]int, x any) int {\n"
|
|
169
|
+
'\tv, _ := m["max"]\n'
|
|
170
|
+
"\ts, _ := x.(string)\n"
|
|
171
|
+
'\tif raw, ok := os.LookupEnv("MAX"); ok {\n'
|
|
172
|
+
"\t\t_ = raw\n"
|
|
173
|
+
"\t}\n"
|
|
174
|
+
'\tcached, _ := cache.Load("max")\n'
|
|
175
|
+
'\tbefore, _, _ := strings.Cut(s, "=")\n'
|
|
176
|
+
"\t_, _ = cached, before\n"
|
|
177
|
+
"\treturn v\n"
|
|
178
|
+
"}\n",
|
|
179
|
+
note="Map lookups, type assertions, LookupEnv, sync.Map.Load and strings.Cut "
|
|
180
|
+
"drop a bool, not an error.",
|
|
181
|
+
),
|
|
182
|
+
good(
|
|
183
|
+
"pkg/limits/limits.go",
|
|
184
|
+
"package limits\n"
|
|
185
|
+
"\n"
|
|
186
|
+
"func FromEnv() (int, error) {\n"
|
|
187
|
+
'\tn, err := strconv.Atoi(os.Getenv("MAX_CONNS"))\n'
|
|
188
|
+
"\tif err != nil {\n"
|
|
189
|
+
"\t\treturn 0, err\n"
|
|
190
|
+
"\t}\n"
|
|
191
|
+
'\t// _ = os.Remove("x") is wrong here\n'
|
|
192
|
+
"\treturn n, nil\n"
|
|
193
|
+
"}\n",
|
|
194
|
+
),
|
|
195
|
+
],
|
|
196
|
+
"go/print-debug": [
|
|
197
|
+
bad(
|
|
198
|
+
"pkg/billing/invoice.go",
|
|
199
|
+
"package billing\n"
|
|
200
|
+
"\n"
|
|
201
|
+
"func Total(items []Item) int {\n"
|
|
202
|
+
"\tsum := 0\n"
|
|
203
|
+
"\tfor _, it := range items {\n"
|
|
204
|
+
'\t\tfmt.Printf("item %+v\\n", it) // expect\n'
|
|
205
|
+
"\t\tsum += it.Price\n"
|
|
206
|
+
"\t}\n"
|
|
207
|
+
"\treturn sum\n"
|
|
208
|
+
"}\n",
|
|
209
|
+
),
|
|
210
|
+
bad(
|
|
211
|
+
"pkg/billing/tax.go",
|
|
212
|
+
"package billing\n"
|
|
213
|
+
"\n"
|
|
214
|
+
"func Tax(total int) int {\n"
|
|
215
|
+
'\tprintln("tax for", total) // expect\n'
|
|
216
|
+
"\treturn total / 5\n"
|
|
217
|
+
"}\n",
|
|
218
|
+
note="The println builtin writes to stderr and is meant for debugging only.",
|
|
219
|
+
),
|
|
220
|
+
good(
|
|
221
|
+
"cmd/report/main.go",
|
|
222
|
+
"package main\n"
|
|
223
|
+
"\n"
|
|
224
|
+
"func main() {\n"
|
|
225
|
+
'\tfmt.Println("report written")\n'
|
|
226
|
+
"}\n",
|
|
227
|
+
note="Printing is a program's output in package main.",
|
|
228
|
+
),
|
|
229
|
+
good(
|
|
230
|
+
"pkg/billing/render.go",
|
|
231
|
+
"package billing\n"
|
|
232
|
+
"\n"
|
|
233
|
+
"func Render(w io.Writer, total int) error {\n"
|
|
234
|
+
'\t_, err := fmt.Fprintf(w, "total: %d\\n", total)\n'
|
|
235
|
+
'\tlog.Printf("rendered %d", total)\n'
|
|
236
|
+
"\treturn err\n"
|
|
237
|
+
"}\n",
|
|
238
|
+
note="Writing to a chosen writer or a logger is not a debug print.",
|
|
239
|
+
),
|
|
240
|
+
],
|
|
241
|
+
}
|