-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Move 'click.command' to the script rather than the module. - Use exceptions to handle errors and pass them to the script, rather than calling 'sys.exit' from the module. - Use 'warnings.warn' to handle warnings and catch these in the script. - Implement colourised print messages along with the '--nocolour' flag to disable them. - Move message printing to the script rather than logging function, along with logic for '--quiet'. - Set flake8 'max-line-length = 100'. - Bump version number to 0.2 to reflect significant changes.
- Loading branch information
Nathan Parsons
committed
Sep 23, 2018
1 parent
726fb3e
commit 51475a8
Showing
6 changed files
with
193 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
[flake8] | ||
max-line-length=100 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -102,3 +102,6 @@ venv.bak/ | |
|
||
# mypy | ||
.mypy_cache/ | ||
|
||
# Ignore personal configuration (contains secrets!) | ||
config.yaml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,65 @@ | ||
#!/usr/bin/env python3 | ||
|
||
import sys | ||
import warnings | ||
|
||
import click | ||
|
||
import godaddy_ddns | ||
|
||
|
||
godaddy_ddns.update_ip() | ||
@click.command() | ||
@click.option( | ||
'--config', '-c', | ||
default="/etc/godaddy-ddns/config.yaml", | ||
help="Path to configuration file (.yaml).", | ||
type=click.File('r') | ||
) | ||
@click.option('--force', '-f', is_flag=True, help="Update the IP regardless of the cached IP.") | ||
@click.option('--quiet', '-q', is_flag=True, help="Don't print to stdout.") | ||
@click.option('--nocolour', is_flag=True, help="Don't colourise messages to stdout.") | ||
def main(config, force, quiet, nocolour): | ||
# Define an echo function to account for 'quiet' and 'nocolour' | ||
def echo(msg): | ||
if quiet: | ||
pass | ||
elif nocolour: | ||
print(msg) | ||
else: | ||
godaddy_ddns.print_colourised(msg) | ||
|
||
# Notify if forced | ||
if force: | ||
echo("Info: Beginning forced update.") | ||
|
||
# Perform the update | ||
try: | ||
# Catch and record warnings to print later | ||
with warnings.catch_warnings(record=True) as caught_warnings: | ||
updated, myip, domains = godaddy_ddns.update_ip(config, force) | ||
except (godaddy_ddns.ConfigError, | ||
godaddy_ddns.BadResponse, | ||
PermissionError, | ||
ConnectionError) as e: | ||
# Echo the message and exit with failure | ||
echo(str(e)) | ||
sys.exit(1) | ||
except: | ||
echo("Error: An unexpected exception occurred!") | ||
raise # raise the exception for debugging/issue reporting | ||
|
||
# Print any warnings | ||
for warning in caught_warnings: | ||
echo(str(warning.message)) | ||
|
||
# Report the status of the update | ||
if updated: | ||
forced = "forcefully " if force else "" | ||
for domain in domains: | ||
echo("Success: IP address {}updated to {} for {}.".format(forced, myip, domain)) | ||
else: | ||
echo("Success: IP address is already up-to-date ({})".format(myip)) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters