Skip to content

Commit

Permalink
Generate OWNERS_ALIASES from sigs.yaml
Browse files Browse the repository at this point in the history
  • Loading branch information
cblecker committed Jan 25, 2018
1 parent 540b01e commit 696f1da
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 25 deletions.
13 changes: 13 additions & 0 deletions generator/aliases.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
aliases:
{{- range .Sigs}}
{{.Dir}}-leads:
{{- range .Leads}}
- {{.GitHub}}
{{- end}}
{{- end}}
{{- range .WorkingGroups}}
{{.Dir}}-leads:
{{- range .Leads}}
- {{.GitHub}}
{{- end}}
{{- end}}
25 changes: 18 additions & 7 deletions generator/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,14 @@ import (
)

const (
readmeTemplate = "readme.tmpl"
listTemplate = "list.tmpl"
headerTemplate = "header.tmpl"
readmeTemplate = "readme.tmpl"
listTemplate = "list.tmpl"
aliasesTemplate = "aliases.tmpl"
headerTemplate = "header.tmpl"

sigsYamlFile = "sigs.yaml"
sigListOutput = "sig-list.md"
aliasesOutput = "OWNERS_ALIASES"
indexFilename = "README.md"

beginMarker = "<!-- BEGIN CUSTOM CONTENT -->"
Expand Down Expand Up @@ -154,7 +156,7 @@ func tzUrlEncode(tz string) string {
return strings.Replace(url.QueryEscape(tz), "+", "%20", -1)
}

func writeTemplate(templatePath, outputPath string, data interface{}) error {
func writeTemplate(templatePath, outputPath string, customContent bool, data interface{}) error {
// set up template
t, err := template.New(filepath.Base(templatePath)).
Funcs(funcMap).
Expand Down Expand Up @@ -194,7 +196,9 @@ func writeTemplate(templatePath, outputPath string, data interface{}) error {
}

// custom content block
writeCustomContentBlock(f, content)
if customContent {
writeCustomContentBlock(f, content)
}

return nil
}
Expand Down Expand Up @@ -230,7 +234,7 @@ func createGroupReadme(groups []Group, prefix string) error {

outputPath := filepath.Join(outputDir, indexFilename)
readmePath := filepath.Join(baseGeneratorDir, templateDir, fmt.Sprintf("%s_%s", prefix, readmeTemplate))
if err := writeTemplate(readmePath, outputPath, group); err != nil {
if err := writeTemplate(readmePath, outputPath, true, group); err != nil {
return err
}
}
Expand Down Expand Up @@ -270,7 +274,14 @@ func main() {

fmt.Println("Generating sig-list.md")
outputPath := filepath.Join(baseGeneratorDir, sigListOutput)
err = writeTemplate(filepath.Join(baseGeneratorDir, templateDir, listTemplate), outputPath, ctx)
err = writeTemplate(filepath.Join(baseGeneratorDir, templateDir, listTemplate), outputPath, true, ctx)
if err != nil {
log.Fatal(err)
}

fmt.Println("Generating OWNERS_ALIASES")
outputPath = filepath.Join(baseGeneratorDir, aliasesOutput)
err = writeTemplate(filepath.Join(baseGeneratorDir, templateDir, aliasesTemplate), outputPath, false, ctx)
if err != nil {
log.Fatal(err)
}
Expand Down
40 changes: 22 additions & 18 deletions generator/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,34 +94,38 @@ content!
`

cases := []struct {
templatePath string
outputPath string
data map[string]string
expectErr bool
expected string
templatePath string
outputPath string
data map[string]string
expectErr bool
customContent bool
expected string
}{
{
templatePath: "./testdata/non_existent_template.tmpl",
expectErr: true,
templatePath: "./testdata/non_existent_template.tmpl",
expectErr: true,
customContent: true,
},
{
templatePath: "./testdata/example.tmpl",
outputPath: "/tmp/non_existing_path.md",
expectErr: false,
data: map[string]string{"Message": "Hello!"},
expected: "Hello!",
templatePath: "./testdata/example.tmpl",
outputPath: "/tmp/non_existing_path.md",
expectErr: false,
customContent: true,
data: map[string]string{"Message": "Hello!"},
expected: "Hello!",
},
{
templatePath: "./testdata/example.tmpl",
outputPath: "./testdata/example.md",
expectErr: false,
data: map[string]string{"Message": "Hello!"},
expected: customContent,
templatePath: "./testdata/example.tmpl",
outputPath: "./testdata/example.md",
expectErr: false,
customContent: true,
data: map[string]string{"Message": "Hello!"},
expected: customContent,
},
}

for _, c := range cases {
err := writeTemplate(c.templatePath, c.outputPath, c.data)
err := writeTemplate(c.templatePath, c.outputPath, c.customContent, c.data)
if err != nil && c.expectErr == false {
t.Fatalf("Received unexpected error for %s: %v", c.templatePath, err)
}
Expand Down

0 comments on commit 696f1da

Please sign in to comment.