Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit84de60e

Browse files
committed
feat: generate typed ScopeName constants from RBAC permissions
Add build-time generation of typed Go constants for all RBAC scopenames to improve type safety and prevent typos when referencingscopes in code.- Add scopenames.gotmpl template for generating ScopeName constants- Extend scripts/typegen/main.go to support "scopenames" template- Generate coderd/rbac/scopes_constants_gen.go with all typed constants- Update Makefile to include generated file in build targets- Constants follow pattern Scope<Resource><Action> = "resource:action"
1 parent5ff503b commit84de60e

File tree

5 files changed

+545
-0
lines changed

5 files changed

+545
-0
lines changed

‎Makefile‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -646,6 +646,7 @@ GEN_FILES := \
646646
$(SITE_GEN_FILES)\
647647
coderd/rbac/object_gen.go\
648648
codersdk/rbacresources_gen.go\
649+
coderd/rbac/scopes_constants_gen.go\
649650
docs/admin/integrations/prometheus.md\
650651
docs/reference/cli/index.md\
651652
docs/admin/security/audit-logs.md\
@@ -695,6 +696,7 @@ gen/mark-fresh:
695696
site/src/api/typesGenerated.ts\
696697
coderd/rbac/object_gen.go\
697698
codersdk/rbacresources_gen.go\
699+
coderd/rbac/scopes_constants_gen.go\
698700
site/src/api/rbacresourcesGenerated.ts\
699701
site/src/api/countriesGenerated.ts\
700702
docs/admin/integrations/prometheus.md\
@@ -842,6 +844,15 @@ coderd/rbac/object_gen.go: scripts/typegen/rbacobject.gotmpl scripts/typegen/mai
842844
rmdir -v"$$tempdir"
843845
touch"$@"
844846

847+
coderd/rbac/scopes_constants_gen.go: scripts/typegen/scopenames.gotmpl scripts/typegen/main.go coderd/rbac/policy/policy.go
848+
# Generate typed low-level ScopeName constants from RBACPermissions
849+
# Write to a temp file first to avoid truncating the package during build
850+
# since the generator imports the rbac package.
851+
tempfile=$(shell mktemp /tmp/scopes_constants_gen.XXXXXX)
852+
go run ./scripts/typegen/main.go rbac scopenames>"$$tempfile"
853+
mv -v"$$tempfile" coderd/rbac/scopes_constants_gen.go
854+
touch"$@"
855+
845856
codersdk/rbacresources_gen.go: scripts/typegen/codersdk.gotmpl scripts/typegen/main.go coderd/rbac/object.go coderd/rbac/policy/policy.go
846857
# Do no overwrite codersdk/rbacresources_gen.go directly, as it would make the file empty, breaking
847858
# the `codersdk` package and any parallel build targets.

‎coderd/rbac/scopes.go‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package rbac
22

33
import (
44
"fmt"
5+
"slices"
56
"strings"
67

78
"github.com/google/uuid"
@@ -107,6 +108,18 @@ var builtinScopes = map[ScopeName]Scope{
107108
},
108109
}
109110

111+
// BuiltinScopeNames returns the list of built-in high-level scope names
112+
// defined in this package (e.g., "all", "application_connect"). The result
113+
// is sorted for deterministic ordering in code generation and tests.
114+
funcBuiltinScopeNames() []ScopeName {
115+
names:=make([]ScopeName,0,len(builtinScopes))
116+
forname:=rangebuiltinScopes {
117+
names=append(names,name)
118+
}
119+
slices.Sort(names)
120+
returnnames
121+
}
122+
110123
typeExpandableScopeinterface {
111124
Expand() (Scope,error)
112125
// Name is for logging and tracing purposes, we want to know the human

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp