-
Notifications
You must be signed in to change notification settings - Fork 644
/
Copy pathgen.go
58 lines (51 loc) · 1.32 KB
/
gen.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
//go:build ignore
package main
import (
"bytes"
"context"
"html/template"
"io"
"log"
"os"
"github.com/github/go-pipe/pipe"
"github.com/grafana/pyroscope/pkg/querier/golang"
)
func main() {
// todo: In the future we might want to support more than one version
// Or even list all files from standard packages to improve matching.
packages, err := golang.StdPackages("")
if err != nil {
log.Fatal(err)
}
t := template.Must(template.New("packages").Parse(packagesTemplate))
var buff bytes.Buffer
p := pipe.New(pipe.WithStdout(&buff))
p.Add(
pipe.Function("", func(ctx context.Context, env pipe.Env, stdin io.Reader, stdout io.Writer) error {
err = t.Execute(stdout, packages)
if err != nil {
log.Fatal(err)
}
return nil
}),
// This might be a bit overkill, but it's nice to have a consistent format.
// todo: We could use "go/format" package only that will simplify the code but also remove the needs
// for expected installed binaries.
pipe.Command("gofmt"),
pipe.Command("goimports"),
)
p.Run(context.Background())
err = os.WriteFile("packages_gen.go", buff.Bytes(), 0666)
if err != nil {
log.Fatal(err)
}
}
var packagesTemplate = `
package golang
// Code generated. DO NOT EDIT.
var StandardPackages = map[string]struct{}{
{{- range $key, $value := .}}
"{{$key}}": {},
{{- end}}
}
`