#!/usr/bin/env python import sys import click import requests @click.group() def kroute(): pass @kroute.command() @click.option("-c", "--command", nargs=1) @click.option("-e", "--entrypoint", default="/bin/sh -c") @click.option("-X", "--method", default="GET") @click.option("--url", envvar='KAPOW_URL') @click.argument("url_pattern", nargs=1) @click.argument("command_file", required=False) def add(url_pattern, entrypoint, command, method, url, command_file): if command: # Command is given inline source = command elif command_file is None: # No command source = "" elif command_file == '-': # Read commands from stdin source = sys.stdin.read() else: # Read commands from a file with open(command_file, 'r', encoding='utf-8') as handler: source = handler.read() response = requests.post(f"{url}/routes", json={"method": method, "url_pattern": url_pattern, "entrypoint": entrypoint, "command": source}) response.raise_for_status() print(response.json()) @kroute.command() @click.option("--url", envvar='KAPOW_URL') @click.argument("route-id") def remove(route_id, url): response = requests.delete(f"{url}/routes/{route_id}") response.raise_for_status() print(response.json()) if __name__ == '__main__': kroute()