From 2a20f6c45aff0f66f07a01f33d2788b7c78701f5 Mon Sep 17 00:00:00 2001 From: InsanePrawn Date: Thu, 8 Sep 2022 17:25:03 +0200 Subject: [PATCH] config: add Config.enforce_profile_flavour_set() --- config/__init__.py | 2 +- config/state.py | 19 +++++++++++++------ packages/device.py | 2 +- 3 files changed, 15 insertions(+), 8 deletions(-) diff --git a/config/__init__.py b/config/__init__.py index 03ce46a..722e3ee 100644 --- a/config/__init__.py +++ b/config/__init__.py @@ -172,7 +172,7 @@ def cmd_config_init(ctx, sections: list[str] = CONFIG_SECTIONS, non_interactive: config.update(results) if 'profiles' in sections: current_profile = 'default' if 'current' not in config.file.profiles else config.file.profiles.current - new_current, _ = prompt_config('profile.current', default=current_profile, field_type=str) + new_current, _ = prompt_config('profiles.current', default=current_profile, field_type=str) profile, changed = prompt_profile(new_current, create=True) config.update_profile(new_current, profile) if not noop: diff --git a/config/state.py b/config/state.py index ddda0e8..278ff19 100644 --- a/config/state.py +++ b/config/state.py @@ -233,7 +233,8 @@ class ConfigStateHolder: self._profile_cache = resolve_profile(name=name, sparse_profiles=self.file.profiles, resolved=self._profile_cache) return self._profile_cache[name] - def enforce_profile_device_set(self, profile_name: Optional[str] = None, hint_or_set_arch: bool = False) -> Profile: + def _enforce_profile_field(self, field: str, profile_name: Optional[str] = None, hint_or_set_arch: bool = False) -> Profile: + # TODO: device profile_name = profile_name if profile_name is not None else self.file.profiles.current arch_hint = '' if not hint_or_set_arch: @@ -243,20 +244,26 @@ class ConfigStateHolder: 'e.g. `kupferbootstrap packages build --arch x86_64`') if not self.is_loaded(): if not self.file_state.exception: - raise Exception('Error enforcing/ config profile device: config hadnt even been loaded yet.\n' + raise Exception(f'Error enforcing config profile {field}: config hadn\'t even been loaded yet.\n' 'This is a bug in kupferbootstrap!') - raise Exception("Profile device couldn't be resolved because the config file couldn't be loaded.\n" + raise Exception(f"Profile {field} couldn't be resolved because the config file couldn't be loaded.\n" "If the config doesn't exist, try running `kupferbootstrap config init`.\n" f"Error: {self.file_state.exception}") if profile_name and profile_name not in self.file.profiles: raise Exception(f'Unknown profile "{profile_name}". Please run `kupferbootstrap config profile init`{arch_hint}') profile = self.get_profile(profile_name) - if not profile.device: - m = (f'Profile "{profile_name}" has no device configured.\n' - f'Please run `kupferbootstrap config profile init device`{arch_hint}') + if field not in profile or not profile[field]: + m = (f'Profile "{profile_name}" has no {field.upper()} configured.\n' + f'Please run `kupferbootstrap config profile init {field}`{arch_hint}') raise Exception(m) return profile + def enforce_profile_device_set(self, **kwargs) -> Profile: + return self._enforce_profile_field(field='device', **kwargs) + + def enforce_profile_flavour_set(self, **kwargs) -> Profile: + return self._enforce_profile_field(field='flavour', **kwargs) + def get_path(self, path_name: str) -> str: paths = self.file.paths return resolve_path_template(paths[path_name], paths) diff --git a/packages/device.py b/packages/device.py index 9b0b00f..4b5cf51 100644 --- a/packages/device.py +++ b/packages/device.py @@ -146,5 +146,5 @@ def get_device(name: str, pkgbuilds: Optional[dict[str, Pkgbuild]] = None, lazy: def get_profile_device(profile_name: Optional[str] = None, hint_or_set_arch: bool = False): - profile = config.enforce_profile_device_set(profile_name, hint_or_set_arch=hint_or_set_arch) + profile = config.enforce_profile_device_set(profile_name=profile_name, hint_or_set_arch=hint_or_set_arch) return get_device(profile.device)