diff --git a/pkgs/clan-cli/clan_cli/dirs.py b/pkgs/clan-cli/clan_cli/dirs.py new file mode 100644 index 0000000..e895df0 --- /dev/null +++ b/pkgs/clan-cli/clan_cli/dirs.py @@ -0,0 +1,25 @@ +import os +import sys +from pathlib import Path + + +def get_clan_flake_toplevel() -> Path: + """Returns the path to the toplevel of the clan flake""" + initial_path = Path(os.getcwd()) + path = Path(initial_path) + while path.parent == path: + project_files = [".clan-flake"] + for project_file in project_files: + if (path / project_file).exists(): + return path + path = path.parent + return initial_path + + +def user_data_dir() -> Path: + if sys.platform == "win32": + raise NotImplementedError("Windows is not supported") + elif sys.platform == "darwin": + return Path(os.path.expanduser("~/Library/Application Support/")) + else: + return Path(os.getenv("XDG_DATA_HOME", os.path.expanduser("~/.local/share"))) diff --git a/pkgs/clan-cli/clan_cli/errors.py b/pkgs/clan-cli/clan_cli/errors.py new file mode 100644 index 0000000..32a26df --- /dev/null +++ b/pkgs/clan-cli/clan_cli/errors.py @@ -0,0 +1,4 @@ +class ClanError(Exception): + """Base class for exceptions in this module.""" + + pass diff --git a/pkgs/clan-cli/clan_cli/tty.py b/pkgs/clan-cli/clan_cli/tty.py new file mode 100644 index 0000000..5d930ec --- /dev/null +++ b/pkgs/clan-cli/clan_cli/tty.py @@ -0,0 +1,25 @@ +import sys +from typing import IO, Any, Callable + + +def is_interactive() -> bool: + """Returns true if the current process is interactive""" + return sys.stdin.isatty() and sys.stdout.isatty() + + +def color_text(code: int, file: IO[Any] = sys.stdout) -> Callable[[str], None]: + """ + Print with color if stderr is a tty + """ + + def wrapper(text: str) -> None: + if file.isatty(): + print(f"\x1b[{code}m{text}\x1b[0m", file=file) + else: + print(text, file=file) + + return wrapper + + +warn = color_text(91, file=sys.stderr) +info = color_text(92, file=sys.stderr) diff --git a/pkgs/clan-cli/clan_cli/zerotier/__init__.py b/pkgs/clan-cli/clan_cli/zerotier/__init__.py index 48a2488..be00bc6 100644 --- a/pkgs/clan-cli/clan_cli/zerotier/__init__.py +++ b/pkgs/clan-cli/clan_cli/zerotier/__init__.py @@ -8,6 +8,7 @@ from pathlib import Path from tempfile import TemporaryDirectory from typing import Any, Iterator, Optional +from ..errors import ClanError from ..nix import nix_shell @@ -30,11 +31,11 @@ def try_connect_port(port: int) -> bool: return result == 0 -def find_free_port(port_range: range) -> int: +def find_free_port(port_range: range) -> Optional[int]: for port in port_range: if try_bind_port(port): return port - raise Exception("cannot find a free port") + return None class ZerotierController: @@ -86,6 +87,8 @@ class ZerotierController: def zerotier_controller() -> Iterator[ZerotierController]: # This check could be racy but it's unlikely in practice controller_port = find_free_port(range(10000, 65535)) + if controller_port is None: + raise ClanError("cannot find a free port for zerotier controller") cmd = nix_shell(["bash", "zerotierone"], ["bash", "-c", "command -v zerotier-one"]) res = subprocess.run( cmd, @@ -95,10 +98,10 @@ def zerotier_controller() -> Iterator[ZerotierController]: ) zerotier_exe = res.stdout.strip() if zerotier_exe is None: - raise Exception("cannot find zerotier-one executable") + raise ClanError("cannot find zerotier-one executable") if not zerotier_exe.startswith("/nix/store"): - raise Exception( + raise ClanError( f"zerotier-one executable needs to come from /nix/store: {zerotier_exe}" ) @@ -136,7 +139,7 @@ def zerotier_controller() -> Iterator[ZerotierController]: while not try_connect_port(controller_port): status = p.poll() if status is not None: - raise Exception( + raise ClanError( f"zerotier-one has been terminated unexpected with {status}" ) time.sleep(0.1) diff --git a/pkgs/clan-cli/tests/__init__.py b/pkgs/clan-cli/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/pkgs/clan-cli/tests/environment.py b/pkgs/clan-cli/tests/environment.py new file mode 100644 index 0000000..24226db --- /dev/null +++ b/pkgs/clan-cli/tests/environment.py @@ -0,0 +1,14 @@ +import os +from contextlib import contextmanager +from typing import Iterator + + +@contextmanager +def mock_env(**environ: str) -> Iterator[None]: + original_environ = dict(os.environ) + os.environ.update(environ) + try: + yield + finally: + os.environ.clear() + os.environ.update(original_environ) diff --git a/pkgs/clan-cli/tests/test_clan_ssh.py b/pkgs/clan-cli/tests/test_clan_ssh.py index e864a18..628bc0e 100644 --- a/pkgs/clan-cli/tests/test_clan_ssh.py +++ b/pkgs/clan-cli/tests/test_clan_ssh.py @@ -1,7 +1,5 @@ -import os import sys -from contextlib import contextmanager -from typing import Iterator, Union +from typing import Union import pytest import pytest_subprocess.fake_process @@ -9,6 +7,8 @@ from pytest_subprocess import utils import clan_cli.ssh +from .environment import mock_env + def test_no_args( capsys: pytest.CaptureFixture, monkeypatch: pytest.MonkeyPatch @@ -20,17 +20,6 @@ def test_no_args( assert captured.err.startswith("usage:") -@contextmanager -def mock_env(**environ: str) -> Iterator[None]: - original_environ = dict(os.environ) - os.environ.update(environ) - try: - yield - finally: - os.environ.clear() - os.environ.update(original_environ) - - # using fp fixture from pytest-subprocess def test_ssh_no_pass(fp: pytest_subprocess.fake_process.FakeProcess) -> None: with mock_env(CLAN_FLAKE="/mocked-flake"): diff --git a/templates/new-clan/.clan-flake b/templates/new-clan/.clan-flake new file mode 100644 index 0000000..406fcfe --- /dev/null +++ b/templates/new-clan/.clan-flake @@ -0,0 +1,2 @@ +# DO NOT DELETE +# This file is used by the clan cli to discover a clan flake diff --git a/templates/new-clan/clan-flake-module.nix b/templates/new-clan/clan-flake-module.nix new file mode 100644 index 0000000..cffd20a --- /dev/null +++ b/templates/new-clan/clan-flake-module.nix @@ -0,0 +1,9 @@ +# AUTOMATICALLY GENERATED by clan +{ ... }: { + imports = + let + relPaths = builtins.fromJSON (builtins.readFile ./imports.json); + paths = map (path: ./. + path) relPaths; + in + paths; +} diff --git a/templates/new-clan/default.nix b/templates/new-clan/default.nix deleted file mode 100644 index 4691d64..0000000 --- a/templates/new-clan/default.nix +++ /dev/null @@ -1,16 +0,0 @@ -# This file provides backward compatibility to nix < 2.4 clients -let - flake = - import - ( - let - lock = builtins.fromJSON (builtins.readFile ./flake.lock); - in - fetchTarball { - url = "https://github.com/edolstra/flake-compat/archive/${lock.nodes.flake-compat.locked.rev}.tar.gz"; - sha256 = lock.nodes.flake-compat.locked.narHash; - } - ) - { src = ./.; }; -in -flake.defaultNix diff --git a/templates/new-clan/flake.nix b/templates/new-clan/flake.nix index c710aae..ca463ed 100644 --- a/templates/new-clan/flake.nix +++ b/templates/new-clan/flake.nix @@ -1,25 +1,15 @@ { - description = ""; + description = ""; inputs = { + clan-core.url = "git+https://git.clan.lol/clan/clan-core"; flake-parts.url = "github:hercules-ci/flake-parts"; - flake-compat = { - url = "github:edolstra/flake-compat"; - flake = false; - }; - nixpkgs.url = "nixpkgs/nixos-unstable"; }; outputs = inputs @ { flake-parts, ... }: flake-parts.lib.mkFlake { inherit inputs; } { - - systems = builtins.fromJSON (builtins.readFile ./systems.json); - - imports = - let - relPaths = builtins.fromJSON (builtins.readFile ./imports.json); - paths = map (path: ./. + path) relPaths; - in - paths; + imports = [ + ./clan-flake-module.nix + ]; }; } diff --git a/templates/new-clan/shell.nix b/templates/new-clan/shell.nix deleted file mode 100644 index 804c2a4..0000000 --- a/templates/new-clan/shell.nix +++ /dev/null @@ -1,2 +0,0 @@ -(import ./default.nix).devShells.${builtins.currentSystem}.default - or (throw "dev-shell not defined. Cannot find flake attribute devShell.${builtins.currentSystem}.default")