vars: print() -> log.info()

This commit is contained in:
DavHau
2024-12-04 13:08:36 +07:00
parent f8bf918415
commit 37ad4eaad5
2 changed files with 34 additions and 27 deletions

View File

@@ -95,15 +95,15 @@ class StoreBase(ABC):
new_file = self._set(generator, var, value) new_file = self._set(generator, var, value)
action_str = "Migrated" if is_migration else "Updated" action_str = "Migrated" if is_migration else "Updated"
if self.is_secret_store: if self.is_secret_store:
print(f"{action_str} secret var {generator.name}/{var.name}\n") log.info(f"{action_str} secret var {generator.name}/{var.name}\n")
else: else:
if value != old_val: if value != old_val:
msg = f"{action_str} var {generator.name}/{var.name}" msg = f"{action_str} var {generator.name}/{var.name}"
if not is_migration: if not is_migration:
msg += f"\n old: {old_val_str}\n new: {string_repr(value)}" msg += f"\n old: {old_val_str}\n new: {string_repr(value)}"
print(msg) log.info(msg)
else: else:
print( log.info(
f"Var {generator.name}/{var.name} remains unchanged: {old_val_str}" f"Var {generator.name}/{var.name} remains unchanged: {old_val_str}"
) )
return new_file return new_file

View File

@@ -1,4 +1,5 @@
import json import json
import logging
import shutil import shutil
from pathlib import Path from pathlib import Path
@@ -18,7 +19,6 @@ from clan_cli.vars.secret_modules import password_store, sops
from clan_cli.vars.set import set_var from clan_cli.vars.set import set_var
from fixtures_flakes import ClanFlake from fixtures_flakes import ClanFlake
from helpers import cli from helpers import cli
from stdout import CaptureOutput
def test_dependencies_as_files(temp_dir: Path) -> None: def test_dependencies_as_files(temp_dir: Path) -> None:
@@ -732,8 +732,8 @@ def test_default_value(
def test_stdout_of_generate( def test_stdout_of_generate(
monkeypatch: pytest.MonkeyPatch, monkeypatch: pytest.MonkeyPatch,
flake: ClanFlake, flake: ClanFlake,
capture_output: CaptureOutput,
sops_setup: SopsSetup, sops_setup: SopsSetup,
caplog: pytest.LogCaptureFixture,
) -> None: ) -> None:
config = flake.machines["my_machine"] config = flake.machines["my_machine"]
config["nixpkgs"]["hostPlatform"] = "x86_64-linux" config["nixpkgs"]["hostPlatform"] = "x86_64-linux"
@@ -750,7 +750,8 @@ def test_stdout_of_generate(
sops_setup.init() sops_setup.init()
from clan_cli.vars.generate import generate_vars_for_machine from clan_cli.vars.generate import generate_vars_for_machine
with capture_output as output: # with capture_output as output:
with caplog.at_level(logging.INFO):
generate_vars_for_machine( generate_vars_for_machine(
Machine(name="my_machine", flake=FlakeId(str(flake.path))), Machine(name="my_machine", flake=FlakeId(str(flake.path))),
"my_generator", "my_generator",
@@ -758,55 +759,61 @@ def test_stdout_of_generate(
fix=False, fix=False,
) )
assert "Updated var my_generator/my_value" in output.out assert "Updated var my_generator/my_value" in caplog.text
assert "old: <not set>" in output.out assert "old: <not set>" in caplog.text
assert "new: hello" in output.out assert "new: hello" in caplog.text
caplog.clear()
set_var("my_machine", "my_generator/my_value", b"world", FlakeId(str(flake.path))) set_var("my_machine", "my_generator/my_value", b"world", FlakeId(str(flake.path)))
with capture_output as output: with caplog.at_level(logging.INFO):
generate_vars_for_machine( generate_vars_for_machine(
Machine(name="my_machine", flake=FlakeId(str(flake.path))), Machine(name="my_machine", flake=FlakeId(str(flake.path))),
"my_generator", "my_generator",
regenerate=True, regenerate=True,
fix=False, fix=False,
) )
assert "Updated var my_generator/my_value" in output.out assert "Updated var my_generator/my_value" in caplog.text
assert "old: world" in output.out assert "old: world" in caplog.text
assert "new: hello" in output.out assert "new: hello" in caplog.text
caplog.clear()
# check the output when nothing gets regenerated # check the output when nothing gets regenerated
with capture_output as output: with caplog.at_level(logging.INFO):
generate_vars_for_machine( generate_vars_for_machine(
Machine(name="my_machine", flake=FlakeId(str(flake.path))), Machine(name="my_machine", flake=FlakeId(str(flake.path))),
"my_generator", "my_generator",
regenerate=True, regenerate=True,
fix=False, fix=False,
) )
assert "Updated" not in output.out assert "Updated" not in caplog.text
assert "hello" in output.out assert "hello" in caplog.text
with capture_output as output: caplog.clear()
with caplog.at_level(logging.INFO):
generate_vars_for_machine( generate_vars_for_machine(
Machine(name="my_machine", flake=FlakeId(str(flake.path))), Machine(name="my_machine", flake=FlakeId(str(flake.path))),
"my_secret_generator", "my_secret_generator",
regenerate=False, regenerate=False,
fix=False, fix=False,
) )
assert "Updated secret var my_secret_generator/my_secret" in output.out assert "Updated secret var my_secret_generator/my_secret" in caplog.text
assert "hello" not in output.out assert "hello" not in caplog.text
caplog.clear()
set_var( set_var(
"my_machine", "my_machine",
"my_secret_generator/my_secret", "my_secret_generator/my_secret",
b"world", b"world",
FlakeId(str(flake.path)), FlakeId(str(flake.path)),
) )
with capture_output as output: with caplog.at_level(logging.INFO):
generate_vars_for_machine( generate_vars_for_machine(
Machine(name="my_machine", flake=FlakeId(str(flake.path))), Machine(name="my_machine", flake=FlakeId(str(flake.path))),
"my_secret_generator", "my_secret_generator",
regenerate=True, regenerate=True,
fix=False, fix=False,
) )
assert "Updated secret var my_secret_generator/my_secret" in output.out assert "Updated secret var my_secret_generator/my_secret" in caplog.text
assert "world" not in output.out assert "world" not in caplog.text
assert "hello" not in output.out assert "hello" not in caplog.text
caplog.clear()
@pytest.mark.with_core @pytest.mark.with_core
@@ -842,7 +849,7 @@ def test_migration(
monkeypatch: pytest.MonkeyPatch, monkeypatch: pytest.MonkeyPatch,
flake: ClanFlake, flake: ClanFlake,
sops_setup: SopsSetup, sops_setup: SopsSetup,
capture_output: CaptureOutput, caplog: pytest.LogCaptureFixture,
) -> None: ) -> None:
config = flake.machines["my_machine"] config = flake.machines["my_machine"]
config["nixpkgs"]["hostPlatform"] = "x86_64-linux" config["nixpkgs"]["hostPlatform"] = "x86_64-linux"
@@ -861,10 +868,10 @@ def test_migration(
monkeypatch.chdir(flake.path) monkeypatch.chdir(flake.path)
sops_setup.init() sops_setup.init()
cli.run(["facts", "generate", "--flake", str(flake.path), "my_machine"]) cli.run(["facts", "generate", "--flake", str(flake.path), "my_machine"])
with capture_output as output: with caplog.at_level(logging.INFO):
cli.run(["vars", "generate", "--flake", str(flake.path), "my_machine"]) cli.run(["vars", "generate", "--flake", str(flake.path), "my_machine"])
assert "Migrated var my_generator/my_value" in output.out assert "Migrated var my_generator/my_value" in caplog.text
assert "Migrated secret var my_generator/my_secret" in output.out assert "Migrated secret var my_generator/my_secret" in caplog.text
in_repo_store = in_repo.FactStore( in_repo_store = in_repo.FactStore(
Machine(name="my_machine", flake=FlakeId(str(flake.path))) Machine(name="my_machine", flake=FlakeId(str(flake.path)))
) )