|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | +"context" |
| 5 | +"fmt" |
| 6 | +"go/types" |
| 7 | +"io" |
| 8 | +"os" |
| 9 | +"reflect" |
| 10 | +"strings" |
| 11 | + |
| 12 | +"golang.org/x/tools/go/packages" |
| 13 | +"golang.org/x/xerrors" |
| 14 | + |
| 15 | +"cdr.dev/slog" |
| 16 | +"cdr.dev/slog/sloggers/sloghuman" |
| 17 | +) |
| 18 | + |
| 19 | +funcmain() { |
| 20 | +ctx:=context.Background() |
| 21 | +log:=slog.Make(sloghuman.Sink(os.Stderr)) |
| 22 | +code,err:=GenerateFromDirectory(ctx,os.Args[1],os.Args[2:]...) |
| 23 | +iferr!=nil { |
| 24 | +log.Fatal(ctx,"generate",slog.Error(err)) |
| 25 | +} |
| 26 | + |
| 27 | +_,_=fmt.Print(code) |
| 28 | +} |
| 29 | + |
| 30 | +// GenerateFromDirectory will return all the typescript code blocks for a directory |
| 31 | +funcGenerateFromDirectory(ctx context.Context,directorystring,typeNames...string) (string,error) { |
| 32 | +g:=Generator{} |
| 33 | +err:=g.parsePackage(ctx,directory) |
| 34 | +iferr!=nil { |
| 35 | +return"",xerrors.Errorf("parse package %q: %w",directory,err) |
| 36 | +} |
| 37 | + |
| 38 | +str,err:=g.generate(typeNames...) |
| 39 | +iferr!=nil { |
| 40 | +return"",xerrors.Errorf("parse package %q: %w",directory,err) |
| 41 | +} |
| 42 | + |
| 43 | +returnstr,nil |
| 44 | +} |
| 45 | + |
| 46 | +typeGeneratorstruct { |
| 47 | +// Package we are scanning. |
| 48 | +pkg*packages.Package |
| 49 | +} |
| 50 | + |
| 51 | +// parsePackage takes a list of patterns such as a directory, and parses them. |
| 52 | +func (g*Generator)parsePackage(ctx context.Context,patterns...string)error { |
| 53 | +cfg:=&packages.Config{ |
| 54 | +// Just accept the fact we need these flags for what we want. Feel free to add |
| 55 | +// more, it'll just increase the time it takes to parse. |
| 56 | +Mode:packages.NeedTypes|packages.NeedName|packages.NeedTypesInfo| |
| 57 | +packages.NeedTypesSizes|packages.NeedSyntax, |
| 58 | +Tests:false, |
| 59 | +Context:ctx, |
| 60 | +} |
| 61 | + |
| 62 | +pkgs,err:=packages.Load(cfg,patterns...) |
| 63 | +iferr!=nil { |
| 64 | +returnxerrors.Errorf("load package: %w",err) |
| 65 | +} |
| 66 | + |
| 67 | +// Only support 1 package for now. We can expand it if we need later, we |
| 68 | +// just need to hook up multiple packages in the generator. |
| 69 | +iflen(pkgs)!=1 { |
| 70 | +returnxerrors.Errorf("expected 1 package, found %d",len(pkgs)) |
| 71 | +} |
| 72 | + |
| 73 | +g.pkg=pkgs[0] |
| 74 | +returnnil |
| 75 | +} |
| 76 | + |
| 77 | +func (g*Generator)generate(typeNames...string) (string,error) { |
| 78 | +sb:= strings.Builder{} |
| 79 | + |
| 80 | +_,_=fmt.Fprint(&sb,"Copy the following code into the audit.AuditableResources table\n\n") |
| 81 | + |
| 82 | +for_,typName:=rangetypeNames { |
| 83 | +obj:=g.pkg.Types.Scope().Lookup(typName) |
| 84 | +ifobj==nil||obj.Type()==nil { |
| 85 | +return"",xerrors.Errorf("type doesn't exist %q",typName) |
| 86 | +} |
| 87 | + |
| 88 | +switchobj:=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 | +switchtyp:=named.Underlying().(type) { |
| 96 | +case*types.Struct: |
| 97 | +g.writeStruct(&sb,typ,typName) |
| 98 | + |
| 99 | +default: |
| 100 | +return"",xerrors.Errorf("invalid type %T",obj) |
| 101 | +} |
| 102 | +default: |
| 103 | +return"",xerrors.Errorf("invalid type %T",obj) |
| 104 | +} |
| 105 | +} |
| 106 | + |
| 107 | +returnsb.String(),nil |
| 108 | +} |
| 109 | + |
| 110 | +func (*Generator)writeStruct(w io.Writer,st*types.Struct,namestring) { |
| 111 | +_,_=fmt.Fprintf(w,"\t&database.%s{}: {\n",name) |
| 112 | + |
| 113 | +fori:=0;i<st.NumFields();i++ { |
| 114 | +_,_=fmt.Fprintf(w,"\t\t\"%s\": ActionIgnore, // TODO: why\n",reflect.StructTag(st.Tag(i)).Get("json")) |
| 115 | +} |
| 116 | + |
| 117 | +_,_=fmt.Fprint(w,"\t},\n") |
| 118 | +} |