diff --git a/struct_module/commands/generate.py b/struct_module/commands/generate.py index fdae95c..acd5000 100644 --- a/struct_module/commands/generate.py +++ b/struct_module/commands/generate.py @@ -4,6 +4,7 @@ import argparse from struct_module.file_item import FileItem from struct_module.completers import file_strategy_completer +from struct_module.utils import project_path # Generate command class class GenerateCommand(Command): @@ -11,7 +12,7 @@ def __init__(self, parser): super().__init__(parser) parser.add_argument('structure_definition', type=str, help='Path to the YAML configuration file') parser.add_argument('base_path', type=str, help='Base path where the structure will be created') - parser.add_argument('-s', '--structures-path', type=str, help='Path to structure definitions', default='contribs') + parser.add_argument('-s', '--structures-path', type=str, help='Path to structure definitions') parser.add_argument('-d', '--dry-run', action='store_true', help='Perform a dry run without creating any files or directories') parser.add_argument('-v', '--vars', type=str, help='Template variables in the format KEY1=value1,KEY2=value2') parser.add_argument('-b', '--backup', type=str, help='Path to the backup folder') @@ -39,7 +40,11 @@ def _create_structure(self, args): with open(args.structure_definition[7:], 'r') as f: config = yaml.safe_load(f) else: - file_path = os.path.join(args.structures_path, f"{args.structure_definition}.yaml") + if args.structures_path is None: + this_file = os.path.dirname(os.path.realpath(__file__)) + file_path = os.path.join(project_path, "contribs", f"{args.structure_definition}.yaml") + else: + file_path = os.path.join(args.structures_path, f"{args.structure_definition}.yaml") # show error if file is not found if not os.path.exists(file_path): self.logger.error(f"File not found: {file_path}") diff --git a/struct_module/commands/list.py b/struct_module/commands/list.py index d9527ba..89c3a3c 100644 --- a/struct_module/commands/list.py +++ b/struct_module/commands/list.py @@ -2,20 +2,29 @@ import os import yaml from struct_module.file_item import FileItem +from struct_module.utils import project_path # List command class class ListCommand(Command): def __init__(self, parser): super().__init__(parser) + parser.add_argument('-s', '--structures-path', type=str, help='Path to structure definitions') parser.set_defaults(func=self.execute) def execute(self, args): self.logger.info(f"Listing available structures") - self._list_structures() + self._list_structures(args) + + def _list_structures(self, args): + + if args.structures_path is None: + this_file = os.path.dirname(os.path.realpath(__file__)) + final_path = os.path.join(project_path, "contribs") + else: + final_path = os.path.join(args.structures_path) - def _list_structures(self): print("Listing available structures") - sorted_list = [structure for structure in os.listdir('contribs') if structure.endswith('.yaml')] + sorted_list = [structure for structure in os.listdir(final_path) if structure.endswith('.yaml')] sorted_list.sort() for structure in sorted_list: print(f" - {structure[:-5]}") diff --git a/struct_module/utils.py b/struct_module/utils.py index d56294c..bf0c97e 100644 --- a/struct_module/utils.py +++ b/struct_module/utils.py @@ -1,5 +1,5 @@ import yaml - +import os def read_config_file(file_path): with open(file_path, 'r') as f: @@ -12,3 +12,5 @@ def merge_configs(file_config, args): if key in args_dict and args_dict[key] is None: args_dict[key] = value return args_dict + +project_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), "..")