Merge pull request #1849 from cblecker/custom-aliases

Add ability to have custom content in OWNERS_ALIASES
This commit is contained in:
k8s-ci-robot 2018-02-26 16:26:44 -08:00 committed by GitHub
commit 6e3e64ffb3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 65 additions and 37 deletions

View File

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

View File

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

View File

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

View File

@ -19,7 +19,7 @@ make 1>/dev/null
mismatches=0 mismatches=0
break=$(printf "=%.0s" $(seq 1 68)) 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/} real=${file#$CRT_DIR/}
if ! diff -q ${file} ${WORKING_DIR}/${real} &>/dev/null; then if ! diff -q ${file} ${WORKING_DIR}/${real} &>/dev/null; then
echo "${file} does not match ${WORKING_DIR}/${real}"; echo "${file} does not match ${WORKING_DIR}/${real}";