Skip to content

Commit d133b22

Browse files
authored
Merge pull request #62 from chainguard-dev/create-pull-request/patch
Export mono/public/terraform-infra-common: refs/heads/main
2 parents c8a5001 + 3b66f0f commit d133b22

29 files changed

Lines changed: 508 additions & 40 deletions

File tree

go.mod

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ require (
88
cloud.google.com/go/bigquery v1.73.1
99
cloud.google.com/go/compute/metadata v0.9.0
1010
cloud.google.com/go/profiler v0.4.3
11-
cloud.google.com/go/pubsub/v2 v2.3.0
11+
cloud.google.com/go/pubsub/v2 v2.5.1
1212
github.com/GoogleCloudPlatform/opentelemetry-operations-go/exporter/trace v1.31.0
1313
github.com/avvmoto/buf-readerat v0.0.0-20171115124131-a17c8cb89270
1414
github.com/bradleyfalzon/ghinstallation/v2 v2.18.0
1515
github.com/chainguard-dev/clog v1.8.0
1616
github.com/cloudevents/sdk-go/v2 v2.16.2
17-
github.com/coreos/go-oidc/v3 v3.17.0
17+
github.com/coreos/go-oidc/v3 v3.18.0
1818
github.com/go-git/go-git/v5 v5.18.0
1919
github.com/google/go-cmp v0.7.0
2020
github.com/google/go-github/v84 v84.0.0
@@ -131,12 +131,12 @@ require (
131131
go.yaml.in/yaml/v2 v2.4.4 // indirect
132132
golang.org/x/crypto v0.49.0 // indirect
133133
golang.org/x/exp v0.0.0-20251023183803-a4bb9ffd2546 // indirect
134-
golang.org/x/mod v0.33.0 // indirect
134+
golang.org/x/mod v0.34.0 // indirect
135135
golang.org/x/net v0.52.0 // indirect
136136
golang.org/x/sys v0.42.0 // indirect
137-
golang.org/x/telemetry v0.0.0-20260209163413-e7419c687ee4 // indirect
138-
golang.org/x/text v0.35.0 // indirect
139-
golang.org/x/tools v0.42.0 // indirect
137+
golang.org/x/telemetry v0.0.0-20260311193753-579e4da9a98c // indirect
138+
golang.org/x/text v0.36.0 // indirect
139+
golang.org/x/tools v0.43.0 // indirect
140140
golang.org/x/xerrors v0.0.0-20240903120638-7835f813f4da // indirect
141141
google.golang.org/genproto v0.0.0-20260217215200-42d3e9bedb6d // indirect
142142
google.golang.org/genproto/googleapis/api v0.0.0-20260401024825-9d38bb4040a9 // indirect

go.sum

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ cloud.google.com/go/monitoring v1.24.3 h1:dde+gMNc0UhPZD1Azu6at2e79bfdztVDS5lvhO
2727
cloud.google.com/go/monitoring v1.24.3/go.mod h1:nYP6W0tm3N9H/bOw8am7t62YTzZY+zUeQ+Bi6+2eonI=
2828
cloud.google.com/go/profiler v0.4.3 h1:IY3QNKlr8VbXwGWHcZbJQsMA/83ZTH6uAHf8jYyj7OI=
2929
cloud.google.com/go/profiler v0.4.3/go.mod h1:3xFodugWfPIQZWFcXdUmfa+yTiiyQ8fWrdT+d2Sg4J0=
30-
cloud.google.com/go/pubsub/v2 v2.3.0 h1:DgAN907x+sP0nScYfBzneRiIhWoXcpCD8ZAut8WX9vs=
31-
cloud.google.com/go/pubsub/v2 v2.3.0/go.mod h1:O5f0KHG9zDheZAd3z5rlCRhxt2JQtB+t/IYLKK3Bpvw=
30+
cloud.google.com/go/pubsub/v2 v2.5.1 h1:+TwXJr78P9RrMV3S8lKHIhJo2E99jI7ta65e+ujJjts=
31+
cloud.google.com/go/pubsub/v2 v2.5.1/go.mod h1:Pd+qeabMX+576vQJhTN7TelE4k6kJh15dLU/ptOQ/UA=
3232
cloud.google.com/go/storage v1.59.0 h1:9p3yDzEN9Vet4JnbN90FECIw6n4FCXcKBK1scxtQnw8=
3333
cloud.google.com/go/storage v1.59.0/go.mod h1:cMWbtM+anpC74gn6qjLh+exqYcfmB9Hqe5z6adx+CLI=
3434
cloud.google.com/go/trace v1.11.7 h1:kDNDX8JkaAG3R2nq1lIdkb7FCSi1rCmsEtKVsty7p+U=
@@ -118,8 +118,8 @@ github.com/cloudflare/circl v1.6.3/go.mod h1:2eXP6Qfat4O/Yhh8BznvKnJ+uzEoTQ6jVKJ
118118
github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc=
119119
github.com/cncf/xds/go v0.0.0-20251210132809-ee656c7534f5 h1:6xNmx7iTtyBRev0+D/Tv1FZd4SCg8axKApyNyRsAt/w=
120120
github.com/cncf/xds/go v0.0.0-20251210132809-ee656c7534f5/go.mod h1:KdCmV+x/BuvyMxRnYBlmVaq4OLiKW6iRQfvC62cvdkI=
121-
github.com/coreos/go-oidc/v3 v3.17.0 h1:hWBGaQfbi0iVviX4ibC7bk8OKT5qNr4klBaCHVNvehc=
122-
github.com/coreos/go-oidc/v3 v3.17.0/go.mod h1:wqPbKFrVnE90vty060SB40FCJ8fTHTxSwyXJqZH+sI8=
121+
github.com/coreos/go-oidc/v3 v3.18.0 h1:V9orjXynvu5wiC9SemFTWnG4F45v403aIcjWo0d41+A=
122+
github.com/coreos/go-oidc/v3 v3.18.0/go.mod h1:DYCf24+ncYi+XkIH97GY1+dqoRlbaSI26KVTCI9SrY4=
123123
github.com/cyphar/filepath-securejoin v0.4.1 h1:JyxxyPEaktOD+GAnqIqTf9A8tHyAG22rowi7HkoSU1s=
124124
github.com/cyphar/filepath-securejoin v0.4.1/go.mod h1:Sdj7gXlvMcPZsbhwhQ33GguGLDGQL7h7bg04C/+u9jI=
125125
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
@@ -324,8 +324,8 @@ github.com/zeebo/assert v1.3.0 h1:g7C04CbJuIDKNPFHmsk4hwZDO5O+kntRxzaUoNXj+IQ=
324324
github.com/zeebo/assert v1.3.0/go.mod h1:Pq9JiuJQpG8JLJdtkwrJESF0Foym2/D9XMU5ciN/wJ0=
325325
github.com/zeebo/xxh3 v1.0.2 h1:xZmwmqxHZA8AI603jOQ0tMqmBr9lPeFwGg6d+xy9DC0=
326326
github.com/zeebo/xxh3 v1.0.2/go.mod h1:5NWz9Sef7zIDm2JHfFlcQvNekmcEl9ekUZQQKCYaDcA=
327-
go.einride.tech/aip v0.73.0 h1:bPo4oqBo2ZQeBKo4ZzLb1kxYXTY1ysJhpvQyfuGzvps=
328-
go.einride.tech/aip v0.73.0/go.mod h1:Mj7rFbmXEgw0dq1dqJ7JGMvYCZZVxmGOR3S4ZcV5LvQ=
327+
go.einride.tech/aip v0.83.0 h1:TI21IdeOnLTwZEJ3BxtImIZk6bsN2Q+sd0x99SLiQ+M=
328+
go.einride.tech/aip v0.83.0/go.mod h1:E8+wdTApA70odnpFzJgsGogHozC2JCIhFJBKPr8bVig=
329329
go.opencensus.io v0.24.0 h1:y73uSU6J157QMP2kn2r30vwW1A2W2WFwSCGnAVxeaD0=
330330
go.opencensus.io v0.24.0/go.mod h1:vNK8G9p7aAivkbmorf4v+7Hgx+Zs0yY+0fOtgBfjQKo=
331331
go.opentelemetry.io/auto/sdk v1.2.1 h1:jXsnJ4Lmnqd11kwkBV2LgLoFMZKizbCi5fNZ/ipaZ64=
@@ -377,8 +377,8 @@ golang.org/x/exp v0.0.0-20251023183803-a4bb9ffd2546/go.mod h1:j/pmGrbnkbPtQfxEe5
377377
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
378378
golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU=
379379
golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
380-
golang.org/x/mod v0.33.0 h1:tHFzIWbBifEmbwtGz65eaWyGiGZatSrT9prnU8DbVL8=
381-
golang.org/x/mod v0.33.0/go.mod h1:swjeQEj+6r7fODbD2cqrnje9PnziFuw4bmLbBZFrQ5w=
380+
golang.org/x/mod v0.34.0 h1:xIHgNUUnW6sYkcM5Jleh05DvLOtwc6RitGHbDk4akRI=
381+
golang.org/x/mod v0.34.0/go.mod h1:ykgH52iCZe79kzLLMhyCUzhMci+nQj+0XkbXpNYtVjY=
382382
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
383383
golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
384384
golang.org/x/net v0.0.0-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
@@ -411,25 +411,25 @@ golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBc
411411
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
412412
golang.org/x/sys v0.42.0 h1:omrd2nAlyT5ESRdCLYdm3+fMfNFE/+Rf4bDIQImRJeo=
413413
golang.org/x/sys v0.42.0/go.mod h1:4GL1E5IUh+htKOUEOaiffhrAeqysfVGipDYzABqnCmw=
414-
golang.org/x/telemetry v0.0.0-20260209163413-e7419c687ee4 h1:bTLqdHv7xrGlFbvf5/TXNxy/iUwwdkjhqQTJDjW7aj0=
415-
golang.org/x/telemetry v0.0.0-20260209163413-e7419c687ee4/go.mod h1:g5NllXBEermZrmR51cJDQxmJUHUOfRAaNyWBM+R+548=
414+
golang.org/x/telemetry v0.0.0-20260311193753-579e4da9a98c h1:6a8FdnNk6bTXBjR4AGKFgUKuo+7GnR3FX5L7CbveeZc=
415+
golang.org/x/telemetry v0.0.0-20260311193753-579e4da9a98c/go.mod h1:TpUTTEp9frx7rTdLpC9gFG9kdI7zVLFTFFlqaH2Cncw=
416416
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
417417
golang.org/x/term v0.41.0 h1:QCgPso/Q3RTJx2Th4bDLqML4W6iJiaXFq2/ftQF13YU=
418418
golang.org/x/term v0.41.0/go.mod h1:3pfBgksrReYfZ5lvYM0kSO0LIkAl4Yl2bXOkKP7Ec2A=
419419
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
420420
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
421421
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
422-
golang.org/x/text v0.35.0 h1:JOVx6vVDFokkpaq1AEptVzLTpDe9KGpj5tR4/X+ybL8=
423-
golang.org/x/text v0.35.0/go.mod h1:khi/HExzZJ2pGnjenulevKNX1W67CUy0AsXcNubPGCA=
422+
golang.org/x/text v0.36.0 h1:JfKh3XmcRPqZPKevfXVpI1wXPTqbkE5f7JA92a55Yxg=
423+
golang.org/x/text v0.36.0/go.mod h1:NIdBknypM8iqVmPiuco0Dh6P5Jcdk8lJL0CUebqK164=
424424
golang.org/x/time v0.15.0 h1:bbrp8t3bGUeFOx08pvsMYRTCVSMk89u4tKbNOZbp88U=
425425
golang.org/x/time v0.15.0/go.mod h1:Y4YMaQmXwGQZoFaVFk4YpCt4FLQMYKZe9oeV/f4MSno=
426426
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
427427
golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
428428
golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY=
429429
golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
430430
golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q=
431-
golang.org/x/tools v0.42.0 h1:uNgphsn75Tdz5Ji2q36v/nsFSfR/9BRFvqhGBaJGd5k=
432-
golang.org/x/tools v0.42.0/go.mod h1:Ma6lCIwGZvHK6XtgbswSoWroEkhugApmsXyrUmBhfr0=
431+
golang.org/x/tools v0.43.0 h1:12BdW9CeB3Z+J/I/wj34VMl8X+fEXBxVR90JeMX5E7s=
432+
golang.org/x/tools v0.43.0/go.mod h1:uHkMso649BX2cZK6+RpuIPXS3ho2hZo4FVwfoy1vIk0=
433433
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
434434
golang.org/x/xerrors v0.0.0-20240903120638-7835f813f4da h1:noIWHXmPHxILtqtCOPIhSt0ABwskkZKjD3bXGnZGpNY=
435435
golang.org/x/xerrors v0.0.0-20240903120638-7835f813f4da/go.mod h1:NDW/Ps6MPRej6fsCIbMTohpP40sJ/P/vI1MoTEGwX90=

modules/aws/apprunner-regional-go-service/example/app/main.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -88,12 +88,10 @@ func main() {
8888

8989
// Health check endpoint
9090
http.HandleFunc("/health", func(w http.ResponseWriter, _ *http.Request) {
91-
uptime := time.Since(startTime).Round(time.Second)
92-
9391
response := HealthResponse{
9492
Status: "healthy",
9593
Region: region,
96-
Uptime: uptime.String(),
94+
Uptime: time.Since(startTime).Round(time.Second).String(),
9795
Time: time.Now(),
9896
}
9997

@@ -229,15 +227,13 @@ func main() {
229227
clog.InfoContextf(ctx, "Server listening on %s", addr)
230228
clog.InfoContextf(ctx, "Endpoints: / /health /ready /info /ui")
231229

232-
server := &http.Server{
230+
if err := (&http.Server{
233231
Addr: addr,
234232
ReadHeaderTimeout: 30 * time.Second,
235233
ReadTimeout: 30 * time.Second,
236234
WriteTimeout: 30 * time.Second,
237235
IdleTimeout: 60 * time.Second,
238-
}
239-
240-
if err := server.ListenAndServe(); err != nil {
236+
}).ListenAndServe(); err != nil {
241237
clog.FatalContextf(ctx, "Server failed to start: %v", err)
242238
}
243239
}

modules/cloudevent-recorder/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ No requirements.
158158
| <a name="input_scaling"></a> [scaling](#input\_scaling) | The scaling configuration for the service. | <pre>object({<br/> min_instances = optional(number, 0)<br/> max_instances = optional(number, 100)<br/> max_instance_request_concurrency = optional(number)<br/> })</pre> | `{}` | no |
159159
| <a name="input_split_triggers"></a> [split\_triggers](#input\_split\_triggers) | Opt-in flag to split into per-trigger dashboards. Helpful when hitting widget limits | `bool` | `false` | no |
160160
| <a name="input_team"></a> [team](#input\_team) | Team label to apply to resources (replaces deprecated 'squad'). | `string` | n/a | yes |
161-
| <a name="input_types"></a> [types](#input\_types) | A map from cloudevent types to the BigQuery schema associated with them, as well as an alert threshold and a list of notification channels (for subscription-level issues). | <pre>map(object({<br/> schema = string<br/> retention_period_days = optional(number, null)<br/> alert_threshold = optional(number, 50000)<br/> notification_channels = optional(list(string), [])<br/> partition_field = optional(string)<br/> clustering = optional(list(string), null)<br/> }))</pre> | n/a | yes |
161+
| <a name="input_types"></a> [types](#input\_types) | A map from cloudevent types to the BigQuery schema associated with them, as well as an alert threshold and a list of notification channels (for subscription-level issues). | <pre>map(object({<br/> schema = string<br/> retention_period_days = optional(number, null)<br/> alert_threshold = optional(number, 50000)<br/> notification_channels = optional(list(string), [])<br/> partition_field = optional(string)<br/> clustering = optional(list(string), null)<br/> // Per-type override for the BigQuery table's deletion_protection<br/> // terraform attribute. When null (the default), the module-wide<br/> // var.deletion_protection applies. Intended for tables whose shapes<br/> // aren't fully defined yet — event types that may still be renamed,<br/> // re-partitioned, or removed — so iterating on the schema doesn't<br/> // require the schema-deletion playbook dance.<br/> deletion_protection = optional(bool, null)<br/> }))</pre> | n/a | yes |
162162

163163
## Outputs
164164

modules/cloudevent-recorder/bigquery.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ resource "google_bigquery_table" "types" {
3333

3434
clustering = each.value.clustering
3535

36-
deletion_protection = var.deletion_protection
36+
deletion_protection = each.value.deletion_protection != null ? each.value.deletion_protection : var.deletion_protection
3737
}
3838

3939
// Create an identity that will be used to run the BQ DTS job,

modules/cloudevent-recorder/variables.tf

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,13 @@ variable "types" {
5454
notification_channels = optional(list(string), [])
5555
partition_field = optional(string)
5656
clustering = optional(list(string), null)
57+
// Per-type override for the BigQuery table's deletion_protection
58+
// terraform attribute. When null (the default), the module-wide
59+
// var.deletion_protection applies. Intended for tables whose shapes
60+
// aren't fully defined yet — event types that may still be renamed,
61+
// re-partitioned, or removed — so iterating on the schema doesn't
62+
// require the schema-deletion playbook dance.
63+
deletion_protection = optional(bool, null)
5764
}))
5865
}
5966

modules/cron/example/go.mod

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
11
module github.com/chainguard-dev/terraform-infra-common/modules/cron/example
22

3-
go 1.24.0
3+
go 1.25
4+
5+
require (
6+
github.com/chainguard-dev/clog v1.8.0
7+
github.com/sethvargo/go-envconfig v1.3.0
8+
)

modules/cron/example/go.sum

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
github.com/chainguard-dev/clog v1.8.0 h1:frlTMEdg3XQR+ioQ6O9i92uigY8GTUcWKpuCFkhcCHA=
2+
github.com/chainguard-dev/clog v1.8.0/go.mod h1:5MQOZi+Iu7fV7GcJG8ag8rCB5elEOpqRMKEASgnGVdo=
3+
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
4+
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
5+
github.com/sethvargo/go-envconfig v1.3.0 h1:gJs+Fuv8+f05omTpwWIu6KmuseFAXKrIaOZSh8RMt0U=
6+
github.com/sethvargo/go-envconfig v1.3.0/go.mod h1:JLd0KFWQYzyENqnEPWWZ49i4vzZo/6nRidxI8YvGiHw=

modules/cron/example/main.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,23 @@ SPDX-License-Identifier: Apache-2.0
66
package main
77

88
import (
9-
"log"
9+
"context"
1010
"os"
11+
"os/signal"
12+
"syscall"
1113
"time"
14+
15+
"github.com/chainguard-dev/clog"
16+
"github.com/sethvargo/go-envconfig"
1217
)
1318

14-
func main() { log.Println("hello the time is now", time.Now(), os.Getenv("EXAMPLE_ENV")) } //nolint:gosec // G706: example app logging operational data
19+
var env = envconfig.MustProcess(context.Background(), &struct {
20+
ExampleEnv string `env:"EXAMPLE_ENV"`
21+
}{})
22+
23+
func main() {
24+
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
25+
defer cancel()
26+
27+
clog.InfoContextf(ctx, "hello the time is now %v %s", time.Now(), env.ExampleEnv) //nolint:gosec // G706: example app logging operational data
28+
}

modules/github-bots/sdk/bot.go

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ import (
99
"context"
1010
"fmt"
1111
"net/http"
12+
"os"
1213
"runtime/debug"
14+
"strconv"
1315

1416
"github.com/chainguard-dev/clog"
1517
_ "github.com/chainguard-dev/clog/gcp/init" // enable GCP logging
@@ -18,7 +20,6 @@ import (
1820
mce "github.com/chainguard-dev/terraform-infra-common/pkg/httpmetrics/cloudevents"
1921
cloudevents "github.com/cloudevents/sdk-go/v2"
2022
"github.com/google/go-github/v84/github"
21-
"github.com/sethvargo/go-envconfig"
2223
)
2324

2425
// Define a type for keys used in context to prevent key collisions.
@@ -65,11 +66,35 @@ func (b *Bot) RegisterHandler(handler EventHandlerFunc) {
6566
b.Handlers[etype] = handler
6667
}
6768

68-
var env = envconfig.MustProcess(context.Background(), &struct {
69-
Port int `env:"PORT, default=8080"`
70-
}{})
69+
// ServeOption configures the Serve function.
70+
type ServeOption func(*serveConfig)
71+
72+
type serveConfig struct {
73+
port int
74+
}
75+
76+
// WithPort sets the port for the bot's HTTP server.
77+
// If not provided, the PORT environment variable is used, defaulting to 8080.
78+
func WithPort(port int) ServeOption {
79+
return func(c *serveConfig) {
80+
c.port = port
81+
}
82+
}
83+
84+
func Serve(b Bot, opts ...ServeOption) {
85+
cfg := &serveConfig{}
86+
for _, opt := range opts {
87+
opt(cfg)
88+
}
89+
if cfg.port == 0 {
90+
cfg.port = 8080
91+
if p := os.Getenv("PORT"); p != "" {
92+
if n, err := strconv.Atoi(p); err == nil {
93+
cfg.port = n
94+
}
95+
}
96+
}
7197

72-
func Serve(b Bot) {
7398
ctx := context.Background()
7499

75100
log := clog.FromContext(ctx)
@@ -83,13 +108,13 @@ func Serve(b Bot) {
83108
})
84109

85110
c, err := mce.NewClientHTTP(b.Name,
86-
cloudevents.WithPort(env.Port),
111+
cloudevents.WithPort(cfg.port),
87112
)
88113
if err != nil {
89114
clog.Fatalf("failed to create event client, %v", err)
90115
}
91116

92-
log.Infof("starting bot %s receiver on port %d", b.Name, env.Port)
117+
log.Infof("starting bot %s receiver on port %d", b.Name, cfg.port)
93118
if err := c.StartReceiver(ctx, func(ctx context.Context, event cloudevents.Event) error {
94119
clog.FromContext(ctx).With("event", event).Debugf("received event")
95120

0 commit comments

Comments
 (0)