Skip to content

Commit 8dd069f

Browse files
committed
added scripts/export_provider_catalog.rb and sync provider workflow
1 parent 869b6b2 commit 8dd069f

File tree

2 files changed

+190
-0
lines changed

2 files changed

+190
-0
lines changed
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
name: Sync Terraform Provider Catalog
2+
3+
on:
4+
release:
5+
types: [published]
6+
7+
permissions:
8+
contents: read
9+
10+
jobs:
11+
sync:
12+
name: Sync provider on release
13+
runs-on: ubuntu-latest
14+
steps:
15+
- name: Checkout logstruct
16+
uses: actions/checkout@v4
17+
with:
18+
fetch-depth: 0
19+
20+
- name: Setup Ruby
21+
uses: ruby/setup-ruby@v1
22+
with:
23+
ruby-version: '.ruby-version'
24+
bundler-cache: true
25+
26+
- name: Setup Node (for pnpm cache if needed)
27+
uses: actions/setup-node@v4
28+
with:
29+
node-version: 20
30+
31+
- name: Setup pnpm
32+
uses: pnpm/action-setup@v4
33+
with:
34+
version: latest
35+
36+
- name: Generate site exports (enums/structs/keys)
37+
run: |
38+
ruby scripts/export_typescript_types.rb
39+
40+
- name: Clone terraform-provider-logstruct repository
41+
env:
42+
PUSH_TOKEN: ${{ secrets.PROVIDER_PUSH_TOKEN }}
43+
run: |
44+
git config --global user.name "github-actions[bot]"
45+
git config --global user.email "github-actions[bot]@users.noreply.github.com"
46+
rm -rf terraform-provider-logstruct
47+
git clone https://x-access-token:${PUSH_TOKEN}@github.com/DocSpring/terraform-provider-logstruct.git terraform-provider-logstruct
48+
49+
- name: Export provider catalog (embedded Go data)
50+
run: |
51+
ruby scripts/export_provider_catalog.rb
52+
53+
- name: Build provider to validate catalog
54+
uses: actions/setup-go@v5
55+
with:
56+
go-version: '1.22'
57+
- name: Compile
58+
run: |
59+
cd terraform-provider-logstruct
60+
go mod tidy
61+
go build ./...
62+
63+
- name: Commit and push provider changes
64+
env:
65+
PUSH_TOKEN: ${{ secrets.PROVIDER_PUSH_TOKEN }}
66+
run: |
67+
cd terraform-provider-logstruct
68+
if ! git diff --quiet; then
69+
git add pkg/data/catalog_gen.go pkg/data/catalog.json go.mod go.sum || true
70+
git commit -m "Sync catalog from logstruct ${{ github.event.release.tag_name }}"
71+
git push origin HEAD:main
72+
fi
73+
74+
- name: Tag provider with release version
75+
env:
76+
PUSH_TOKEN: ${{ secrets.PROVIDER_PUSH_TOKEN }}
77+
run: |
78+
cd terraform-provider-logstruct
79+
TAG="${{ github.event.release.tag_name }}"
80+
if git rev-parse -q --verify "refs/tags/$TAG" >/dev/null; then
81+
echo "Tag $TAG already exists; skipping."
82+
exit 0
83+
fi
84+
git tag -a "$TAG" -m "Release $TAG (logstruct sync)"
85+
git push origin "$TAG"

scripts/export_provider_catalog.rb

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
#!/usr/bin/env ruby
2+
# typed: true
3+
# frozen_string_literal: true
4+
5+
require "json"
6+
require "fileutils"
7+
8+
root = File.expand_path("..", __dir__)
9+
site_gen = File.join(root, "site", "lib", "log-generation")
10+
enums_path = File.join(site_gen, "sorbet-enums.json")
11+
structs_path = File.join(site_gen, "sorbet-log-structs.json")
12+
keys_path = File.join(site_gen, "log-keys.json")
13+
14+
unless File.exist?(enums_path) && File.exist?(structs_path) && File.exist?(keys_path)
15+
abort "Missing generated files in #{site_gen}. Run scripts/all_check.sh first."
16+
end
17+
18+
enums = JSON.parse(File.read(enums_path))
19+
structs = JSON.parse(File.read(structs_path))
20+
keys = JSON.parse(File.read(keys_path))
21+
22+
# Build lookup maps for enum serialized values
23+
event_values = {}
24+
(enums["LogStruct::Event"] || []).each { |e| event_values[e["name"]] = e["value"] }
25+
source_values = {}
26+
(enums["LogStruct::Source"] || []).each { |e| source_values[e["name"]] = e["value"] }
27+
28+
catalog = {"keys" => keys, "structs" => {}}
29+
30+
structs.each do |fq_name, info|
31+
name = info["name"] || fq_name.split("::").last
32+
fields = info["fields"] || {}
33+
s = {"name" => name}
34+
35+
# Fixed source if enum_single
36+
if fields["source"] && fields["source"]["type"] == "enum_single" && fields["source"]["enum_value"]
37+
src_name = fields["source"]["enum_value"]
38+
s["fixed_source"] = source_values[src_name] || src_name.downcase
39+
else
40+
s["fixed_source"] = nil
41+
end
42+
43+
# Allowed events (serialized)
44+
allowed = []
45+
if fields["event"]
46+
ef = fields["event"]
47+
case ef["type"]
48+
when "enum_single"
49+
if ef["enum_value"]
50+
nm = ef["enum_value"]
51+
allowed << (event_values[nm] || nm.downcase)
52+
end
53+
when "enum_union"
54+
(ef["enum_values"] || []).each do |nm|
55+
allowed << (event_values[nm] || nm.downcase)
56+
end
57+
end
58+
end
59+
s["allowed_events"] = allowed.uniq
60+
61+
catalog["structs"][name] = s
62+
end
63+
64+
provider_dir = File.join(root, "terraform-provider-logstruct")
65+
data_dir = File.join(provider_dir, "pkg", "data")
66+
FileUtils.mkdir_p(data_dir)
67+
68+
# Always write JSON (useful for inspection)
69+
catalog_path = File.join(data_dir, "catalog.json")
70+
File.write(catalog_path, JSON.pretty_generate(catalog))
71+
72+
# Also generate a Go source file with embedded types and data for zero runtime parsing
73+
gen_path = File.join(data_dir, "catalog_gen.go")
74+
75+
def go_string(str)
76+
# Escape backslashes and quotes for Go string literals
77+
str.to_s.gsub("\\", "\\\\").gsub('"', '\\"')
78+
end
79+
80+
go = +"// Code generated by scripts/export_provider_catalog.rb; DO NOT EDIT.\n"
81+
go << "package data\n\n"
82+
go << "func ptr[T any](v T) *T { return &v }\n\n"
83+
go << "type StructCatalog struct {\n\tName string\n\tFixedSource *string\n\tAllowedEvents []string\n}\n\n"
84+
go << "type Catalog struct {\n\tKeys map[string]string\n\tStructs map[string]StructCatalog\n}\n\n"
85+
go << "var CatalogData = Catalog{\n\tKeys: map[string]string{\n"
86+
keys.each do |k, v|
87+
go << "\t\t\"#{go_string(k)}\": \"#{go_string(v)}\",\n"
88+
end
89+
go << "\t},\n\tStructs: map[string]StructCatalog{\n"
90+
catalog["structs"].each do |name, s|
91+
go << "\t\t\"#{go_string(name)}\": {Name: \"#{go_string(s["name"])}\","
92+
go << if s["fixed_source"]
93+
" FixedSource: ptr(\"#{go_string(s["fixed_source"])}\"),"
94+
else
95+
" FixedSource: nil,"
96+
end
97+
go << " AllowedEvents: []string{"
98+
go << s["allowed_events"].map { |ev| "\"#{go_string(ev)}\"" }.join(", ")
99+
go << "}},\n"
100+
end
101+
go << "\t},\n}\n"
102+
103+
File.write(gen_path, go)
104+
105+
puts "Wrote provider catalog to #{catalog_path} and #{gen_path}"

0 commit comments

Comments
 (0)