forked from coder/coder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
144 lines (123 loc) · 3.26 KB
/
main.go
File metadata and controls
144 lines (123 loc) · 3.26 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package main
import (
"bytes"
"encoding/json"
"fmt"
"go/parser"
"go/token"
"io/fs"
"os"
"path"
"path/filepath"
"strings"
"github.com/gohugoio/hugo/parser/pageparser"
"golang.org/x/xerrors"
"github.com/coder/coder/v2/codersdk"
)
const (
examplesDir = "examples"
examplesSrc = "examples.go"
)
func main() {
if err := run(); err != nil {
panic(err)
}
}
func run() error {
fset := token.NewFileSet()
src, err := parser.ParseFile(fset, filepath.Join(examplesDir, examplesSrc), nil, parser.ParseComments)
if err != nil {
return err
}
var paths []string
for _, comment := range src.Comments {
for _, line := range comment.List {
if s, ok := parseEmbedTag(line.Text); ok && !strings.HasSuffix(s, ".json") {
paths = append(paths, s)
}
}
}
var examples []codersdk.TemplateExample
files := os.DirFS(examplesDir)
for _, name := range paths {
dir, err := fs.Stat(files, name)
if err != nil {
return err
}
if !dir.IsDir() {
continue
}
exampleID := dir.Name()
// Each one of these is a example!
readme, err := fs.ReadFile(files, path.Join(name, "README.md"))
if err != nil {
return xerrors.Errorf("example %q does not contain README.md", exampleID)
}
frontMatter, err := pageparser.ParseFrontMatterAndContent(bytes.NewReader(readme))
if err != nil {
return xerrors.Errorf("parse example %q front matter: %w", exampleID, err)
}
nameRaw, exists := frontMatter.FrontMatter["name"]
if !exists {
return xerrors.Errorf("example %q front matter does not contain name", exampleID)
}
name, valid := nameRaw.(string)
if !valid {
return xerrors.Errorf("example %q name isn't a string", exampleID)
}
descriptionRaw, exists := frontMatter.FrontMatter["description"]
if !exists {
return xerrors.Errorf("example %q front matter does not contain name", exampleID)
}
description, valid := descriptionRaw.(string)
if !valid {
return xerrors.Errorf("example %q description isn't a string", exampleID)
}
tags := []string{}
tagsRaw, exists := frontMatter.FrontMatter["tags"]
if exists {
tagsI, valid := tagsRaw.([]interface{})
if !valid {
return xerrors.Errorf("example %q tags isn't a slice: type %T", exampleID, tagsRaw)
}
for _, tagI := range tagsI {
tag, valid := tagI.(string)
if !valid {
return xerrors.Errorf("example %q tag isn't a string: type %T", exampleID, tagI)
}
tags = append(tags, tag)
}
}
var icon string
iconRaw, exists := frontMatter.FrontMatter["icon"]
if exists {
icon, valid = iconRaw.(string)
if !valid {
return xerrors.Errorf("example %q icon isn't a string", exampleID)
}
}
examples = append(examples, codersdk.TemplateExample{
ID: exampleID,
Name: name,
Description: description,
Icon: icon,
Tags: tags,
Markdown: string(frontMatter.Content),
// URL is set by examples/examples.go.
})
}
w := os.Stdout
_, err = fmt.Fprint(w, "// Code generated by examplegen. DO NOT EDIT.\n")
if err != nil {
return err
}
enc := json.NewEncoder(os.Stdout)
enc.SetIndent("", " ")
return enc.Encode(examples)
}
func parseEmbedTag(s string) (string, bool) {
if !strings.HasPrefix(s, "//go:embed") {
return "", false
}
return strings.TrimSpace(strings.TrimPrefix(s, "//go:embed")), true
}