-
Notifications
You must be signed in to change notification settings - Fork 109
Rework kci pubsub
commands with Click
framework
#2203
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
# SPDX-License-Identifier: LGPL-2.1-or-later | ||
# | ||
# Copyright (C) 2023 Collabora Limited | ||
# Author: Guillaume Tucker <[email protected]> | ||
# Author: Jeny Sadadia <[email protected]> | ||
|
||
"""Tool to interact with the Pub/Sub interface and message queues""" | ||
|
||
import sys | ||
import json | ||
|
||
import click | ||
|
||
import kernelci.api | ||
import kernelci.api.helper | ||
import kernelci.config | ||
from . import Args, kci | ||
|
||
|
||
@kci.group(name='event') | ||
def kci_event(): | ||
"""Interact with Pub/Sub and message queue events""" | ||
|
||
|
||
@kci_event.command(secrets=True) | ||
@click.argument('channel') | ||
@Args.config | ||
@Args.api | ||
def subscribe(config, api, channel, secrets): | ||
"""Subscribe to a Pub/Sub channel""" | ||
configs = kernelci.config.load(config) | ||
api_config = configs['api'][api] | ||
api = kernelci.api.get_api(api_config, secrets.api.token) | ||
sub_id = api.subscribe(channel) | ||
click.echo(sub_id) | ||
|
||
|
||
@kci_event.command(secrets=True) | ||
@click.argument('sub_id') | ||
@Args.config | ||
@Args.api | ||
def unsubscribe(config, api, sub_id, secrets): | ||
"""Unsubscribe from a Pub/Sub channel""" | ||
configs = kernelci.config.load(config) | ||
api_config = configs['api'][api] | ||
api = kernelci.api.get_api(api_config, secrets.api.token) | ||
api.unsubscribe(sub_id) | ||
|
||
|
||
@kci_event.command(secrets=True) | ||
@click.option('--is-json', help="Parse input data as JSON", is_flag=True) | ||
@Args.config | ||
@Args.api | ||
@click.argument('channel') | ||
def send(config, api, is_json, channel, secrets): | ||
"""Read some data on stdin and send it as an event on a channel""" | ||
configs = kernelci.config.load(config) | ||
api_config = configs['api'][api] | ||
api = kernelci.api.get_api(api_config, secrets.api.token) | ||
data = sys.stdin.read() | ||
if is_json: | ||
data = json.loads(data) | ||
api.send_event(channel, {'data': data}) | ||
|
||
|
||
@kci_event.command(secrets=True) | ||
@click.argument('sub_id') | ||
@Args.config | ||
@Args.api | ||
@Args.indent | ||
def receive(config, api, indent, sub_id, secrets): | ||
gctucker marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"""Wait and receive an event from a subscription and print on stdout""" | ||
configs = kernelci.config.load(config) | ||
api_config = configs['api'][api] | ||
api = kernelci.api.get_api(api_config, secrets.api.token) | ||
helper = kernelci.api.helper.APIHelper(api) | ||
event = helper.receive_event_data(sub_id) | ||
if isinstance(event, str): | ||
click.echo(event.strip()) | ||
elif isinstance(event, dict): | ||
click.echo(json.dumps(event, indent=indent)) | ||
else: | ||
click.echo(event) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.