@@ -19,7 +19,7 @@ import (
19
19
func main () {
20
20
ctx := context .Background ()
21
21
log := slog .Make (sloghuman .Sink (os .Stderr ))
22
- code , err := GenerateFromDirectory (ctx , os .Args [1 ], os .Args [2 ] )
22
+ code , err := GenerateFromDirectory (ctx , os .Args [1 ], os .Args [2 :] ... )
23
23
if err != nil {
24
24
log .Fatal (ctx , "generate" , slog .Error (err ))
25
25
}
@@ -28,14 +28,14 @@ func main() {
28
28
}
29
29
30
30
// GenerateFromDirectory will return all the typescript code blocks for a directory
31
- func GenerateFromDirectory (ctx context.Context , directory , typName string ) (string , error ) {
31
+ func GenerateFromDirectory (ctx context.Context , directory string , typeNames ... string ) (string , error ) {
32
32
g := Generator {}
33
33
err := g .parsePackage (ctx , directory )
34
34
if err != nil {
35
35
return "" , xerrors .Errorf ("parse package %q: %w" , directory , err )
36
36
}
37
37
38
- str , err := g .generate (typName )
38
+ str , err := g .generate (typeNames ... )
39
39
if err != nil {
40
40
return "" , xerrors .Errorf ("parse package %q: %w" , directory , err )
41
41
}
@@ -74,38 +74,40 @@ func (g *Generator) parsePackage(ctx context.Context, patterns ...string) error
74
74
return nil
75
75
}
76
76
77
- func (g * Generator ) generate (typName string ) (string , error ) {
77
+ func (g * Generator ) generate (typeNames ... string ) (string , error ) {
78
78
sb := strings.Builder {}
79
79
80
80
_ , _ = fmt .Fprint (& sb , "Copy the following code into the audit.AuditableResources table\n \n " )
81
81
82
- obj := g .pkg .Types .Scope ().Lookup (typName )
83
- if obj == nil || obj .Type () == nil {
84
- return "" , xerrors .Errorf ("type doesn't exist %q" , typName )
85
- }
86
-
87
- switch obj := obj .(type ) {
88
- case * types.TypeName :
89
- named , ok := obj .Type ().(* types.Named )
90
- if ! ok {
91
- panic ("all typenames should be named types" )
82
+ for _ , typName := range typeNames {
83
+ obj := g .pkg .Types .Scope ().Lookup (typName )
84
+ if obj == nil || obj .Type () == nil {
85
+ return "" , xerrors .Errorf ("type doesn't exist %q" , typName )
92
86
}
93
87
94
- switch typ := named .Underlying ().(type ) {
95
- case * types.Struct :
96
- g .writeStruct (& sb , typ , typName )
88
+ switch obj := obj .(type ) {
89
+ case * types.TypeName :
90
+ named , ok := obj .Type ().(* types.Named )
91
+ if ! ok {
92
+ panic ("all typenames should be named types" )
93
+ }
94
+
95
+ switch typ := named .Underlying ().(type ) {
96
+ case * types.Struct :
97
+ g .writeStruct (& sb , typ , typName )
97
98
99
+ default :
100
+ return "" , xerrors .Errorf ("invalid type %T" , obj )
101
+ }
98
102
default :
99
103
return "" , xerrors .Errorf ("invalid type %T" , obj )
100
104
}
101
- default :
102
- return "" , xerrors .Errorf ("invalid type %T" , obj )
103
105
}
104
106
105
107
return sb .String (), nil
106
108
}
107
109
108
- func (g * Generator ) writeStruct (w io.Writer , st * types.Struct , name string ) {
110
+ func (* Generator ) writeStruct (w io.Writer , st * types.Struct , name string ) {
109
111
_ , _ = fmt .Fprintf (w , "\t &database.%s{}: {\n " , name )
110
112
111
113
for i := 0 ; i < st .NumFields (); i ++ {
0 commit comments