This commit is contained in:
2023-03-31 16:33:25 +02:00
parent e479f46d56
commit 1d96d7df60
6 changed files with 60 additions and 8 deletions

1
.gitignore vendored
View File

@@ -1,3 +1,4 @@
.env
env/
__pycache__/
*egg-info/

31
README.md Normal file
View File

@@ -0,0 +1,31 @@
# Publish Sites to Netlify via CLI!
```bash
> ls mysite
└── folder
├── index.html
├── another_page.html
├── styles.css
└── a.jpg
>
> pub --root-dir mysite
the site is published: http://6426ed336771f2380224fb84--scintillating-mochi-760bd3.netlify.app
```
# Publish Sites to Netlify With Custom Domains Too!
```bash
> pub --root-dir mysite --custom-domain dude.helpers.fun
the site is published: http://6426ee6a10e4e43866b46a42--startling-gingersnap-425138.netlify.app
the custom domain was set to dude.helpers.fun.
```
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) 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

0
publify/__init__.py Normal file
View File

View File

@@ -6,7 +6,6 @@ from time import sleep
import uuid
import requests
from rich.pretty import pprint
from dotenv import load_dotenv
@@ -60,16 +59,15 @@ def deploy_page_to_netlify(dirpath: pl.Path, custom_domain: str | None = None) -
if not response.ok:
raise Exception("something went wrong")
rj = response.json()
print("the url is " + rj["url"])
print("the site is published: " + rj["url"])
if custom_domain is not None:
sleep(2)
set_to_custom_domain(rj["id"], custom_domain)
print(f"custom domain was set to {custom_domain}")
print(f"the custom domain was set to {custom_domain}.")
def remove_custom_domain(site_id: str) -> None:
URL = f"https://app.netlify.com/access-control/bb-api/api/v1/sites/{site_id}"
print(f"{URL=}")
response = requests.put(
URL,
json={"custom_domain": None},
@@ -143,7 +141,7 @@ def cli_set_custom_domain() -> None:
try:
custom_domain = sys.argv[sys.argv.index("--custom-domain") + 1]
except IndexError:
print("Please provide a domain")
print("Please provide a --custom-domain")
return
if "--domain" not in sys.argv:
print("Please provide a --domain")
@@ -225,7 +223,7 @@ def deploy_site() -> None:
try:
custom_domain = sys.argv[sys.argv.index("--custom-domain") + 1]
except IndexError:
print("Please provide a domain")
print("Please provide a --custom-domain")
return
if "--root-dir" not in sys.argv:
print("Please provide a root directory")
@@ -243,7 +241,6 @@ def deploy_site() -> None:
):
custom_domain = f"{custom_domain}.{DOMAINS[0]}"
deploy_page_to_netlify(root_dir, custom_domain)
print("okay, should be all set!")
def cli_list_sites() -> None:
@@ -285,7 +282,7 @@ def main() -> None:
elif "--delete-site" in sys.argv or "--remove-site" in sys.argv:
cli_delete_site()
sys.exit()
elif "--custom-domain" in sys.argv:
elif "--custom-domain" in sys.argv and "--root-dir" not in sys.argv:
cli_set_custom_domain()
sys.exit()
else:

9
requirements.txt Normal file
View File

@@ -0,0 +1,9 @@
certifi==2022.12.7
charset-normalizer==3.1.0
idna==3.4
markdown-it-py==2.2.0
mdurl==0.1.2
Pygments==2.14.0
python-dotenv==1.0.0
requests==2.28.2
urllib3==1.26.15

14
setup.py Normal file
View File

@@ -0,0 +1,14 @@
from setuptools import setup
setup(
name="publify",
version="0.1.0",
description="A CLI for publishing sites to Netlify and assigning custom domains to them.",
author="Zev Averbach",
author_email="zev@averba.ch",
license="MIT",
packages=["publify"],
install_requires=["python-dotenv", "requests"],
url="https://github.com/zevaverbach/publify",
entry_points={"console_scripts": ["pub = publify.publify:main"]},
)