diff --git a/air_link/main_page.py b/air_link/main_page.py index e7f0c4f..849925d 100644 --- a/air_link/main_page.py +++ b/air_link/main_page.py @@ -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 @@ -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') diff --git a/air_link/package.py b/air_link/package.py index 92d74ab..825f6e9 100644 --- a/air_link/package.py +++ b/air_link/package.py @@ -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'))) @@ -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() @@ -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(): diff --git a/air_link/run.py b/air_link/run.py index f5881a8..e653f51 100644 --- a/air_link/run.py +++ b/air_link/run.py @@ -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 @@ -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)