Skip to content

Commit

Permalink
Read and save env variables outside of installing a package (#14)
Browse files Browse the repository at this point in the history
The environment variables were only saved while installing a new package and were actually never read from the system.
This PR reads them on startup and allows the user to load and save them manually.

* save and read env vars manually and read on startup
* fix env not found error
  • Loading branch information
pascalzauberzeug authored Sep 17, 2024
1 parent 7acfbc7 commit 41cd4dd
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 5 deletions.
9 changes: 7 additions & 2 deletions air_link/main_page.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from nicegui import app, ui

from .authorized_keys import AuthorizedKeysDialog
from .package import add_package, show_packages
from .package import add_package, get_target_folder, read_env, show_packages, write_env
from .system import docker_prune_preview, show_disk_space


Expand All @@ -27,7 +27,12 @@ def main_page():
.tooltip('Restart Air Link') \
.props('flat round color=white')

ui.label('Environment variables').classes('text-2xl')
with ui.row():
ui.label('Environment variables').classes('text-2xl')
ui.button(icon='refresh', on_click=lambda: read_env(get_target_folder())) \
.props('flat outline').tooltip('Load environment variables')
ui.button(icon='save', on_click=lambda: write_env(get_target_folder())) \
.props('flat outline').tooltip('Save environment variables')
ui.codemirror().bind_value(app.storage.general, 'env').classes('h-32 border')

ui.label('Packages').classes('text-2xl')
Expand Down
22 changes: 19 additions & 3 deletions air_link/package.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,23 @@ def sorted_nicely(paths: List[Path]) -> List[Path]:
return sorted(paths, key=lambda path: [int(c) if c.isdigit() else c for c in re.split('([0-9]+)', path.stem)])


def write_env(target_folder: Path) -> None:
Path(target_folder / '.env').write_text(app.storage.general.get('env', ''))


def read_env(target_folder: Path) -> None:
file_path = Path(target_folder / '.env')
if not file_path.exists():
file_path.touch()
app.storage.general['env'] = file_path.read_text()


def get_target_folder() -> Path:
target_folder = Path(app.storage.general['target_directory']).expanduser()
target_folder.mkdir(exist_ok=True)
return target_folder


@ui.refreshable
def show_packages() -> ui.row:
paths = sorted_nicely(list(PACKAGES_PATH.glob('*.zip')))
Expand Down Expand Up @@ -49,8 +66,7 @@ def remove_package(path: Path) -> None:

async def install_package(path: Path) -> None:
logging.info(f'Extracting {path}...')
target = Path(app.storage.general['target_directory']).expanduser()
target.mkdir(exist_ok=True)
target = get_target_folder()
shutil.rmtree(target)
with zipfile.ZipFile(path, 'r') as zip_ref:
members = zip_ref.infolist()
Expand All @@ -59,7 +75,7 @@ async def install_package(path: Path) -> None:
os.chmod(extracted_path, member.external_attr >> 16)
logging.info('...done!')

Path(target / '.env').write_text(app.storage.general.get('env', ''))
write_env(target)

logging.info('Running install script...')
with ui.dialog(value=True).props('maximized persistent') as dialog, ui.card():
Expand Down
2 changes: 2 additions & 0 deletions air_link/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from nicegui import app, ui

from .main_page import create_page
from .package import get_target_folder, read_env
from .ssh import setup


Expand All @@ -14,6 +15,7 @@ def run() -> None:
if on_air:
app.on_startup(setup)

read_env(get_target_folder())
create_page()

ui.run(title='Air Link', favicon='⛑', reload=False, on_air=on_air)

0 comments on commit 41cd4dd

Please sign in to comment.