Add ability to have custom content in OWNERS_ALIASES

This commit is contained in:
Christoph Blecker 2018-02-23 16:30:39 -08:00
parent b7ade6a479
commit d9b419fbff
No known key found for this signature in database
GPG Key ID: B34A59A9D39F838B
4 changed files with 65 additions and 37 deletions

View File

@ -128,3 +128,6 @@ aliases:
wg-resource-management-leads:
- vishh
- derekwaynecarr
## BEGIN CUSTOM CONTENT
## END CUSTOM CONTENT

View File

@ -41,8 +41,10 @@ const (
aliasesOutput = "OWNERS_ALIASES"
indexFilename = "README.md"
beginMarker = "<!-- BEGIN CUSTOM CONTENT -->"
endMarker = "<!-- END CUSTOM CONTENT -->"
beginCustomMarkdown = "<!-- BEGIN CUSTOM CONTENT -->"
endCustomMarkdown = "<!-- END CUSTOM CONTENT -->"
beginCustomYaml = "## BEGIN CUSTOM CONTENT"
endCustomYaml = "## END CUSTOM CONTENT"
)
var (
@ -129,10 +131,23 @@ func createDirIfNotExists(path string) error {
return nil
}
func getExistingContent(path string) (string, error) {
func getExistingContent(path string, fileFormat string) (string, error) {
capture := false
var captured []string
beginMarker := ""
endMarker := ""
switch fileFormat {
case "markdown":
beginMarker = beginCustomMarkdown
endMarker = endCustomMarkdown
case "yaml":
beginMarker = beginCustomYaml
endMarker = endCustomYaml
case "":
return "", nil
}
// NOTE: For some reason using bufio.Scanner with existing file pointer prepends
// a bunch of null ^@ characters, so using to ioutil.ReadFile instead.
content, err := ioutil.ReadFile(path)
@ -166,7 +181,7 @@ func tzUrlEncode(tz string) string {
return strings.Replace(url.QueryEscape(tz), "+", "%20", -1)
}
func writeTemplate(templatePath, outputPath string, customContent bool, data interface{}) error {
func writeTemplate(templatePath, outputPath string, fileFormat string, data interface{}) error {
// set up template
t, err := template.New(filepath.Base(templatePath)).
Funcs(funcMap).
@ -191,7 +206,7 @@ func writeTemplate(templatePath, outputPath string, customContent bool, data int
defer f.Close()
// get any existing content
content, err := getExistingContent(outputPath)
content, err := getExistingContent(outputPath, fileFormat)
if err != nil {
return err
}
@ -205,15 +220,25 @@ func writeTemplate(templatePath, outputPath string, customContent bool, data int
return err
}
// custom content block
if customContent {
writeCustomContentBlock(f, content)
}
writeCustomContentBlock(f, content, fileFormat)
return nil
}
func writeCustomContentBlock(f *os.File, content string) {
func writeCustomContentBlock(f *os.File, content string, fileFormat string) {
beginMarker := ""
endMarker := ""
switch fileFormat {
case "markdown":
beginMarker = beginCustomMarkdown
endMarker = endCustomMarkdown
case "yaml":
beginMarker = beginCustomYaml
endMarker = endCustomYaml
case "":
return
}
lines := []string{beginMarker, "\n", content, "\n", endMarker, "\n"}
for _, line := range lines {
f.Write([]byte(line))
@ -244,7 +269,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, true, group); err != nil {
if err := writeTemplate(readmePath, outputPath, "markdown", group); err != nil {
return err
}
}
@ -284,14 +309,14 @@ func main() {
fmt.Println("Generating sig-list.md")
outputPath := filepath.Join(baseGeneratorDir, sigListOutput)
err = writeTemplate(filepath.Join(baseGeneratorDir, templateDir, listTemplate), outputPath, true, ctx)
err = writeTemplate(filepath.Join(baseGeneratorDir, templateDir, listTemplate), outputPath, "markdown", 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)
err = writeTemplate(filepath.Join(baseGeneratorDir, templateDir, aliasesTemplate), outputPath, "yaml", ctx)
if err != nil {
log.Fatal(err)
}

View File

@ -67,7 +67,7 @@ func TestGetExistingData(t *testing.T) {
}
for _, c := range cases {
content, err := getExistingContent(c.path)
content, err := getExistingContent(c.path, "markdown")
if err != nil && c.expectErr == false {
t.Fatalf("Received unexpected error for %s: %v", c.path, err)
}
@ -94,38 +94,38 @@ content!
`
cases := []struct {
templatePath string
outputPath string
data map[string]string
expectErr bool
customContent bool
expected string
templatePath string
outputPath string
data map[string]string
expectErr bool
fileFormat string
expected string
}{
{
templatePath: "./testdata/non_existent_template.tmpl",
expectErr: true,
customContent: true,
templatePath: "./testdata/non_existent_template.tmpl",
expectErr: true,
fileFormat: "markdown",
},
{
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: "/tmp/non_existing_path.md",
expectErr: false,
fileFormat: "markdown",
data: map[string]string{"Message": "Hello!"},
expected: "Hello!",
},
{
templatePath: "./testdata/example.tmpl",
outputPath: "./testdata/example.md",
expectErr: false,
customContent: true,
data: map[string]string{"Message": "Hello!"},
expected: customContent,
templatePath: "./testdata/example.tmpl",
outputPath: "./testdata/example.md",
expectErr: false,
fileFormat: "markdown",
data: map[string]string{"Message": "Hello!"},
expected: customContent,
},
}
for _, c := range cases {
err := writeTemplate(c.templatePath, c.outputPath, c.customContent, c.data)
err := writeTemplate(c.templatePath, c.outputPath, c.fileFormat, c.data)
if err != nil && c.expectErr == false {
t.Fatalf("Received unexpected error for %s: %v", c.templatePath, err)
}

View File

@ -19,7 +19,7 @@ make 1>/dev/null
mismatches=0
break=$(printf "=%.0s" $(seq 1 68))
for file in $(ls ${CRT_DIR}/sig-*/README.md ${CRT_DIR}/wg-*/README.md ${CRT_DIR}/sig-list.md); do
for file in $(ls ${CRT_DIR}/sig-*/README.md ${CRT_DIR}/wg-*/README.md ${CRT_DIR}/sig-list.md ${CRT_DIR}/OWNERS_ALIASES); do
real=${file#$CRT_DIR/}
if ! diff -q ${file} ${WORKING_DIR}/${real} &>/dev/null; then
echo "${file} does not match ${WORKING_DIR}/${real}";