it's working as an installable CLI on testPypi. added more docs, handled when there's no custom domains, added to gitignore

This commit is contained in:
2023-03-31 19:09:06 +02:00
parent da8bdd1969
commit 55aebb86f6
5 changed files with 77 additions and 28 deletions

1
.gitignore vendored
View File

@@ -2,3 +2,4 @@
env/
__pycache__/
*egg-info/
dist/

View File

@@ -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

View File

@@ -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 <path> --custom-domain <domain (or subdomain prefix if there's only one domain in DOMAINS)>
netlify.py --root-dir <path> --custom-domain <domain (or subdomain prefix if there's only one domain in NETLIFY_DOMAINS)>
netlify.py --list-sites
netlify.py --custom-domain <custom_domain (or subdomain prefix if there's only one domain in DOMAINS)> --domain <existing_domain>
netlify.py --remove-custom-domain <custom_domain (or subdomain prefix if there's only one domain in DOMAINS)>
netlify.py --delete-site <domain (or subdomain prefix if there's only one domain in DOMAINS)>
netlify.py --custom-domain <custom_domain (or subdomain prefix if there's only one domain in NETLIFY_DOMAINS)> --domain <existing_domain>
netlify.py --remove-custom-domain <custom_domain (or subdomain prefix if there's only one domain in NETLIFY_DOMAINS)>
netlify.py --delete-site <domain (or subdomain prefix if there's only one domain in NETLIFY_DOMAINS)>
"""
)
@@ -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:
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:
try:
deploy_site()
except NoCustomDomains as e:
print(str(e))
if __name__ == "__main__":

6
pyproject.toml Normal file
View File

@@ -0,0 +1,6 @@
[build-system]
requires = [
"setuptools>=42",
"wheel"
]
build-backend = "setuptools.build_meta"

View File

@@ -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"]},
)