Skip to content

Commit

Permalink
add -D option
Browse files Browse the repository at this point in the history
  • Loading branch information
badra001 committed Jan 28, 2026
1 parent 09afbe8 commit e1f59c7
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 20 deletions.
13 changes: 13 additions & 0 deletions local-app/python-tools/sso-tools/sso-create-sc-group/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,13 @@ The following variables are exposed to Jinja2 and can be used in your templates

```

**Use of --directory option**:

If you run `./sso-create-sc-group.py -b fin -a billing -g admins`:

* **Default**: Creates folder `fin-billing/` containing `fin-billing-admins.yaml`.
* **With `-D**`: Creates folder `fin-billing-admins/` containing `fin-billing-admins.yaml`.

## Template Variables

You can now use these metadata variables in your templates (e.g., in a file header):
Expand Down Expand Up @@ -133,6 +140,12 @@ To see your local timezone name or offset in your generated files, you can use:

## CHANGELOG

#### v1.0.10

* Added `--directory` (`-D`) flag.
* Refactored directory creation logic to support application-level folders (default) or group-specific folders (`-D`).
* Updated banner output to display both the Group identifier and the Target Directory.

#### v1.0.9

* Added a validation check to ensure at least one `GROUP*.j2` file exists in the `TEMPLATE/` directory before starting.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
#!/usr/bin/env python

import argparse
import shutil
import sys
import os
import glob
from pathlib import Path
from datetime import datetime
from jinja2 import Environment, FileSystemLoader

# Version History:
# 1.0.0 - 1.0.8: Core logic, naming, force, metadata, local timezone
# 1.0.9: Added pre-flight check for GROUP*.j2 template files
__version__ = "1.0.9"
# 1.0.0 - 1.0.9: Core logic, naming, metadata, pre-flight checks.
# 1.0.10: Added --directory (-D) flag to control group inclusion in path.
__version__ = "1.0.10"

def create_sc_group():
parser = argparse.ArgumentParser(description="Automate file creation from templates.")
parser.add_argument("-b", "--business-label", required=True, help="Required business label")
parser.add_argument("-a", "--application-label", help="Optional application label")
parser.add_argument("-b", "--business-label", required=True, help="Required business label (Org)")
parser.add_argument("-a", "--application-label", help="Optional application label (App)")
parser.add_argument("-g", "--group", help="Group name (defaults to current directory name)")
parser.add_argument("-d", "--description", help="Description for the group")
parser.add_argument("-D", "--directory", action="store_true", help="Include group name in the directory path")
parser.add_argument("-f", "--force", action="store_true", help="Overwrite existing directory if it exists")
parser.add_argument("-v", "--version", action="version", version=f"%(prog)s {__version__}")

Expand All @@ -32,40 +33,51 @@ def create_sc_group():
raw_group = args.group if args.group else Path.cwd().name
group_name = raw_group.lower()

# Construct the created_group string (the identifier)
if args.application_label:
created_group = f"{args.business_label}-{args.application_label}-{group_name}".lower()
else:
created_group = f"{args.business_label}-{group_name}".lower()

# 3. Output Startup Banner
# 3. Determine the target directory name based on the -D flag
# If -D is present: {org}-{app}-{group} (which is the created_group)
# If -D is NOT present: {org}-{app} (or just {org} if app is missing)
if args.directory:
target_dir_name = created_group
else:
if args.application_label:
target_dir_name = f"{args.business_label}-{args.application_label}".lower()
else:
target_dir_name = args.business_label.lower()

# 4. Output Startup Banner
print(f"--- {script_name} v{__version__} ---")
print(f"Targeting Group: {created_group}\n")
print(f"Targeting Group: {created_group}")
print(f"Output Directory: {target_dir_name}\n")

# 4. Pre-flight Checks
# 5. Pre-flight Checks
template_dir = Path("TEMPLATE")
if not template_dir.exists():
print(f"Error: {template_dir} directory not found.")
sys.exit(1)

# Check for at least one GROUP*.j2 file
has_template = any(template_dir.glob("GROUP*.j2"))
if not has_template:
if not any(template_dir.glob("GROUP*.j2")):
print(f"Error: group {created_group} not configured for script generation")
sys.exit(1)

# 5. Check for existing target directory
target_dir = Path(created_group)
# 6. Check for existing target directory
target_dir = Path(target_dir_name)
if target_dir.exists() and not args.force:
print(f"Error: Directory '{created_group}' already exists.")
print(f"Error: Directory '{target_dir_name}' already exists.")
print("Use --force or -f to recreate the files.")
sys.exit(1)

target_dir.mkdir(parents=True, exist_ok=True)

# 6. Initialize Jinja2 Environment
# 7. Initialize Jinja2 Environment
env = Environment(loader=FileSystemLoader(str(template_dir)))

# 7. Context variables
# 8. Context variables
render_vars = {
"business_label": args.business_label,
"application_label": args.application_label,
Expand All @@ -77,13 +89,13 @@ def create_sc_group():
"created_time": now
}

# 8. Process files
# 9. Process files
for item in template_dir.iterdir():
if item.is_dir():
continue

if item.suffix == ".j2":
# Logic: GROUP.extension.j2 -> {created_group}.extension
# Files inside the directory still use the full created_group name
new_filename = item.name.replace("GROUP", created_group).replace(".j2", "")
target_path = target_dir / new_filename

Expand All @@ -97,7 +109,7 @@ def create_sc_group():
shutil.copy2(item, target_dir / item.name)
print(f" [Copied] {item.name}")

print(f"\nDone. Files located in: ./{created_group}")
print(f"\nDone. Files located in: ./{target_dir_name}")

if __name__ == "__main__":
create_sc_group()

0 comments on commit e1f59c7

Please sign in to comment.