-
Notifications
You must be signed in to change notification settings - Fork 1
/
generate-workflow.go
76 lines (62 loc) · 1.95 KB
/
generate-workflow.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
package main
import (
"bytes"
"flag"
"fmt"
"io"
"os"
"strings"
"text/template"
"github.com/goware/prefixer"
"github.com/lithammer/dedent"
log "github.com/sirupsen/logrus"
)
var (
fCronExpr string
fOwnerRepo string
fPackage string
fTemplate string
dockerfile *template.Template
)
func init() {
flag.StringVar(&fCronExpr, "c", "", "The cron expression for a scheduled build.")
flag.StringVar(&fOwnerRepo, "r", "", "The GitHub owner/repo to lookup.")
flag.StringVar(&fPackage, "p", "", "The name of the package to create stubs for.")
flag.StringVar(&fTemplate, "t", "", "The name of the template file to use.")
flag.Parse()
}
func main() {
dockerfile = template.Must(template.ParseFiles(fTemplate))
doNotEditWarning := strings.TrimSpace(dedent.Dedent(`
DO NOT EDIT THIS FILE!
1. Edit the *.gotmpl.yml files instead.
2. go run generate-workflow.go -t ` + fTemplate + ` -p ` + fPackage + ` -r '` + fOwnerRepo + `' -c '` + fCronExpr + `'
`))
dockerfileFile, err := os.Create(fmt.Sprintf(".github/workflows/build-%s.yml", fPackage))
if err != nil {
log.Fatal(err)
}
// Do some magic to convert the DO NOT EDIT warning into a valid Dockerfile comment.
buf := bytes.NewBufferString(doNotEditWarning)
prefixReader := prefixer.New(buf, "# ")
commentedBuf, err := io.ReadAll(prefixReader)
if err != nil {
log.Fatal(err)
}
doNotEditStringValue := strings.TrimSpace(dedent.Dedent(`
################################################################################
%s
################################################################################
`))
// Take the template, execute it using variables we pass-in from the
// JSON config, and write the result to the new Dockerfile.
err = dockerfile.Execute(dockerfileFile, map[string]interface{}{
"CronExpr": fCronExpr,
"OwnerRepo": fOwnerRepo,
"Package": fPackage,
"DoNotEdit": fmt.Sprintf(doNotEditStringValue, string(commentedBuf)),
})
if err != nil {
log.Fatal(err)
}
}