From 55aebb86f6468f11dcf507af7806fb1186eba6d6 Mon Sep 17 00:00:00 2001 From: Zev Averbach Date: Fri, 31 Mar 2023 19:09:06 +0200 Subject: [PATCH] it's working as an installable CLI on testPypi. added more docs, handled when there's no custom domains, added to gitignore --- .gitignore | 1 + README.md | 16 ++++++------ publify/publify.py | 64 +++++++++++++++++++++++++++++++++------------- pyproject.toml | 6 +++++ setup.py | 18 +++++++++++-- 5 files changed, 77 insertions(+), 28 deletions(-) create mode 100644 pyproject.toml diff --git a/.gitignore b/.gitignore index b22cd45..1c0f921 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ env/ __pycache__/ *egg-info/ +dist/ diff --git a/README.md b/README.md index 6cebb53..cd41371 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,6 @@ -# Publish Sites to Netlify via CLI! +# What Can Publify Do? + +## Publish Sites to Netlify via CLI! ```bash > ls mysite @@ -12,7 +14,7 @@ the site is published: http://6426ed336771f2380224fb84--scintillating-mochi-760bd3.netlify.app ``` -# Publish Sites to Netlify With Custom Domains Too! +## Publish Sites to Netlify With Custom Domains Too! ```bash > pub --root-dir mysite --custom-domain dude.helpers.fun @@ -20,12 +22,10 @@ the site is published: http://6426ee6a10e4e43866b46a42--startling-gingersnap-425 the custom domain was set to dude.helpers.fun. ``` +# But First, a Bit of Configuration + To do this magic, you have to first -1) Add an entry for `DOMAINS` in the `.env` file, with comma-separated values (no spaces). +1) Create an environment variable `NETLIFY_TOKEN`, obtained [here](https://app.netlify.com/user/applications#personal-access-tokens). +1) Create an environment variable `NETLIFY_DOMAINS`, with comma-separated values (no spaces), if you're planning to use custom domains. 1) Delegate DNS management of all domains listed in `DOMAINS` to Netlify ([link](https://docs.netlify.com/domains-https/netlify-dns/delegate-to-netlify/)) - -# Environment Variables - -- `NETLIFY_TOKEN` -- `DOMAINS` <-- comma-delimited domains you've already set up in Netlify diff --git a/publify/publify.py b/publify/publify.py index 4010aee..1a7e8fa 100644 --- a/publify/publify.py +++ b/publify/publify.py @@ -12,7 +12,13 @@ from dotenv import load_dotenv load_dotenv() NETLIFY_TOKEN = os.environ["NETLIFY_TOKEN"] AUTH_HEADER = {"Authorization": f"Bearer {NETLIFY_TOKEN}"} -DOMAINS = os.environ["DOMAINS"].split(",") +NETLIFY_DOMAINS = os.getenv("NETLIFY_DOMAINS") or "" +if NETLIFY_DOMAINS: + NETLIFY_DOMAINS = NETLIFY_DOMAINS.split(",") + + +class NoCustomDomains(Exception): + pass class NoNestedFolder(Exception): @@ -122,13 +128,16 @@ def cli_remove_custom_domain() -> None: """ Remove a custom domain from a Netlify site """ + if len(NETLIFY_DOMAINS) == 0: + print("Please set NETLIFY_DOMAINS in your .env file") + raise NoCustomDomains try: custom_domain = sys.argv[sys.argv.index("--remove-custom-domain") + 1] except IndexError: print("Please provide a domain") return - if len(DOMAINS) == 1 and custom_domain.count(".") == 0: - custom_domain = f"{custom_domain}.{DOMAINS[0]}" + if len(NETLIFY_DOMAINS) == 1 and custom_domain.count(".") == 0: + custom_domain = f"{custom_domain}.{NETLIFY_DOMAINS[0]}" site_id = get_site_id_from_custom_domain(custom_domain) remove_custom_domain(site_id) print(f"{custom_domain} was removed") @@ -143,9 +152,6 @@ def cli_set_custom_domain() -> None: except IndexError: print("Please provide a --custom-domain") return - if "--domain" not in sys.argv: - print("Please provide a --domain") - return try: domain = sys.argv[sys.argv.index("--domain") + 1] except IndexError: @@ -158,8 +164,8 @@ def cli_set_custom_domain() -> None: except NoResult: print(f"No site found with domain '{domain}'") return - if len(DOMAINS) == 1 and custom_domain.count(".") == 0: - custom_domain = f"{custom_domain}.{DOMAINS[0]}" + if len(NETLIFY_DOMAINS) == 1 and custom_domain.count(".") == 0: + custom_domain = f"{custom_domain}.{NETLIFY_DOMAINS[0]}" set_to_custom_domain(site_id, custom_domain) print(f"custom domain was set to {custom_domain}") @@ -182,9 +188,13 @@ def cli_delete_site() -> None: try: site_id = get_site_id_from_netlify_domain(netlify_domain) except NoResult: + if len(NETLIFY_DOMAINS) == 0: + raise NoCustomDomains( + "No custom domains configured in NETLIFY_DOMAINS, and couldn't find a site with that domain" + ) print(f"No site found with domain '{domain}', trying custom domains") - if domain.count(".") == 0 and len(DOMAINS) == 1: - domain = f"{domain}.{DOMAINS[0]}" + if domain.count(".") == 0 and len(NETLIFY_DOMAINS) == 1: + domain = f"{domain}.{NETLIFY_DOMAINS[0]}" try: site_id = get_site_id_from_custom_domain(domain) except NoResult: @@ -205,11 +215,11 @@ def display_help() -> None: print( """ netlify.py --help - netlify.py --root-dir --custom-domain + netlify.py --root-dir --custom-domain netlify.py --list-sites - netlify.py --custom-domain --domain - netlify.py --remove-custom-domain - netlify.py --delete-site + netlify.py --custom-domain --domain + netlify.py --remove-custom-domain + netlify.py --delete-site """ ) @@ -220,6 +230,10 @@ def deploy_site() -> None: """ custom_domain = None if "--custom-domain" in sys.argv: + if len(NETLIFY_DOMAINS) == 0: + raise NoCustomDomains( + "No custom domains configured in NETLIFY_DOMAINS, and --custom-domain was provided" + ) try: custom_domain = sys.argv[sys.argv.index("--custom-domain") + 1] except IndexError: @@ -236,10 +250,10 @@ def deploy_site() -> None: if ( custom_domain is not None - and len(DOMAINS) == 1 + and len(NETLIFY_DOMAINS) == 1 and custom_domain.count(".") == 0 ): - custom_domain = f"{custom_domain}.{DOMAINS[0]}" + custom_domain = f"{custom_domain}.{NETLIFY_DOMAINS[0]}" deploy_page_to_netlify(root_dir, custom_domain) @@ -278,15 +292,29 @@ def main() -> None: cli_remove_custom_domain() except NoResult: print("No site found with that custom domain") + except NoCustomDomains: + print("No custom domains configured in NETLIFY_DOMAINS") sys.exit() elif "--delete-site" in sys.argv or "--remove-site" in sys.argv: - cli_delete_site() + try: + cli_delete_site() + except NoCustomDomains as e: + print(str(e)) sys.exit() elif "--custom-domain" in sys.argv and "--root-dir" not in sys.argv: + if len(NETLIFY_DOMAINS) == 0: + print("No custom domains configured in NETLIFY_DOMAINS") + sys.exit() + if "--domain" in sys.argv: + print("Please supply a --domain") + sys.exit() cli_set_custom_domain() sys.exit() else: - deploy_site() + try: + deploy_site() + except NoCustomDomains as e: + print(str(e)) if __name__ == "__main__": diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..374b58c --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,6 @@ +[build-system] +requires = [ + "setuptools>=42", + "wheel" +] +build-backend = "setuptools.build_meta" diff --git a/setup.py b/setup.py index f01faf2..e7c0115 100644 --- a/setup.py +++ b/setup.py @@ -1,14 +1,28 @@ +import pathlib as pl from setuptools import setup setup( name="publify", - version="0.1.0", + version="0.1.2", description="A CLI for publishing sites to Netlify and assigning custom domains to them.", author="Zev Averbach", author_email="zev@averba.ch", + description_file="README.md", + long_description=pl.Path("README.md").read_text(), + long_description_content_type="text/markdown", license="MIT", + classifiers=[ + "Development Status :: 3 - Alpha", + "Intended Audience :: Developers", + "License :: OSI Approved :: MIT License", + "Programming Language :: Python :: 3", + "Programming Language :: Python :: 3.10", + "Programming Language :: Python :: 3.11", + ], packages=["publify"], + include_package_data=True, install_requires=["python-dotenv", "requests"], + python_requires=">=3.10", # only because we're using | instead of typing.Union; otherwise >= 3.9 url="https://github.com/zevaverbach/publify", - entry_points={"console_scripts": ["pub = publify.publify:main"]}, + entry_points={"console_scripts": ["pub=publify.publify:main"]}, )