from ...Logging.Logger import log, Severity
from ...Machine.PaedMLMachineCollection import MACHINE_COLLECTION
from ..Playbooks.Playbook import FileCleanupTask, PlaybookCredentialCollection
from ...SetupSteps.SetupStep import SetupStep


class SetRepositoryUrl(SetupStep):
    def __init__(self, repo_url: str, keyring_path: str, repo: str, repo_file: str):
        super().__init__()
        self.repo_url = repo_url
        self.keyring_path = keyring_path
        self.repo = repo
        self.repo_file = repo_file

    def run(self, file_cleanup_stack : list[FileCleanupTask], credentials: PlaybookCredentialCollection) -> bool:
        machine = MACHINE_COLLECTION.backup
        log('Applying Repository...', Severity.INFO)
        repo_content = f'deb [signed-by={self.keyring_path}] {self.repo_url} {self.repo}'
        machine.write_file(self.repo_file, repo_content)
        return True