Skip to content

Commit 8f6888d

Browse files
authored
Add List Command and Enhance Structure Definition in Generate Command (#8)
#### Overview: This pull request introduces a new command to list available structures and enhances the `generate` command by improving how structure definitions are handled. #### Changes: - **New `list` command**: - Introduces a `ListCommand` that lists available structure definitions from the `contribs` directory. - Files ending with `.yaml` are sorted and displayed. - **Enhancements to `generate` command**: - Refactors the `yaml_file` argument to `structure_definition`. - Adds support for structure definition paths and checks for file existence. - A new optional argument `--structures-path` allows specifying a custom directory for structure definitions. - **Integration in main module**: - The new `list` command is added to the main CLI interface. #### Justification: These changes improve the usability of the `generate` command by allowing more flexible paths for structure definitions and add a helpful listing command for structure files. #### Impact: - The new `list` command provides an easier way for users to find available structure definitions. - The refactored `generate` command ensures better error handling when structure files are not found.
1 parent 6eb83b3 commit 8f6888d

3 files changed

Lines changed: 37 additions & 4 deletions

File tree

struct_module/commands/generate.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@
77
class GenerateCommand(Command):
88
def __init__(self, parser):
99
super().__init__(parser)
10-
parser.add_argument('yaml_file', type=str, help='Path to the YAML configuration file')
10+
parser.add_argument('structure_definition', type=str, help='Path to the YAML configuration file')
1111
parser.add_argument('base_path', type=str, help='Base path where the structure will be created')
12+
parser.add_argument('-s', '--structures-path', type=str, help='Path to structure definitions', default='contribs')
1213
parser.add_argument('-d', '--dry-run', action='store_true', help='Perform a dry run without creating any files or directories')
1314
parser.add_argument('-v', '--vars', type=str, help='Template variables in the format KEY1=value1,KEY2=value2')
1415
parser.add_argument('-b', '--backup', type=str, help='Path to the backup folder')
@@ -17,7 +18,7 @@ def __init__(self, parser):
1718
parser.set_defaults(func=self.execute)
1819

1920
def execute(self, args):
20-
self.logger.info(f"Generating structure at {args.base_path} with config {args.yaml_file}")
21+
self.logger.info(f"Generating structure at {args.base_path} with config {args.structure_definition}")
2122

2223
if args.backup and not os.path.exists(args.backup):
2324
os.makedirs(args.backup)
@@ -30,8 +31,17 @@ def execute(self, args):
3031

3132

3233
def _create_structure(self, args):
33-
with open(args.yaml_file, 'r') as f:
34-
config = yaml.safe_load(f)
34+
if args.structure_definition.startswith("file://") and args.structure_definition.endswith(".yaml"):
35+
with open(args.structure_definition[7:], 'r') as f:
36+
config = yaml.safe_load(f)
37+
else:
38+
file_path = os.path.join(args.structures_path, f"{args.structure_definition}.yaml")
39+
# show error if file is not found
40+
if not os.path.exists(file_path):
41+
self.logger.error(f"File not found: {file_path}")
42+
return
43+
with open(file_path, 'r') as f:
44+
config = yaml.safe_load(f)
3545

3646
template_vars = dict(item.split('=') for item in args.vars.split(',')) if args.vars else None
3747
config_structure = config.get('structure', [])

struct_module/commands/list.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from struct_module.commands import Command
2+
import os
3+
import yaml
4+
from struct_module.file_item import FileItem
5+
6+
# List command class
7+
class ListCommand(Command):
8+
def __init__(self, parser):
9+
super().__init__(parser)
10+
parser.set_defaults(func=self.execute)
11+
12+
def execute(self, args):
13+
self.logger.info(f"Listing available structures")
14+
self._list_structures()
15+
16+
def _list_structures(self):
17+
print("Listing available structures")
18+
sorted_list = [structure for structure in os.listdir('contribs') if structure.endswith('.yaml')]
19+
sorted_list.sort()
20+
for structure in sorted_list:
21+
print(f" - {structure[:-5]}")

struct_module/main.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from struct_module.commands.generate import GenerateCommand
66
from struct_module.commands.info import InfoCommand
77
from struct_module.commands.validate import ValidateCommand
8+
from struct_module.commands.list import ListCommand
89

910

1011

@@ -23,6 +24,7 @@ def main():
2324
InfoCommand(subparsers.add_parser('info', help='Show information about the package'))
2425
ValidateCommand(subparsers.add_parser('validate', help='Validate the YAML configuration file'))
2526
GenerateCommand(subparsers.add_parser('generate', help='Generate the project structure'))
27+
ListCommand(subparsers.add_parser('list', help='List available structures'))
2628

2729
args = parser.parse_args()
2830

0 commit comments

Comments
 (0)