Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions kci
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ from kernelci.cli import ( # pylint: disable=unused-import
api as kci_api,
config as kci_config,
docker as kci_docker,
event as kci_event,
storage as kci_storage,
user as kci_user,
)
Expand Down
83 changes: 83 additions & 0 deletions kernelci/cli/event.py
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):
"""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)
2 changes: 0 additions & 2 deletions kernelci/legacy/cli/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
api,
job,
node,
pubsub,
sched,
show,
storage,
Expand All @@ -24,7 +23,6 @@
'api': api.main,
'job': job.main,
'node': node.main,
'pubsub': pubsub.main,
'sched': sched.main,
'show': show.main,
'storage': storage.main,
Expand Down
82 changes: 0 additions & 82 deletions kernelci/legacy/cli/pubsub.py

This file was deleted.