Skip to content

Commit da51d5c

Browse files
authored
πŸ”§ Automatic file:// protocol handling and devcontainer improvements (#68)
## 🎯 Overview This PR introduces automatic file protocol handling and improves the development container configuration for better developer experience. ## ✨ What's Changed ### πŸ”§ File Protocol Enhancement - **Automatic `file://` prepending** - YAML files without protocol automatically get `file://` prefix - **Simplified usage** - Users can now specify YAML files directly without remembering protocol syntax - **Backward compatibility** - Existing `file://` prefixed paths continue to work unchanged ### 🐳 DevContainer Improvements - **Streamlined Python setup** - Commented out redundant Python feature (base image already includes Python) - **OpenAI API integration** - Added environment variable for AI-powered features - **Better development experience** - Optimized container configuration for faster startup ## πŸ“– Usage Examples ### Before (Manual Protocol) ```bash # Users had to remember the file:// protocol struct generate file://my-config.yaml ./output ``` ### After (Automatic Protocol) ```bash # Protocol is automatically added for .yaml files struct generate my-config.yaml ./output # Still works with explicit protocol struct generate file://my-config.yaml ./output ``` ## πŸ”§ Technical Implementation ### File Protocol Logic - Checks if file ends with `.yaml` and doesn't start with `file://` - Automatically prepends `file://` protocol when conditions are met - Maintains existing behavior for all other cases ### DevContainer Optimizations - Removed redundant Python feature installation - Added OpenAI API key environment variable for AI integrations - Preserved all existing extensions and settings ## βœ… Benefits ### For End Users - **Simplified commands** - No need to remember file protocol syntax - **Reduced errors** - Automatic protocol handling prevents common mistakes - **Consistent experience** - Works the same way regardless of protocol specification ### For Developers - **Faster container startup** - Removed redundant Python installation - **AI integration ready** - OpenAI API key environment configured - **Better DX** - Streamlined development environment ## πŸ§ͺ Testing - βœ… Verified automatic protocol prepending works correctly - βœ… Confirmed backward compatibility with explicit protocols - βœ… Tested devcontainer builds successfully with new configuration - βœ… Validated all existing functionality remains intact ## πŸ“ Files Modified - **`struct_module/commands/generate.py`** - Added automatic file protocol handling - **`.devcontainer/devcontainer.json`** - Optimized container configuration ## πŸ”— Compatibility - **βœ… Backward Compatible** - All existing commands continue to work - **βœ… Protocol Agnostic** - Supports both explicit and automatic protocol handling - **βœ… Environment Ready** - Enhanced development container setup --- This enhancement improves user experience by removing the need to manually specify file protocols while maintaining full backward compatibility and improving the development environment.
1 parent 0b53db6 commit da51d5c

8 files changed

Lines changed: 34 additions & 9 deletions

File tree

β€Ž.devcontainer/devcontainer.jsonβ€Ž

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "Struct devcontainer",
33
"image": "mcr.microsoft.com/devcontainers/python:3",
44
"features": {
5-
"ghcr.io/devcontainers/features/python:1": {},
5+
// "ghcr.io/devcontainers/features/python:1": {},
66
"ghcr.io/gvatsal60/dev-container-features/pre-commit:1": {}
77
},
88
"postCreateCommand": "bash ./scripts/devcontainer_start.sh",
@@ -30,5 +30,9 @@
3030
"mounts": [
3131
"type=bind,source=${localWorkspaceFolder},target=/work",
3232
"type=bind,source=/home/${localEnv:USER}/.ssh,target=/home/vscode/.ssh"
33-
]
33+
],
34+
// environment variables to be set in the container
35+
"remoteEnv": {
36+
"OPENAI_API_KEY": "set-key-here"
37+
}
3438
}

β€Ždocs/configuration.mdβ€Ž

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ When defining your project structure in the YAML configuration file, you can use
1010
- **content**: Define the content of the file directly in the YAML configuration.
1111
- **file**: Specify a local or remote file to include. Supported protocols include `file://`, `http://`, `https://`, `github://`, `githubhttps://`, `githubssh://`, `s3://`, and `gs://`.
1212

13+
> **Note**: For local `.yaml` files, the `file://` protocol is automatically added if not specified.
14+
1315
Example:
1416

1517
```yaml

β€Ždocs/development.mdβ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ pytest tests/integration/
178178
### Enable Debug Logging
179179

180180
```sh
181-
struct --log=DEBUG generate file://my-config.yaml ./output
181+
struct --log=DEBUG generate my-config.yaml ./output
182182
```
183183

184184
### Use Python Debugger

β€Ždocs/file-handling.mdβ€Ž

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,15 +138,17 @@ Control how STRUCT handles existing files with the `--file-strategy` option:
138138

139139
```sh
140140
# Skip existing files
141-
struct generate --file-strategy=skip file://my-config.yaml ./output
141+
struct generate --file-strategy=skip my-config.yaml ./output
142142
143143
# Backup existing files
144-
struct generate --file-strategy=backup --backup=/tmp/backup file://my-config.yaml ./output
144+
struct generate --file-strategy=backup --backup=/tmp/backup my-config.yaml ./output
145145
146146
# Rename existing files
147-
struct generate --file-strategy=rename file://my-config.yaml ./output
147+
struct generate --file-strategy=rename my-config.yaml ./output
148148
```
149149

150+
> **Note**: The `file://` protocol is automatically added for `.yaml` files, so these examples work with or without the explicit protocol.
151+
150152
## Advanced Examples
151153

152154
### Conditional File Creation

β€Ždocs/mappings.mdβ€Ž

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,8 @@ This approach allows you to pass specific mapping values as variables to nested
146146
Use the `--mappings-file` argument with the `generate` command:
147147

148148
```sh
149+
# Both commands work identically - file:// is automatically added for .yaml files
150+
struct generate --mappings-file ./mymap.yaml my-struct.yaml .
149151
struct generate --mappings-file ./mymap.yaml file://my-struct.yaml .
150152
```
151153

@@ -157,7 +159,7 @@ You can specify multiple mappings files that will be merged in order:
157159
struct generate \
158160
--mappings-file ./common-mappings.yaml \
159161
--mappings-file ./env-specific-mappings.yaml \
160-
file://my-struct.yaml .
162+
my-struct.yaml .
161163
```
162164

163165
**Merging behavior:**
@@ -173,7 +175,7 @@ struct generate \
173175
struct generate \
174176
--mappings-file ./mappings/common.yaml \
175177
--mappings-file ./mappings/${ENVIRONMENT}.yaml \
176-
file://infrastructure.yaml \
178+
infrastructure.yaml \
177179
./output
178180
```
179181

β€Ždocs/quickstart.mdβ€Ž

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,11 @@ mkdir example
4040
cd example/
4141
touch structure.yaml
4242
vim structure.yaml # copy the content from the example folder
43-
struct generate file://structure.yaml .
43+
struct generate structure.yaml .
4444
```
4545

46+
> **Note**: The `file://` protocol is automatically added for `.yaml` files, so `structure.yaml` and `file://structure.yaml` work identically.
47+
4648
## First Example
4749

4850
After installing STRUCT, try this simple example:

β€Ždocs/usage.mdβ€Ž

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,16 @@ struct -h
2424
struct generate terraform-module ./my-terraform-module
2525
```
2626

27+
### YAML File Usage
28+
29+
For local YAML configuration files, the `file://` protocol is automatically added:
30+
31+
```sh
32+
# Both of these work identically
33+
struct generate my-config.yaml ./output
34+
struct generate file://my-config.yaml ./output
35+
```
36+
2737
### Complete Example
2838

2939
```sh

β€Žstruct_module/commands/generate.pyβ€Ž

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ def _run_hooks(self, hooks, hook_type="pre"): # helper for running hooks
6262
return True
6363

6464
def _load_yaml_config(self, structure_definition, structures_path):
65+
if structure_definition.endswith(".yaml") and not structure_definition.startswith("file://"):
66+
structure_definition = f"file://{structure_definition}"
67+
6568
if structure_definition.startswith("file://") and structure_definition.endswith(".yaml"):
6669
with open(structure_definition[7:], 'r') as f:
6770
return yaml.safe_load(f)

0 commit comments

Comments
Β (0)