diff --git a/catmux/session.py b/catmux/session.py index 5cea1aa..ef5c2e6 100644 --- a/catmux/session.py +++ b/catmux/session.py @@ -70,8 +70,8 @@ def init_from_yaml(self, yaml_data): """Initialize config directly by an already loaded yaml structure.""" self.__yaml_data = yaml_data - self._parse_common() self._parse_parameters() + self._parse_common() self._parse_windows() def run(self, parent_server: libtmux.Server, debug=False): @@ -101,11 +101,28 @@ def _parse_common(self): if "common" in self.__yaml_data and self.__yaml_data["common"]: common = self.__yaml_data["common"] if "before_commands" in common and common["before_commands"]: - self._before_commands = common["before_commands"] + # self._before_commands = common["before_commands"] + self._parse_before_commands(common["before_commands"]) if "default_window" in common and common["default_window"]: self._default_window = common["default_window"] + + def _parse_before_commands(self, commands): + command_list = [] + for command in commands: + if isinstance(command, str): + command_list.append(command) + elif isinstance(command, dict): + conditional_param = command.get('if') + print(conditional_param) + if conditional_param not in self._parameters: + raise RuntimeError + elif self._parameters[conditional_param]: + command_list.append(command) + self._before_commands = command_list + + def _parse_overwrites(self, data_string): """Separates a comma-separated list of foo=val1,bar=val2 into a dictionary.""" if data_string is None: diff --git a/test/test_parsing.py b/test/test_parsing.py index 353f4ac..f4d495a 100644 --- a/test/test_parsing.py +++ b/test/test_parsing.py @@ -93,6 +93,29 @@ def test_empty_before_commands(): assert session._default_window == None +def test_conditional_before_commands(): + CONFIG = """common: + before_commands: + - if: param_true + command: echo "true" + - if: param_false_ + command: echo "false" +windows: + - name: left-right + splits: + - commands: + - echo "left" + - name: foo + splits: + - commands: + - echo "left" +""" + session = Session("name") + session.init_from_yaml(yaml.safe_load(CONFIG)) + + assert len(session._before_commands) == 1 + + def test_missing_parameter_for_if_unless(): CONFIG = """common: before_commands: