Only output a single record on successfull add

Fixes #1
main
Micke Nordin 2 years ago
parent 7caed48746
commit 665942653e
Signed by untrusted user: micke
GPG Key ID: 0DA0A7A5708FE257

@ -9,6 +9,7 @@ import urllib.parse
from os import environ, mkdir from os import environ, mkdir
from os.path import isdir, isfile, join from os.path import isdir, isfile, join
from typing import Union from typing import Union
from urllib.parse import urlparse
import argcomplete import argcomplete
import requests import requests
@ -62,9 +63,20 @@ def output(response: list[dict], jsonout: bool = False):
# Define the runner for each command # Define the runner for each command
def run_add(url: str, jsonout: bool, headers: dict): def run_add(url: str, jsonout: bool, headers: dict):
print(url) parsed = split_url(url)
response = requests.put(url, headers=headers) response = requests.put(url, headers=headers)
output(response.json(), jsonout) out = response.json()
if isinstance(out, list):
for record in out:
if (
record["data"] == parsed["data"]
and record["name"] == parsed["name"]
and record["rtype"] == parsed["rtype"]
):
output(record, jsonout)
break
else:
output(out, jsonout)
def run_complete(shell: Union[None, str]): def run_complete(shell: Union[None, str]):
@ -188,6 +200,38 @@ def setup_url(
return url return url
def split_url(url: str) -> dict:
parsed = urlparse(url, allow_fragments=False)
path = parsed.path
query = parsed.query
arguments: Union[None, list[str]] = query.split("&")
path_arr = path.split("/")
data: Union[None, str] = None
name: Union[None, str] = None
rtype: Union[None, str] = None
ttl: Union[None, str] = None
zone: Union[None, str] = None
if len(path_arr) > 2:
zone = path_arr[2]
if len(path_arr) > 4:
name = path_arr[4]
if len(path_arr) > 5:
rtype = path_arr[5]
if len(path_arr) > 6:
data = path_arr[6]
if len(path_arr) > 7:
ttl = path_arr[7]
return {
"arguments": arguments,
"data": data,
"name": name,
"rtype": rtype,
"ttl": ttl,
"zone": zone,
}
# Entry point to program # Entry point to program
def main() -> int: def main() -> int:
# Grab user input # Grab user input

Loading…
Cancel
Save