spcd/cd/__init__.py
Marc Beninca 70a8fd3e93
Some checks failed
/ job (push) Failing after 8s
ssh/wip
2024-05-31 00:29:12 +02:00

89 lines
2 KiB
Python

import os
from cd.project import Project
from cd.projects import Projects
from rwx import fs
from rwx import ps
COMMANDS_PREFIX = 'cd-'
projects = Projects(os.environ)
project = Project(projects)
def cd_browse_workspace():
print(f'''\
{project.root}
''', end=str())
paths = []
for directory, directories, files in os.walk(project.root):
for file in files:
absolute_path = os.path.join(directory, file)
relative_path = os.path.relpath(absolute_path, start=project.root)
paths.append(relative_path)
for path in sorted(paths):
print(path)
def cd_build_project():
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():
print(f'''\
{project.url}
''', end=str(), flush=True)
ps.run('git',
'clone',
'--branch', project.branch,
'--',
project.url,
project.root,
)
def cd_list_environment():
for variable, value in sorted(projects.environment.items()):
print(variable, '=', value)
def install_commands(path):
user = '/usr/local/bin'
for command in [
'browse-workspace',
'build-project',
'clone-branch',
'list-environment',
]:
print(command)
os.symlink(path, os.path.join(user, f'{COMMANDS_PREFIX}{command}'))
def set_ssh():
ssh_hosts = projects.environment.get('CD_SSH_HOSTS', None)
ssh_key = projects.environment.get('CD', None)
ssh_type = projects.environment.get('CD_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)