Skip to content

Commit e7ab6c1

Browse files
author
rootware
committed
added redaction support for linux piped commands
1 parent 5b66cd9 commit e7ab6c1

File tree

2 files changed

+27
-13
lines changed

2 files changed

+27
-13
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ and simply didn't have the time to go back and retroactively create one.
1616

1717
### Added
1818

19-
- None at the moment.
19+
- Added in redaction support from linux piped input.
20+
- echo 'This is my ip: 127.0.0.1. My email is [email protected]. My favorite secret link is github.com' | prk
2021

2122
## [0.4.0] - 2022-07-27
2223

pyredactkit/runner.py

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,11 @@
3636
help_menu = """
3737
PyRedactKit - Redact and Un-redact any sensitive data from your text files!
3838
Example usage:\n
39-
prk 'This is my ip: 127.0.0.1. My email is [email protected]. My favorite secret link is github.com'\n
40-
prk [file/directory_with_files]\n
41-
prk redacted_file --unredact .hashshadow.json\n
42-
prk file --customfile custom.json\n
39+
prk 'This is my ip: 127.0.0.1. My email is [email protected]. My favorite secret link is github.com'
40+
prk [file/directory_with_files]
41+
prk redacted_file --unredact .hashshadow.json
42+
prk file --customfile custom.json
43+
echo 'This is my ip: 127.0.0.1. My email is [email protected]. My favorite secret link is github.com' | prk \n
4344
"""
4445

4546

@@ -51,12 +52,9 @@ def arg_helper() -> argparse.Namespace:
5152
parser.add_argument(
5253
"text",
5354
help="""Supply either a text chunk or file name path to redact sensitive data from command prompt.""",
54-
nargs="*"
55+
nargs="*",
56+
default=sys.stdin
5557
)
56-
if len(sys.argv) == 1:
57-
print(help_menu)
58-
parser.print_help(sys.stderr)
59-
sys.exit(1)
6058
parser.add_argument(
6159
"-u",
6260
"--unredact",
@@ -94,9 +92,8 @@ def arg_helper() -> argparse.Namespace:
9492
default='',
9593
help='File extension to filter by.'
9694
)
97-
args = parser.parse_args()
9895

99-
return args
96+
return parser
10097

10198

10299
def is_it_file(file_path: str) -> bool:
@@ -117,12 +114,28 @@ def recursive_file_search(full_path: str, extension: str, recursive: bool) -> se
117114

118115

119116
def execute_redact_logic() -> None:
120-
args = arg_helper()
117+
# Access main parser namespace first
118+
parser = arg_helper()
119+
# Parse the arguments from parser object
120+
args = parser.parse_args()
121+
122+
if len(sys.argv) == 1:
123+
if sys.stdin.isatty():
124+
print(help_menu)
125+
parser.print_help(sys.stderr)
126+
sys.exit(1)
127+
128+
# This is reading in from linux piped stdin to redact. - echo 'This is my ip: 127.0.0.1.' | prk
129+
stdin = parser.parse_args().text.read().splitlines()
130+
core_redact.process_text(stdin)
131+
sys.exit(1)
121132

133+
# This is detecting if it's a text or file input and redacting argument supplied like - prk 'This is my ip: 127.0.0.1.'
122134
is_text = is_it_file(args.text[0])
123135
if not is_text:
124136
core_redact.process_text(args.text)
125137

138+
# This is redacting all the files.
126139
files = recursive_file_search(args.text, args.extension, args.recursive)
127140

128141
for file in files:

0 commit comments

Comments
 (0)