belt 0.2.10 → 0.2.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +63 -0
- data/README.md +169 -0
- data/lib/belt/action_router.rb +32 -4
- data/lib/belt/cli/app_detection.rb +19 -5
- data/lib/belt/cli/contracts_command.rb +142 -0
- data/lib/belt/cli/destroy_command.rb +193 -10
- data/lib/belt/cli/environment_command.rb +32 -0
- data/lib/belt/cli/generate_command.rb +5 -5
- data/lib/belt/cli/new_command.rb +2 -2
- data/lib/belt/cli/plugin_command.rb +221 -0
- data/lib/belt/cli/routes_command/schema_loader.rb +17 -7
- data/lib/belt/cli/routes_command.rb +7 -3
- data/lib/belt/cli/setup_command.rb +1 -1
- data/lib/belt/cli/views_command.rb +7 -2
- data/lib/belt/cli.rb +7 -0
- data/lib/belt/helpers/cors_origin.rb +19 -2
- data/lib/belt/helpers/response.rb +13 -2
- data/lib/belt/lambda_handler.rb +14 -5
- data/lib/belt/root.rb +12 -3
- data/lib/belt/route_dsl.rb +2 -2
- data/lib/belt/version.rb +1 -1
- data/lib/templates/environment/backend.tf.erb +1 -2
- data/lib/templates/frontend_infra/frontend.tf.erb +2 -1
- data/lib/templates/module/frontend.tf.erb +2 -1
- data/lib/templates/module/main.tf.erb +1 -1
- data/lib/templates/new_app/AGENTS.md.erb +4 -4
- data/lib/templates/new_app/README.md.erb +2 -2
- data/lib/templates/plugin/AGENTS.md.erb +135 -0
- data/lib/templates/plugin/CHANGELOG.md.erb +5 -0
- data/lib/templates/plugin/Gemfile.erb +11 -0
- data/lib/templates/plugin/LICENSE.erb +21 -0
- data/lib/templates/plugin/README.md.erb +63 -0
- data/lib/templates/plugin/Rakefile.erb +7 -0
- data/lib/templates/plugin/gemspec.erb +26 -0
- data/lib/templates/plugin/gitignore.erb +11 -0
- data/lib/templates/plugin/lib/belt/generators/generator.rb.erb +122 -0
- data/lib/templates/plugin/lib/belt/module/configuration.rb.erb +15 -0
- data/lib/templates/plugin/lib/belt/module/version.rb.erb +7 -0
- data/lib/templates/plugin/lib/belt/module.rb.erb +27 -0
- data/lib/templates/plugin/lib/entry.rb.erb +3 -0
- data/lib/templates/plugin/rspec.erb +3 -0
- data/lib/templates/plugin/spec/configuration_spec.rb.erb +17 -0
- data/lib/templates/plugin/spec/spec_helper.rb.erb +19 -0
- metadata +21 -3
- /data/lib/templates/new_app/config/{schema.tf.rb.erb → contracts.rb.erb} +0 -0
- /data/lib/templates/new_app/config/{routes.tf.rb.erb → routes.rb.erb} +0 -0
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 10e92ad2ab3fe79f48f07cca6495ce8b9ccdad09755c7a6517e651deadcc118d
|
|
4
|
+
data.tar.gz: 6cd8a8ee7ad1023fb949183122c3aca465c216cf91d175e8f9395e8f413ad880
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: cb7d5dacc2b90e0ae7c4dd9853d76ea42ef8d6e324d30edd91c5fbe6d934affafd2c3bee6fb752960d9c8462a59663244ae448797ab7e57de7ad05c50afa627f
|
|
7
|
+
data.tar.gz: fd7f4ff9af68c36b34c02c2f4af2c42649f0e9494ac192eaaefeae3111e0da81ced4091b88510b82ac183fefaadfd0677e4d34f270e16a7dcf389b54259e8c52
|
data/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,68 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 0.2.12
|
|
4
|
+
|
|
5
|
+
### Rename: `routes.tf.rb` → `routes.rb`, `schema.tf.rb` → `contracts.rb`
|
|
6
|
+
|
|
7
|
+
The `.tf.rb` extension was a vestige of when these files produced HCL output — that hasn't been the case for a while. New apps now get clean, Rails-familiar names:
|
|
8
|
+
|
|
9
|
+
- `config/routes.rb` — API route definitions
|
|
10
|
+
- `config/contracts.rb` — API request/response contracts
|
|
11
|
+
|
|
12
|
+
**Backward compatible:** All detection helpers (`Belt.root`, `Belt.routes_file`, `Belt.contracts_file`, `find_routes_file_path`, `find_contracts_file_path`) check new names first, then fall back to `*.tf.rb` and `infrastructure/` paths. Existing apps continue working without changes.
|
|
13
|
+
|
|
14
|
+
### New command: `belt contracts`
|
|
15
|
+
|
|
16
|
+
Dedicated CLI command for inspecting API contracts, separate from `belt routes`:
|
|
17
|
+
|
|
18
|
+
```bash
|
|
19
|
+
belt contracts # Human-readable table of request/response models
|
|
20
|
+
belt contracts -f json # JSON output (for tooling/CI)
|
|
21
|
+
belt contracts -g post # Filter by pattern
|
|
22
|
+
belt contracts --file path.rb # Explicit file override
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
Previously, contracts were only accessible as a side-effect of `belt routes -f json --schema <file>`. Now they have their own first-class command. `belt routes -f json` continues to include models for backward compatibility.
|
|
26
|
+
|
|
27
|
+
### Other changes
|
|
28
|
+
|
|
29
|
+
- `Belt.schema_file` is now an alias for `Belt.contracts_file`
|
|
30
|
+
- `find_schema_file_path` is now an alias for `find_contracts_file_path`
|
|
31
|
+
- Module template updated: `source` points to `config/routes.rb`
|
|
32
|
+
- All scaffold/generator help text and templates reference new filenames
|
|
33
|
+
|
|
34
|
+
## Unreleased
|
|
35
|
+
|
|
36
|
+
## 0.2.11
|
|
37
|
+
|
|
38
|
+
### Security hardening
|
|
39
|
+
|
|
40
|
+
- **CORS origin validation**: format validation (scheme required, no paths/queries/fragments), length limit (253 chars) to prevent ReDoS, restrict wildcard matching to valid subdomain chars `[a-z0-9-]`.
|
|
41
|
+
- **Security headers** on all responses: `X-Content-Type-Options: nosniff`, `Vary: Origin`; HTML responses also get `X-Frame-Options: DENY` and `Referrer-Policy: strict-origin-when-cross-origin`.
|
|
42
|
+
- **Path traversal protection** in `ActionRouter`: strict regex for controller names, reject `..` sequences / absolute paths / backslashes, `realpath` validation in `resolve_from_paths`.
|
|
43
|
+
- **Request body size limit** (10 MB): returns 413 for oversized bodies; proper 400 for invalid JSON.
|
|
44
|
+
|
|
45
|
+
### Safe `belt destroy environment`
|
|
46
|
+
|
|
47
|
+
Previously `belt destroy environment <name>` immediately deleted the infrastructure directory with no checks — leaving orphaned AWS resources.
|
|
48
|
+
|
|
49
|
+
Now the command:
|
|
50
|
+
1. Checks if Terraform state exists (local `.terraform` dir or remote state)
|
|
51
|
+
2. If state found, prompts user to run `terraform destroy` first
|
|
52
|
+
3. Requires explicit confirmation before deleting files
|
|
53
|
+
4. Supports `--force` (skip prompts) and `--skip-terraform` flags
|
|
54
|
+
|
|
55
|
+
### Environment generator state bucket fix
|
|
56
|
+
|
|
57
|
+
The environment generator was writing `belt-terraform-state` as the backend bucket name, missing the account-ID suffix. Now resolves via sibling `backend.tf`, STS `get-caller-identity`, or placeholder (for later `belt setup state` patch).
|
|
58
|
+
|
|
59
|
+
### Contributing & plugins
|
|
60
|
+
|
|
61
|
+
- README: **Plugins** and **Contributing** sections — how to contribute to belt, how plugins register via `GeneratorRegistry`, layout used by `belt-messaging` / `belt-pay`, generator checklist for humans and agents.
|
|
62
|
+
- **`belt plugin new <name>`** — scaffold a new plugin gem (gemspec, `Belt::<Name>` module, generator stub, RSpec, README, AGENTS.md), similar to `rails plugin new`.
|
|
63
|
+
- **`AGENTS.md`** at the belt gem root — agent-oriented map of core layout, CLI, and the plugin/GeneratorRegistry contract (complements README).
|
|
64
|
+
- Plugin scaffold includes **`AGENTS.md`** so new plugins match the agent guidance pattern already used by `belt new` apps.
|
|
65
|
+
|
|
3
66
|
## 0.2.10
|
|
4
67
|
|
|
5
68
|
### Quiet `belt new` (with optional verbose)
|
data/README.md
CHANGED
|
@@ -575,6 +575,175 @@ These are set via Terraform variables in each environment's `terraform.tfvars`.
|
|
|
575
575
|
|
|
576
576
|
The backup phase reads table names from Terraform outputs. On a brand-new environment that has never been deployed, there are no outputs yet — Belt will warn and skip the backup phase gracefully. After the first successful deploy, backups run normally on subsequent deploys.
|
|
577
577
|
|
|
578
|
+
## Plugins
|
|
579
|
+
|
|
580
|
+
Belt is designed to stay lean. Optional capabilities ship as **separate gems** that plug into the CLI and runtime the same way Rails engines and generators do.
|
|
581
|
+
|
|
582
|
+
### Official / example plugins
|
|
583
|
+
|
|
584
|
+
| Gem | Purpose | Status |
|
|
585
|
+
|-----|---------|--------|
|
|
586
|
+
| [`belt-messaging`](https://github.com/stowzilla/belt-messaging) | Two-way SMS via AWS End User Messaging (Pinpoint) | Early (not production-hardened) |
|
|
587
|
+
| [`belt-pay`](https://github.com/stowzilla/belt-pay) | Stripe payments & subscriptions | Early (not production-hardened) |
|
|
588
|
+
|
|
589
|
+
Install a plugin in a Belt app:
|
|
590
|
+
|
|
591
|
+
```ruby
|
|
592
|
+
# Gemfile
|
|
593
|
+
gem "belt-messaging"
|
|
594
|
+
# gem "belt-pay"
|
|
595
|
+
```
|
|
596
|
+
|
|
597
|
+
```bash
|
|
598
|
+
bundle install
|
|
599
|
+
belt generate messaging # or: belt g pay
|
|
600
|
+
belt generate --help # lists built-ins + gem generators
|
|
601
|
+
```
|
|
602
|
+
|
|
603
|
+
### How plugins register
|
|
604
|
+
|
|
605
|
+
No central registry file, no initializer hook. Belt discovers generators by scanning loaded gems:
|
|
606
|
+
|
|
607
|
+
1. Gem is in the app `Gemfile` and bundled
|
|
608
|
+
2. Gem ships a file at `lib/belt/generators/<name>_generator.rb`
|
|
609
|
+
3. Class lives in `Belt::Generators`, named `<Name>Generator`
|
|
610
|
+
4. Implements `.run(args)` (required), `.destroy(args)` and `.description` (optional)
|
|
611
|
+
|
|
612
|
+
That is the whole contract. After `bundle install`, `belt generate <name>` and `belt destroy <name>` just work.
|
|
613
|
+
|
|
614
|
+
See `Belt::CLI::GeneratorRegistry` and the CHANGELOG entry for **0.1.13 Generator Extension API**.
|
|
615
|
+
|
|
616
|
+
### Plugin layout (canonical)
|
|
617
|
+
|
|
618
|
+
```
|
|
619
|
+
belt-messaging/
|
|
620
|
+
├── belt-messaging.gemspec
|
|
621
|
+
├── lib/
|
|
622
|
+
│ ├── belt-messaging.rb # require entrypoint
|
|
623
|
+
│ └── belt/
|
|
624
|
+
│ ├── messaging.rb # Belt::Messaging API
|
|
625
|
+
│ ├── messaging/
|
|
626
|
+
│ │ ├── configuration.rb
|
|
627
|
+
│ │ ├── version.rb
|
|
628
|
+
│ │ ├── controllers/ # default controllers (optional)
|
|
629
|
+
│ │ └── templates/ # ERB templates for the generator
|
|
630
|
+
│ │ ├── terraform/
|
|
631
|
+
│ │ ├── lambda/
|
|
632
|
+
│ │ ├── config/
|
|
633
|
+
│ │ └── controllers/
|
|
634
|
+
│ └── generators/
|
|
635
|
+
│ └── messaging_generator.rb # ← auto-discovered
|
|
636
|
+
└── spec/
|
|
637
|
+
```
|
|
638
|
+
|
|
639
|
+
**Runtime code stays in the gem.** Generators copy only what the host app must own (Terraform modules, Lambda entrypoints, optional controller overrides). Prefer gem defaults + `belt g <plugin> --controllers` over dumping everything into the app (same idea as `rails g devise:views`).
|
|
640
|
+
|
|
641
|
+
### Creating a new plugin
|
|
642
|
+
|
|
643
|
+
Scaffold a ready-to-fill gem (Rails-style `plugin new`):
|
|
644
|
+
|
|
645
|
+
```bash
|
|
646
|
+
belt plugin new notifications
|
|
647
|
+
# → ./belt-notifications/
|
|
648
|
+
|
|
649
|
+
belt plugin new pay --path ~/Code --summary "Stripe payments for Belt"
|
|
650
|
+
# → ~/Code/belt-pay/
|
|
651
|
+
```
|
|
652
|
+
|
|
653
|
+
Then:
|
|
654
|
+
|
|
655
|
+
```bash
|
|
656
|
+
cd belt-notifications
|
|
657
|
+
bundle install
|
|
658
|
+
# implement lib/belt/notifications/* and the generator
|
|
659
|
+
```
|
|
660
|
+
|
|
661
|
+
Point a Belt app at it while developing:
|
|
662
|
+
|
|
663
|
+
```ruby
|
|
664
|
+
# In the app Gemfile
|
|
665
|
+
gem "belt-notifications", path: "../belt-notifications"
|
|
666
|
+
```
|
|
667
|
+
|
|
668
|
+
```bash
|
|
669
|
+
bundle install
|
|
670
|
+
belt generate notifications
|
|
671
|
+
```
|
|
672
|
+
|
|
673
|
+
When packaging Lambdas, `belt deploy` vendors path gems into `vendor/cache` so conveyor-belt can package them.
|
|
674
|
+
|
|
675
|
+
### Generator checklist (for humans and agents)
|
|
676
|
+
|
|
677
|
+
A solid plugin generator typically:
|
|
678
|
+
|
|
679
|
+
1. **Terraform module** → `infrastructure/modules/<name>/` (`main.tf`, `variables.tf`, `outputs.tf`)
|
|
680
|
+
2. **Lambda config** → `config/lambda/<name>.yml` (timeout, memory, env, triggers)
|
|
681
|
+
3. **Lambda entrypoint** → `lambda/<name>.rb` using `Belt::LambdaHandler`
|
|
682
|
+
4. **Routes / schema** → inject into `config/routes.tf.rb` or `infrastructure/schema.tf.rb` when needed
|
|
683
|
+
5. **Optional overrides** → `--controllers` flag for app-local subclasses
|
|
684
|
+
6. **Destroy path** → `belt destroy <name>` removes what generate created
|
|
685
|
+
7. **Help text** → `.description` + `--help` explaining what was installed and next steps
|
|
686
|
+
|
|
687
|
+
Copy from `belt-messaging` or `belt-pay` rather than inventing a new structure.
|
|
688
|
+
|
|
689
|
+
## Contributing
|
|
690
|
+
|
|
691
|
+
Bug reports and pull requests are welcome on [GitHub](https://github.com/stowzilla/belt).
|
|
692
|
+
|
|
693
|
+
### Development setup
|
|
694
|
+
|
|
695
|
+
```bash
|
|
696
|
+
git clone https://github.com/stowzilla/belt.git
|
|
697
|
+
cd belt
|
|
698
|
+
bundle install
|
|
699
|
+
bundle exec rspec
|
|
700
|
+
bundle exec rubocop
|
|
701
|
+
```
|
|
702
|
+
|
|
703
|
+
### Guidelines
|
|
704
|
+
|
|
705
|
+
1. **Branch from `master`** — open a PR against `master`
|
|
706
|
+
2. **Keep changes focused** — one concern per PR when practical
|
|
707
|
+
3. **Follow existing patterns** — look at neighboring files and `lib/belt/cli/` before inventing new ones
|
|
708
|
+
4. **Tests + lint** — run `bundle exec rspec` and `bundle exec rubocop` before opening (or updating) a PR. CI requires both (plus `bundler-audit`); failing checks block merge on `master`
|
|
709
|
+
5. **Changelog** — note user-facing changes in `CHANGELOG.md` under the next version / Unreleased
|
|
710
|
+
6. **No secrets** — never commit AWS keys, tokens, or real account IDs
|
|
711
|
+
|
|
712
|
+
### Project layout (for contributors)
|
|
713
|
+
|
|
714
|
+
| Path | What lives there |
|
|
715
|
+
|------|------------------|
|
|
716
|
+
| `lib/belt.rb` | Public require entry |
|
|
717
|
+
| `lib/belt/` | Framework core (controller, router, handler, observability) |
|
|
718
|
+
| `lib/belt/cli/` | CLI commands (`new`, `generate`, `deploy`, `plugin`, …) |
|
|
719
|
+
| `lib/belt_controller/` | `BeltController::Base` |
|
|
720
|
+
| `lib/templates/` | ERB templates for `belt new`, generators, plugin scaffold |
|
|
721
|
+
| `exe/belt` | CLI executable |
|
|
722
|
+
| `spec/` | RSpec suite |
|
|
723
|
+
| `AGENTS.md` | Agent-oriented map of this gem (layout, CLI, plugin contract) |
|
|
724
|
+
|
|
725
|
+
AI / coding agents: start with **[AGENTS.md](AGENTS.md)**. Belt apps and plugins scaffolded by the CLI get their own `AGENTS.md` as well (`belt new`, `belt plugin new`).
|
|
726
|
+
|
|
727
|
+
### Local gem development against an app
|
|
728
|
+
|
|
729
|
+
```ruby
|
|
730
|
+
# In a Belt app's Gemfile
|
|
731
|
+
gem "belt", path: "../belt"
|
|
732
|
+
```
|
|
733
|
+
|
|
734
|
+
`belt deploy` detects `path:` gems and materializes them into `vendor/cache` for Lambda packaging so you can iterate without publishing a gem for every try.
|
|
735
|
+
|
|
736
|
+
### Contributing a plugin
|
|
737
|
+
|
|
738
|
+
Plugins are separate repositories (not vendored into this repo). To add a new first-class capability:
|
|
739
|
+
|
|
740
|
+
1. `belt plugin new <name>` (or copy `belt-messaging` / `belt-pay`)
|
|
741
|
+
2. Implement the runtime API + generator contract above
|
|
742
|
+
3. Document install steps in the plugin README (`gem …` → `belt generate …`)
|
|
743
|
+
4. Open a PR on the plugin repo; optionally link it from this README's plugin table
|
|
744
|
+
|
|
745
|
+
Questions about plugin design or core changes: open a GitHub issue or discuss in the project Discord.
|
|
746
|
+
|
|
578
747
|
## License
|
|
579
748
|
|
|
580
749
|
MIT
|
data/lib/belt/action_router.rb
CHANGED
|
@@ -102,15 +102,35 @@ module Belt
|
|
|
102
102
|
end
|
|
103
103
|
|
|
104
104
|
def dispatch_to_controller(route_info, event, body)
|
|
105
|
-
|
|
105
|
+
controller_name = route_info[:controller]
|
|
106
|
+
|
|
107
|
+
# Prevent path traversal — controller names must be simple identifiers or single-level nested
|
|
108
|
+
unless valid_controller_name?(controller_name)
|
|
109
|
+
Belt::Observability::Logger.instance&.warn('Invalid controller name', controller: controller_name)
|
|
110
|
+
return error_response('Not found', 404, event)
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
controller_class = resolve_controller(controller_name)
|
|
106
114
|
controller = controller_class.new(event: event, body: body)
|
|
107
115
|
|
|
108
|
-
|
|
109
|
-
Belt::Observability::Logger.instance&.info("Processing by #{
|
|
116
|
+
class_label = controller_class.name.split('::').last.gsub('Controller', '')
|
|
117
|
+
Belt::Observability::Logger.instance&.info("Processing by #{class_label}##{route_info[:action]}")
|
|
110
118
|
|
|
111
119
|
controller.dispatch(route_info[:action].to_sym)
|
|
112
120
|
end
|
|
113
121
|
|
|
122
|
+
# Validate controller names to prevent path traversal.
|
|
123
|
+
# Allows: "posts", "admin/posts" (single nesting level, no dots or special chars)
|
|
124
|
+
def valid_controller_name?(name)
|
|
125
|
+
return false if name.nil? || name.empty?
|
|
126
|
+
return false if name.include?('..')
|
|
127
|
+
return false if name.start_with?('/') || name.end_with?('/')
|
|
128
|
+
return false if name.include?('\\')
|
|
129
|
+
|
|
130
|
+
# Allow only lowercase letters, digits, underscores, and a single forward slash for nesting
|
|
131
|
+
name.match?(%r{\A[a-z][a-z0-9_]*(/[a-z][a-z0-9_]*)?\z})
|
|
132
|
+
end
|
|
133
|
+
|
|
114
134
|
def resolve_controller(controller_name)
|
|
115
135
|
# Try namespace module first (app's own controllers)
|
|
116
136
|
begin
|
|
@@ -137,10 +157,18 @@ module Belt
|
|
|
137
157
|
def resolve_from_paths(controller_name)
|
|
138
158
|
if controller_name.include?('/')
|
|
139
159
|
end
|
|
140
|
-
file_name = "#{controller_name}_controller.rb"
|
|
160
|
+
file_name = "#{controller_name.gsub('/', '_')}_controller.rb"
|
|
141
161
|
|
|
142
162
|
Belt.all_controller_paths.each do |path|
|
|
143
163
|
full_path = File.join(path, file_name)
|
|
164
|
+
|
|
165
|
+
# Ensure resolved path stays within the allowed controller directory
|
|
166
|
+
begin
|
|
167
|
+
resolved = File.realpath(full_path)
|
|
168
|
+
rescue Errno::ENOENT, Errno::EACCES
|
|
169
|
+
next
|
|
170
|
+
end
|
|
171
|
+
next unless resolved.start_with?(File.realpath(path))
|
|
144
172
|
next unless File.exist?(full_path)
|
|
145
173
|
|
|
146
174
|
require full_path
|
|
@@ -40,17 +40,31 @@ module Belt
|
|
|
40
40
|
name.to_s.downcase.tr('_', '-')
|
|
41
41
|
end
|
|
42
42
|
|
|
43
|
-
# Finds routes
|
|
43
|
+
# Finds routes file checking config/routes.rb first, then legacy paths.
|
|
44
44
|
def find_routes_file_path
|
|
45
|
-
candidates = [
|
|
45
|
+
candidates = [
|
|
46
|
+
'config/routes.rb',
|
|
47
|
+
'config/routes.tf.rb',
|
|
48
|
+
'infrastructure/routes.tf.rb'
|
|
49
|
+
]
|
|
46
50
|
candidates.find { |f| File.exist?(f) }
|
|
47
51
|
end
|
|
48
52
|
|
|
49
|
-
# Finds
|
|
50
|
-
def
|
|
51
|
-
candidates = [
|
|
53
|
+
# Finds contracts file checking config/contracts.rb first, then legacy paths.
|
|
54
|
+
def find_contracts_file_path
|
|
55
|
+
candidates = [
|
|
56
|
+
'config/contracts.rb',
|
|
57
|
+
'config/contracts.tf.rb',
|
|
58
|
+
'config/schema.tf.rb',
|
|
59
|
+
'infrastructure/schema.tf.rb'
|
|
60
|
+
]
|
|
52
61
|
candidates.find { |f| File.exist?(f) }
|
|
53
62
|
end
|
|
63
|
+
|
|
64
|
+
# Legacy alias for backward compatibility
|
|
65
|
+
def find_schema_file_path
|
|
66
|
+
find_contracts_file_path
|
|
67
|
+
end
|
|
54
68
|
end
|
|
55
69
|
end
|
|
56
70
|
end
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'json'
|
|
4
|
+
require 'optparse'
|
|
5
|
+
require_relative '../route_dsl'
|
|
6
|
+
require_relative 'routes_command/schema_loader'
|
|
7
|
+
|
|
8
|
+
module Belt
|
|
9
|
+
module CLI
|
|
10
|
+
class ContractsCommand
|
|
11
|
+
include RoutesCommand::SchemaLoader
|
|
12
|
+
|
|
13
|
+
def self.run(args)
|
|
14
|
+
new(args).run
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def initialize(args)
|
|
18
|
+
@options = {}
|
|
19
|
+
parse_options(args)
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def run
|
|
23
|
+
contracts_file = find_contracts_file
|
|
24
|
+
unless contracts_file
|
|
25
|
+
abort 'Error: No contracts file found. ' \
|
|
26
|
+
'Expected config/contracts.rb (or config/contracts.tf.rb, config/schema.tf.rb)'
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
models = load_contracts(contracts_file)
|
|
30
|
+
|
|
31
|
+
if @options[:format] == 'json'
|
|
32
|
+
output_json(models)
|
|
33
|
+
else
|
|
34
|
+
output_concise(models)
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
private
|
|
39
|
+
|
|
40
|
+
def parse_options(args)
|
|
41
|
+
OptionParser.new do |opts|
|
|
42
|
+
opts.banner = 'Usage: belt contracts [options]'
|
|
43
|
+
|
|
44
|
+
opts.on('-f', '--format FORMAT', 'Output format: concise (default), json') do |format|
|
|
45
|
+
@options[:format] = format
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
opts.on('-g', '--grep PATTERN', 'Filter contracts matching pattern') do |pattern|
|
|
49
|
+
@options[:grep] = pattern
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
opts.on('--file FILE', 'Path to contracts file (overrides auto-detection)') do |file|
|
|
53
|
+
@options[:contracts_file] = file
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
opts.on('-h', '--help', 'Show this help') do
|
|
57
|
+
puts opts
|
|
58
|
+
exit
|
|
59
|
+
end
|
|
60
|
+
end.parse!(args)
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
def find_contracts_file
|
|
64
|
+
if @options[:contracts_file]
|
|
65
|
+
file = @options[:contracts_file]
|
|
66
|
+
return file if File.exist?(file)
|
|
67
|
+
|
|
68
|
+
abort "Error: Specified contracts file not found: #{file}"
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
candidates = [
|
|
72
|
+
'config/contracts.rb',
|
|
73
|
+
'config/contracts.tf.rb',
|
|
74
|
+
'config/schema.tf.rb',
|
|
75
|
+
'infrastructure/schema.tf.rb'
|
|
76
|
+
]
|
|
77
|
+
candidates.find { |f| File.exist?(f) }
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
def load_contracts(file)
|
|
81
|
+
Belt.instance_variable_set(:@application, nil)
|
|
82
|
+
begin
|
|
83
|
+
eval(File.read(file), binding, file) # rubocop:disable Security/Eval
|
|
84
|
+
rescue StandardError => e
|
|
85
|
+
abort "Error: Failed to load contracts file #{file}: #{e.message}"
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
schema = Belt.application.schema.to_h
|
|
89
|
+
models = build_models_from_schema(schema)
|
|
90
|
+
models = apply_grep(models) if @options[:grep]
|
|
91
|
+
models
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
def apply_grep(models)
|
|
95
|
+
pattern = Regexp.new(@options[:grep], Regexp::IGNORECASE)
|
|
96
|
+
models.select do |m|
|
|
97
|
+
m[:name].match?(pattern) ||
|
|
98
|
+
m[:kind].match?(pattern) ||
|
|
99
|
+
m[:description].match?(pattern)
|
|
100
|
+
end
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
def output_json(models)
|
|
104
|
+
puts JSON.pretty_generate(models: models)
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
def output_concise(models)
|
|
108
|
+
return puts('No contracts defined.') if models.empty?
|
|
109
|
+
|
|
110
|
+
requests = models.select { |m| m[:kind] == 'request' }
|
|
111
|
+
responses = models.select { |m| m[:kind] == 'response' }
|
|
112
|
+
|
|
113
|
+
if requests.any?
|
|
114
|
+
puts 'REQUEST MODELS'
|
|
115
|
+
puts '-' * 60
|
|
116
|
+
requests.each { |m| print_model(m) }
|
|
117
|
+
puts
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
if responses.any?
|
|
121
|
+
puts 'RESPONSE MODELS'
|
|
122
|
+
puts '-' * 60
|
|
123
|
+
responses.each { |m| print_model(m) }
|
|
124
|
+
end
|
|
125
|
+
|
|
126
|
+
puts "\n#{models.length} contract(s) total (#{requests.length} request, #{responses.length} response)"
|
|
127
|
+
end
|
|
128
|
+
|
|
129
|
+
def print_model(model)
|
|
130
|
+
required = model[:required] || []
|
|
131
|
+
props = (model[:properties] || {}).map do |name, meta|
|
|
132
|
+
type = meta['type'] || meta[:type] || 'string'
|
|
133
|
+
req_marker = required.include?(name.to_s) ? ' *' : ''
|
|
134
|
+
"#{name}:#{type}#{req_marker}"
|
|
135
|
+
end
|
|
136
|
+
|
|
137
|
+
puts " #{model[:name]} (#{model[:kind]})"
|
|
138
|
+
puts " #{props.join(', ')}" if props.any?
|
|
139
|
+
end
|
|
140
|
+
end
|
|
141
|
+
end
|
|
142
|
+
end
|