Skip to content

Commit 5f5cf03

Browse files
committed
Add support for loading custom 'kathara.conf' from lab directory (#340)
Introduce functionality to load a custom 'kathara.conf' file from the lab directory if it exists. This ensures commands (lstart, lclean, lrestart, linfo, exec, connect) to use lab-specific configurations seamlessly.
1 parent e62577a commit 5f5cf03

File tree

7 files changed

+37
-0
lines changed

7 files changed

+37
-0
lines changed

src/Kathara/cli/command/ConnectCommand.py

+4
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,10 @@ def run(self, current_path: str, argv: List[str]) -> int:
6565
else:
6666
lab_path = args['directory'].replace('"', '').replace("'", '') if args['directory'] else current_path
6767
lab_path = utils.get_absolute_path(lab_path)
68+
69+
# Load custom 'kathara.conf' if it exists
70+
self._load_custom_configuration(lab_path)
71+
6872
try:
6973
lab = LabParser.parse(lab_path)
7074
except (Exception, IOError):

src/Kathara/cli/command/ExecCommand.py

+4
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,10 @@ def run(self, current_path: str, argv: List[str]) -> int:
8282
else:
8383
lab_path = args['directory'].replace('"', '').replace("'", '') if args['directory'] else current_path
8484
lab_path = utils.get_absolute_path(lab_path)
85+
86+
# Load custom 'kathara.conf' if it exists
87+
self._load_custom_configuration(lab_path)
88+
8589
try:
8690
lab = LabParser.parse(lab_path)
8791
except (Exception, IOError):

src/Kathara/cli/command/LcleanCommand.py

+4
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ def run(self, current_path: str, argv: List[str]) -> int:
5454

5555
lab_path = args['directory'].replace('"', '').replace("'", '') if args['directory'] else current_path
5656
lab_path = utils.get_absolute_path(lab_path)
57+
58+
# Load custom 'kathara.conf' if it exists
59+
self._load_custom_configuration(lab_path)
60+
5761
try:
5862
lab = LabParser.parse(lab_path)
5963
except (Exception, IOError):

src/Kathara/cli/command/LconfigCommand.py

+3
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@ def run(self, current_path: str, argv: List[str]) -> int:
6565
lab_path = args['directory'].replace('"', '').replace("'", '') if args['directory'] else current_path
6666
lab_path = utils.get_absolute_path(lab_path)
6767

68+
# Load custom 'kathara.conf' if it exists
69+
self._load_custom_configuration(lab_path)
70+
6871
lab = LabParser.parse(lab_path)
6972

7073
Kathara.get_instance().update_lab_from_api(lab)

src/Kathara/cli/command/LinfoCommand.py

+4
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ def run(self, current_path: str, argv: List[str]) -> int:
7676

7777
lab_path = args['directory'].replace('"', '').replace("'", '') if args['directory'] else current_path
7878
lab_path = utils.get_absolute_path(lab_path)
79+
80+
# Load custom 'kathara.conf' if it exists
81+
self._load_custom_configuration(lab_path)
82+
7983
try:
8084
lab = LabParser.parse(lab_path)
8185
except (Exception, IOError):

src/Kathara/cli/command/LstartCommand.py

+3
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,9 @@ def run(self, current_path: str, argv: List[str]) -> int:
149149
lab_path = args['directory'].replace('"', '').replace("'", '') if args['directory'] else current_path
150150
lab_path = utils.get_absolute_path(lab_path)
151151

152+
# Load custom 'kathara.conf' if it exists
153+
self._load_custom_configuration(lab_path)
154+
152155
Setting.get_instance().open_terminals = args['terminals'] if args['terminals'] is not None \
153156
else Setting.get_instance().open_terminals
154157
Setting.get_instance().terminal = args['xterm'] or Setting.get_instance().terminal

src/Kathara/foundation/cli/command/Command.py

+15
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1+
import logging
2+
import os
13
from abc import ABC, abstractmethod
24
from typing import List, Dict, Any
35

46
from rich.console import Console
57
from rich.theme import Theme
68

9+
from Kathara.setting.Setting import Setting
710
from ..CliArgs import CliArgs
811

912

@@ -33,3 +36,15 @@ def parse_args(self, argv: List[str]) -> None:
3336
@staticmethod
3437
def get_args() -> Dict[str, Any]:
3538
return CliArgs.get_instance().args
39+
40+
@staticmethod
41+
def _load_custom_configuration(lab_path: str) -> None:
42+
"""Load custom configuration from the given lab path if it exists.
43+
44+
Args:
45+
lab_path (str): Path to the lab directory
46+
"""
47+
custom_conf_path = os.path.join(lab_path, 'kathara.conf')
48+
if os.path.exists(custom_conf_path):
49+
logging.info(f'Loading custom configuration file: {custom_conf_path} ...')
50+
Setting.get_instance().load_from_disk(lab_path)

0 commit comments

Comments
 (0)