Skip to content

Commit 0321548

Browse files
author
Aaron Loo
committed
DRYer code
1 parent 680f00a commit 0321548

File tree

4 files changed

+19
-18
lines changed

4 files changed

+19
-18
lines changed

detect_secrets/core/audit.py

+2-6
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
from ..plugins.core import initialize
1111
from ..plugins.high_entropy_strings import HighEntropyStringsPlugin
12+
from .baseline import format_baseline_for_output
1213
from .baseline import merge_results
1314
from .bidirectional_iterator import BidirectionalIterator
1415
from .color import BashColor
@@ -201,12 +202,7 @@ def _handle_user_decision(decision, secret):
201202

202203
def _save_baseline_to_file(filename, data): # pragma: no cover
203204
with open(filename, 'w') as f:
204-
f.write(json.dumps(
205-
data,
206-
indent=2,
207-
sort_keys=True,
208-
separators=(',', ': '),
209-
))
205+
f.write(format_baseline_for_output(data))
210206

211207

212208
def _get_secret_with_context(

detect_secrets/core/baseline.py

+14
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from __future__ import absolute_import
22

3+
import json
34
import os
45
import re
56
import subprocess
@@ -229,6 +230,19 @@ def merge_results(old_results, new_results):
229230
return new_results
230231

231232

233+
def format_baseline_for_output(baseline):
234+
"""
235+
:type baseline: dict
236+
:rtype: str
237+
"""
238+
return json.dumps(
239+
baseline,
240+
indent=2,
241+
sort_keys=True,
242+
separators=(',', ': '),
243+
)
244+
245+
232246
def _get_git_tracked_files(rootdir='.'):
233247
"""Parsing .gitignore rules is hard.
234248

detect_secrets/main.py

+1-4
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,8 @@ def main(argv=None):
3838
_scan_string(line, plugins)
3939

4040
else:
41-
output = json.dumps(
41+
output = baseline.format_baseline_for_output(
4242
_perform_scan(args, plugins),
43-
indent=2,
44-
sort_keys=True,
45-
separators=(',', ': '),
4643
)
4744

4845
if args.import_filename:

detect_secrets/pre_commit_hook.py

+2-8
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import textwrap
77

88
from detect_secrets import VERSION
9+
from detect_secrets.core.baseline import format_baseline_for_output
910
from detect_secrets.core.baseline import get_secrets_not_in_baseline
1011
from detect_secrets.core.baseline import update_baseline_with_removed_secrets
1112
from detect_secrets.core.log import get_logger
@@ -72,14 +73,7 @@ def main(argv=None):
7273
def _write_to_baseline_file(filename, payload): # pragma: no cover
7374
"""Breaking this function up for mockability."""
7475
with open(filename, 'w') as f:
75-
f.write(
76-
json.dumps(
77-
payload,
78-
indent=2,
79-
sort_keys=True,
80-
separators=(',', ': '),
81-
),
82-
)
76+
f.write(format_baseline_for_output(payload))
8377

8478

8579
def get_baseline(baseline_filename):

0 commit comments

Comments
 (0)