Compare commits

..

No commits in common. 'main' and 'v0.0.6' have entirely different histories.
main ... v0.0.6

@ -9,7 +9,6 @@ import urllib.parse
from os import environ, mkdir
from os.path import isdir, isfile, join
from typing import Union
from urllib.parse import urlparse
import argcomplete
import requests
@ -42,8 +41,9 @@ def nested_out(input, tabs="") -> str:
string += "{}\n".format(input)
elif isinstance(input, dict):
for key, value in input.items():
string += "{}{}: {}".format(tabs, key,
nested_out(value, tabs + " "))
string += "{}{}: {}".format(
tabs, key, nested_out(value, tabs + " ")
)
elif isinstance(input, list):
for entry in input:
string += "{}\n{}".format(tabs, nested_out(entry, tabs + " "))
@ -62,18 +62,9 @@ def output(response: list[dict], jsonout: bool = False):
# Define the runner for each command
def run_add(url: str, jsonout: bool, headers: dict):
parsed = split_url(url)
print(url)
response = requests.put(url, headers=headers)
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)
output(response.json(), jsonout)
def run_complete(shell: Union[None, str]):
@ -103,7 +94,8 @@ def run_config(
error(
"Can not configure without {}".format(need),
"No {}".format(need),
))
)
)
sys.exit(1)
config[need] = input("Enter {}: ".format(need))
@ -127,10 +119,9 @@ def run_delete(url: str, jsonout: bool, headers: dict):
output(reply, jsonout)
def run_list(url: str,
jsonout: bool,
headers: dict,
ret=False) -> Union[None, str]:
def run_list(
url: str, jsonout: bool, headers: dict, ret=False
) -> Union[None, str]:
response = requests.get(url, headers=headers)
string = response.json()
if ret:
@ -180,14 +171,16 @@ def setup_url(
error(
"ttl only makes sense with rtype, name and zone",
"Missing parameter",
))
)
)
sys.exit(1)
if rtype and (not name or not zone):
output(
error(
"rtype only makes sense with name and zone",
"Missing parameter",
))
)
)
sys.exit(1)
if name and not zone:
output(error("name only makes sense with a zone", "Missing parameter"))
@ -195,38 +188,6 @@ def setup_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
def main() -> int:
# Grab user input
@ -308,10 +269,6 @@ def main() -> int:
except KeyError:
output(response.json())
return 1
except requests.exceptions.JSONDecodeError:
output(
error("Could not decode api response as JSON", "Could not decode"))
return 1
headers = {"Authorization": "Bearer {}".format(token)}
# Route based on command
@ -354,7 +311,8 @@ def main() -> int:
run_update(url, args.json, headers)
except (RequestsJSONDecodeError, SimplejsonJSONDecodeError):
output(
error("Could not decode api response as JSON", "Could not decode"))
error("Could not decode api response as JSON", "Could not decode")
)
return 0

Loading…
Cancel
Save