spcd/spcd/__init__.py
2024-07-22 10:00:39 +02:00

68 lines
1.5 KiB
Python

"""Shell to Python Continuous Deployment."""
__version__ = "0.0.1"
import sys
from pathlib import Path
from rwx import fs
from rwx.log import stream as log
from rwx.ps import run
import spcd
from spcd import cmd
from spcd.util import browse, cat, step
COMMANDS_PREFIX = "spcd-"
def install_commands(path: str) -> None:
step("Install commands")
user = Path("/usr/local/bin")
for command in [
"browse-workspace",
"build-project",
"clone-branch",
"list-environment",
"synchronize",
]:
log.info(command)
(user / f"{COMMANDS_PREFIX}{command}").symlink_to(path)
def main(main: str) -> None:
path, *arguments = sys.argv
name = Path(path).name
if name == "__main__.py":
spcd.set_ssh(*arguments)
spcd.install_commands(main)
else:
function = getattr(cmd, name.replace("-", "_"))
function(*arguments)
def set_ssh(*arguments: list[str]) -> None:
step("Set SSH")
# get variables
ssh_key, ssh_hosts = arguments
# set key type
ssh_type = "ed25519"
# set home directory
home = Path("~").expanduser()
# make home directory
ssh = home / ".ssh"
ssh.mkdir(exist_ok=True, parents=True)
ssh.chmod(0o700)
# write private key
key = ssh / f"id_{ssh_type}"
if ssh_key:
fs.write(key, ssh_key)
key.chmod(0o400)
# write known hosts
known = ssh / "known_hosts"
if ssh_hosts:
fs.write(known, ssh_hosts)
known.chmod(0o400)
# display
browse(ssh)
cat(known)