Skip to content

Commit 8c4d015

Browse files
committed
kernelci.cli: use click.File for reading input
Use click.File rather than sys.stdin for reading input data. If the file name is '-' then it will use stdin which is standard practice. Signed-off-by: Guillaume Tucker <gtucker@gtucker.io>
1 parent 3ecc0d2 commit 8c4d015

File tree

2 files changed

+15
-16
lines changed

2 files changed

+15
-16
lines changed

kernelci/cli/event.py

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
"""Tool to interact with the Pub/Sub interface and message queues"""
88

9-
import sys
109
import json
1110

1211
import click
@@ -47,16 +46,16 @@ def unsubscribe(config, api, sub_id, secrets):
4746

4847

4948
@kci_event.command(secrets=True)
49+
@click.argument('input_file', type=click.File('r'))
5050
@click.option('--is-json', help="Parse input data as JSON", is_flag=True)
5151
@Args.config
5252
@Args.api
5353
@click.argument('channel')
54-
def send(config, api, is_json, channel, secrets):
55-
"""Read some data on stdin and send it as an event on a channel"""
54+
# pylint: disable=too-many-arguments
55+
def send(input_file, is_json, config, api, channel, secrets):
56+
"""Read some data and send it as an event on a channel"""
5657
api = get_api(config, api, secrets)
57-
data = sys.stdin.read()
58-
if is_json:
59-
data = json.loads(data)
58+
data = json.load(input_file) if is_json else input_file.read()
6059
api.send_event(channel, {'data': data})
6160

6261

@@ -78,16 +77,16 @@ def receive(config, api, indent, sub_id, secrets):
7877

7978

8079
@kci_event.command(secrets=True)
80+
@click.argument('input_file', type=click.File('r'))
81+
@click.argument('list_name')
8182
@click.option('--is-json', help="Parse input data as JSON", is_flag=True)
8283
@Args.config
8384
@Args.api
84-
@click.argument('list_name')
85-
def push(config, api, is_json, list_name, secrets):
86-
"""Read some data on stdin and push it as an event on a list"""
85+
# pylint: disable=too-many-arguments
86+
def push(input_file, list_name, is_json, config, api, secrets):
87+
"""Read some data and push it as an event on a list"""
8788
api = get_api(config, api, secrets)
88-
data = sys.stdin.read()
89-
if is_json:
90-
data = json.loads(data)
89+
data = json.load(input_file) if is_json else input_file.read()
9190
api.push_event(list_name, {'data': data})
9291

9392

kernelci/cli/node.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
"""Tool to manage KernelCI API node objects"""
88

99
import json
10-
import sys
1110

1211
import click
1312

@@ -69,13 +68,14 @@ def count(attributes, config, api):
6968

7069

7170
@kci_node.command(secrets=True)
71+
@click.argument('input_file', type=click.File('r'))
7272
@Args.config
7373
@Args.api
7474
@Args.indent
75-
def submit(config, api, secrets, indent):
76-
"""Submit a new node or update an existing one from stdin"""
75+
def submit(input_file, config, api, indent, secrets):
76+
"""Submit a new node or update an existing one"""
7777
api = get_api(config, api, secrets)
78-
data = json.load(sys.stdin)
78+
data = json.load(input_file)
7979
if 'id' in data:
8080
node = api.update_node(data)
8181
else:

0 commit comments

Comments
 (0)