import os import env from rwx import fs, ps from cd.project import Project from cd.projects import Projects COMMANDS_PREFIX = "cd-" projects = Projects(os.environ) project = Project(projects) def cd_browse_workspace() -> None: browse(project.root) def cd_build_project() -> None: for extension in ["py", "sh"]: path = os.path.join(project.root, f"build.{extension}") if os.path.exists(path): ps.run(path) break else: pass def cd_clone_branch() -> None: print(f"""\ {project.url} ↓ """, end="", flush=True) ps.run("git", "clone", "--branch", project.branch, "--", project.url, project.root, ) def cd_list_environment() -> None: for variable, value in sorted(projects.environment.items()): print(variable, "=", value) def cd_synchronize() -> None: host = "rwx.work" source = "out" user = "cd" # root = os.sep.join(["", user, project.branch, projects.group, project.name]) # target = f"{user}@{host}:{root}" ps.run("rsync", "--archive", "--delete-before", "--verbose", f"{source}/", f"{target}/", "--dry-run", ) def browse(root: str) -> None: paths = [] for directory, _, files in os.walk(root): for file in files: absolute_path = os.path.join(directory, file) relative_path = os.path.relpath(absolute_path, start=root) paths.append(relative_path) open(root) for path in sorted(paths): print(path) shut(root) def cat(file: str) -> None: open(file) print(fs.read_file_text(file).rstrip()) shut(file) def install_commands(path) -> None: step("Install commands") user = "/usr/local/bin" for command in [ "browse-workspace", "build-project", "clone-branch", "list-environment", "synchronize", ]: print(command) os.symlink(path, os.path.join(user, f"{COMMANDS_PREFIX}{command}")) def set_ssh(*arguments) -> None: step("Set SSH") # ssh_key, ssh_hosts = arguments # ssh_type = "ed25519" # home = os.path.expanduser("~") # ssh = os.path.join(home, ".ssh") os.makedirs(ssh, exist_ok=True) os.chmod(ssh, 0o700) # key = os.path.join(ssh, f"id_{ssh_type}") if ssh_key: fs.write(key, ssh_key) os.chmod(key, 0o400) # known = os.path.join(ssh, "known_hosts") if ssh_hosts: fs.write(known, ssh_hosts) os.chmod(known, 0o400) # browse(ssh) cat(known) def open(*arguments) -> None: print(env.CD_OPEN, end="") print(*arguments, flush=True) def shut(*arguments) -> None: print(env.CD_SHUT, end="") print(*arguments, flush=True) def split() -> None: print(env.CD_SPLT, flush=True) def step(*arguments) -> None: env.CD_STEP += 1 print(env.CD_DOWN) print(env.CD_VERT, env.CD_STEP, *arguments) print(env.CD___UP, flush=True)