|
| 1 | +// Copyright The OpenTelemetry Authors |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package main |
| 5 | + |
| 6 | +import ( |
| 7 | + "embed" |
| 8 | + "flag" |
| 9 | + "log" |
| 10 | + "os" |
| 11 | + "path/filepath" |
| 12 | + "strings" |
| 13 | + "text/template" |
| 14 | +) |
| 15 | + |
| 16 | +var ( |
| 17 | + out = flag.String("output", "./", "output directory") |
| 18 | + tag = flag.String("tag", "", "OpenTelemetry tagged version") |
| 19 | + |
| 20 | + //go:embed templates/*.tmpl |
| 21 | + rootFS embed.FS |
| 22 | +) |
| 23 | + |
| 24 | +// SemanticConventions are information about the semantic conventions being |
| 25 | +// generated. |
| 26 | +type SemanticConventions struct { |
| 27 | + // TagVer is the tagged version (i.e. v1.7.0 and not 1.7.0). |
| 28 | + TagVer string |
| 29 | +} |
| 30 | + |
| 31 | +func (sc SemanticConventions) SemVer() string { |
| 32 | + return strings.TrimPrefix(*tag, "v") |
| 33 | +} |
| 34 | + |
| 35 | +// render renders all templates to the dest directory using the data. |
| 36 | +func render(src, dest string, data *SemanticConventions) error { |
| 37 | + tmpls, err := template.ParseFS(rootFS, src) |
| 38 | + if err != nil { |
| 39 | + return err |
| 40 | + } |
| 41 | + for _, tmpl := range tmpls.Templates() { |
| 42 | + target := filepath.Join(dest, strings.TrimSuffix(tmpl.Name(), ".tmpl")) |
| 43 | + // nolint: gosec |
| 44 | + wr, err := os.Create(target) |
| 45 | + if err != nil { |
| 46 | + return err |
| 47 | + } |
| 48 | + |
| 49 | + err = tmpl.Execute(wr, data) |
| 50 | + if err != nil { |
| 51 | + return err |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + return nil |
| 56 | +} |
| 57 | + |
| 58 | +func main() { |
| 59 | + flag.Parse() |
| 60 | + |
| 61 | + if *tag == "" { |
| 62 | + log.Fatalf("invalid tag: %q", *tag) |
| 63 | + } |
| 64 | + |
| 65 | + sc := &SemanticConventions{TagVer: *tag} |
| 66 | + |
| 67 | + if err := render("templates/*.tmpl", *out, sc); err != nil { |
| 68 | + log.Fatal(err) |
| 69 | + } |
| 70 | +} |
0 commit comments