sane-ip-check: refactor: split out a `main` function

This commit is contained in:
Colin 2024-04-18 19:42:16 +00:00
parent bdc3b1ed0e
commit 3bd56fb565
1 changed files with 16 additions and 14 deletions

View File

@ -28,6 +28,21 @@ def get_wan_fallback():
else:
return ip
def main(format: str, try_upnp: bool) -> None:
upnp_details = get_any_wan() if try_upnp else None
if upnp_details:
root_dev, _lan_ip, wan_ip = upnp_details
else:
root_dev, wan_ip = "", get_wan_fallback()
if format == "plaintext":
print(wan_ip)
elif format == "json":
print(json.dumps(dict(
wan=wan_ip,
upnp=root_dev,
)))
if __name__ == '__main__':
logging.basicConfig()
@ -50,17 +65,4 @@ if __name__ == '__main__':
if args.no_upnp:
try_upnp = False
upnp_details = get_any_wan() if try_upnp else None
if upnp_details:
root_dev, _lan_ip, wan_ip = upnp_details
else:
root_dev, wan_ip = "", get_wan_fallback()
if format == "plaintext":
print(wan_ip)
elif format == "json":
print(json.dumps(dict(
wan=wan_ip,
upnp=root_dev,
)))
main(format=format, try_upnp=try_upnp)