diff --git a/src/solstone_linux/cli.py b/src/solstone_linux/cli.py index 181e4e3..5fe0997 100644 --- a/src/solstone_linux/cli.py +++ b/src/solstone_linux/cli.py @@ -92,15 +92,34 @@ def cmd_setup(args: argparse.Namespace) -> int: config.ensure_dirs() save_config(config) - # Auto-register + # Auto-register — try sol CLI first (no server needed), fall back to HTTP if not config.key: - print("Registering with server...") - client = UploadClient(config) - if client.ensure_registered(config): - config = load_config() - print(f"Registered (key: {config.key[:8]}...)") - else: - print("Warning: registration failed. Run setup again when server is available.") + sol = shutil.which("sol") + if sol: + print("Registering via sol CLI...") + try: + result = subprocess.run( + [sol, "remote", "--json", "create", config.stream], + capture_output=True, text=True, timeout=10, + ) + if result.returncode == 0: + data = json.loads(result.stdout) + config.key = data["key"] + save_config(config) + print(f"Registered (key: {config.key[:8]}...)") + else: + print(f"CLI registration failed, trying HTTP...") + except (subprocess.TimeoutExpired, json.JSONDecodeError, KeyError, OSError): + print("CLI registration failed, trying HTTP...") + + if not config.key: + print("Registering with server...") + client = UploadClient(config) + if client.ensure_registered(config): + config = load_config() + print(f"Registered (key: {config.key[:8]}...)") + else: + print("Warning: registration failed. Run setup again when server is available.") else: print(f"Already registered (key: {config.key[:8]}...)") diff --git a/src/solstone_linux/upload.py b/src/solstone_linux/upload.py index 7a6844d..2c423a3 100644 --- a/src/solstone_linux/upload.py +++ b/src/solstone_linux/upload.py @@ -15,6 +15,8 @@ from __future__ import annotations import json import logging +import shutil +import subprocess import time from enum import Enum from pathlib import Path @@ -70,15 +72,34 @@ class UploadClient: def ensure_registered(self, config: Config) -> bool: """Ensure the client has a valid key, auto-registering if needed. + Tries sol CLI first (no server needed), falls back to HTTP. Returns True if a key is available. """ if self._key: return True + + # Try sol CLI registration first + name = self._stream or "solstone-linux" + sol = shutil.which("sol") + if sol: + try: + result = subprocess.run( + [sol, "remote", "--json", "create", name], + capture_output=True, text=True, timeout=10, + ) + if result.returncode == 0: + data = json.loads(result.stdout) + self._key = data["key"] + self._persist_key(config, self._key) + logger.info(f"CLI-registered as '{name}' (key: {self._key[:8]}...)") + return True + except (subprocess.TimeoutExpired, json.JSONDecodeError, KeyError, OSError) as e: + logger.debug(f"CLI registration failed: {e}") + if not self._url: return False url = f"{self._url}/app/remote/api/create" - name = self._stream or "solstone-linux" retries = min(3, len(self._retry_backoff)) for attempt in range(retries):