forked from testcontainers/testcontainers-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
122 lines (99 loc) · 3.23 KB
/
main.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
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
package main
import (
"flag"
"fmt"
"html/template"
"os"
"path/filepath"
"strings"
"golang.org/x/text/cases"
"golang.org/x/text/language"
)
var nameVar string
var imageVar string
var templates = []string{
"docs_example.md", "example_test.go", "example.go", "go.mod", "go.sum", "Makefile", "tools.go",
}
func init() {
flag.StringVar(&nameVar, "name", "", "Name of the example, use camel-case when needed")
flag.StringVar(&imageVar, "image", "", "Fully-qualified name of the Docker image to be used by the example")
}
type Example struct {
Image string // fully qualified name of the Docker image
Name string
}
func (e *Example) Lower() string {
return strings.ToLower(e.Name)
}
func (e *Example) Title() string {
return cases.Title(language.Und, cases.NoLower).String(e.Lower())
}
func main() {
required := []string{"name", "image"}
flag.Parse()
seen := make(map[string]bool)
flag.Visit(func(f *flag.Flag) { seen[f.Name] = true })
for _, req := range required {
if !seen[req] {
// or possibly use `log.Fatalf` instead of:
fmt.Fprintf(os.Stderr, "missing required -%s argument/flag\n", req)
os.Exit(2) // the same exit code flag.Parse uses
}
}
examplesDir, err := filepath.Abs(filepath.Dir(nameVar))
if err != nil {
fmt.Printf(">> could not get the examples dir: %v\n", err)
os.Exit(1)
}
examplesDocsPath := filepath.Join(filepath.Dir(examplesDir), "docs", "examples")
err = generate(Example{Name: nameVar, Image: imageVar}, examplesDir, examplesDocsPath)
if err != nil {
fmt.Printf(">> error generating the example: %v\n", err)
os.Exit(1)
}
}
func generate(example Example, examplesDir string, docsDir string) error {
funcMap := template.FuncMap{
"ToLower": strings.ToLower,
"Title": cases.Title(language.Und, cases.NoLower).String,
"codeinclude": func(s string) template.HTML { return template.HTML(s) }, // escape HTML comments for codeinclude
}
// create the example dir
err := os.MkdirAll(examplesDir, 0700)
if err != nil {
return err
}
for _, tmpl := range templates {
name := tmpl + ".tmpl"
t, err := template.New(name).Funcs(funcMap).ParseFiles(filepath.Join("_template", name))
if err != nil {
return err
}
exampleLower := example.Lower()
// create a new file
var exampleFilePath string
if strings.EqualFold(tmpl, "docs_example.md") {
// docs example file will go into the docs directory
exampleFilePath = filepath.Join(docsDir, exampleLower+".md")
} else if strings.EqualFold(tmpl, "tools.go") {
// tools.go example file will go into the tools package
exampleFilePath = filepath.Join(examplesDir, exampleLower, "tools", tmpl)
} else {
exampleFilePath = filepath.Join(examplesDir, exampleLower, strings.ReplaceAll(tmpl, "example", exampleLower))
}
err = os.MkdirAll(filepath.Dir(exampleFilePath), 0777)
if err != nil {
return err
}
exampleFile, _ := os.Create(exampleFilePath)
defer exampleFile.Close()
err = t.ExecuteTemplate(exampleFile, name, example)
if err != nil {
return err
}
}
fmt.Println("Please go to", example.Lower(), "directory and execute 'go mod tidy' to synchronize the dependencies")
fmt.Println("Commit the modified files and submit a pull request to include them into the project")
fmt.Println("Thanks!")
return nil
}