Skip to content

Commit 2d2db1e

Browse files
authored
Add --from-stdin to stacks encrypt (#18)
* Add --from-stdin to stacks encrypt * Update stacks encrypt docs
1 parent 7bebf0a commit 2d2db1e

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

docs/3.3.4. stacks encrypt.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@
22

33
## Usage
44
```
5-
Usage: stacks encrypt [OPTIONS] STRING
5+
Usage: stacks encrypt [OPTIONS] [STRING]
66
77
Encrypt a secret string using a public key. Can run in any directory.
88
99
Options:
1010
--public-key-path TEXT [required]
11+
--from-stdin Read input string from standard input
1112
--help Show this message and exit.
1213
```
1314

src/stacks/main.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import importlib.metadata
22
import pathlib
33
import platform
4+
import sys
45

56
import click
67

@@ -65,12 +66,19 @@ def genkey(public_key_path, private_key_path):
6566

6667
@cli.command()
6768
@click.option("--public-key-path", required=True)
68-
@click.argument("string")
69-
def encrypt(public_key_path, string):
69+
@click.option("--from-stdin", is_flag=True, help="Read input string from standard input")
70+
@click.argument("string", required=False)
71+
def encrypt(public_key_path, from_stdin, string):
7072
"""
7173
Encrypt a secret string using a public key.
7274
Can run in any directory.
7375
"""
76+
if from_stdin:
77+
string = sys.stdin.read()
78+
if not string:
79+
raise click.ClickException("No input received from stdin.")
80+
elif string is None:
81+
raise click.ClickException("You must provide either a string argument or use --from-stdin.")
7482
print(helpers.encrypt(public_key_path=pathlib.Path(public_key_path), string=string))
7583

7684

0 commit comments

Comments
 (0)