From a344633bbbad0e36d26e6eaa3879828bc9d5126b Mon Sep 17 00:00:00 2001 From: Micke Nordin Date: Thu, 27 Oct 2022 15:47:54 +0200 Subject: [PATCH] Add support for query parameters --- scripts/knotctl | 26 +++++++++++++++++++++----- setup.py | 2 +- 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/scripts/knotctl b/scripts/knotctl index 8581bb7..cb0e46d 100755 --- a/scripts/knotctl +++ b/scripts/knotctl @@ -5,7 +5,7 @@ import getpass import json import os import sys -from collections.abc import Sequence +import urllib.parse from os import environ, mkdir from os.path import isdir, isfile, join from typing import Union @@ -19,7 +19,7 @@ from simplejson.errors import JSONDecodeError as SimplejsonJSONDecodeError # Helper functions -def error(description: str, error: str) -> Sequence[dict]: +def error(description: str, error: str) -> list[dict]: response = [] reply = {} # https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/406 @@ -49,7 +49,7 @@ def nested_out(input, tabs="") -> str: return string -def output(response: Sequence[dict], jsonout: bool = False): +def output(response: list[dict], jsonout: bool = False): try: if jsonout: print(json.dumps(response)) @@ -128,6 +128,7 @@ def run_update(url: str, jsonout: bool, headers: dict): # Set up the url def setup_url( baseurl: str, + arguments: Union[None, list[str]], data: Union[None, str], name: Union[None, str], rtype: Union[None, str], @@ -147,6 +148,13 @@ def setup_url( url += "/{}".format(data) if ttl and data and zone and name and rtype: url += "/{}".format(ttl) + if data and zone and name and rtype and arguments: + url += "?" + for arg in arguments: + if not url.endswith("?"): + url += "&" + key, value = arg.split("=") + url += key + "=" + urllib.parse.quote_plus(value) if ttl and (not rtype or not name or not zone): output( @@ -198,10 +206,17 @@ def main() -> int: listcmd.add_argument("-z", "--zone") updatecmd = subparsers.add_parser("update") + updatecmd.add_argument( + "-a", + "--argument", + nargs="*", + help="Specify key - value pairs to be updated: name=dns1.example.com.", + required=True, + ) updatecmd.add_argument("-d", "--data", required=True) updatecmd.add_argument("-n", "--name", required=True) updatecmd.add_argument("-r", "--rtype", required=True) - updatecmd.add_argument("-t", "--ttl", required=True) + updatecmd.add_argument("-t", "--ttl") updatecmd.add_argument("-z", "--zone", required=True) argcomplete.autocomplete(parser) @@ -244,7 +259,8 @@ def main() -> int: ttl = None if 'ttl' in args: ttl = args.ttl - url = setup_url(baseurl, args.data, args.name, args.rtype, ttl, args.zone) + url = setup_url(baseurl, args.argument, args.data, args.name, args.rtype, + ttl, args.zone) try: if args.command == "add": run_add(url, args.json, headers) diff --git a/setup.py b/setup.py index 47eb8fa..9172eaf 100644 --- a/setup.py +++ b/setup.py @@ -5,7 +5,7 @@ with open("README.md", "r", encoding="utf-8") as fh: setuptools.setup( name="knotctl", - version="0.0.1", + version="0.0.2", packages = setuptools.find_packages(), author="Micke Nordin", author_email="hej@mic.ke",