func verifyBoilerplate()

in hack/verify-boilerplate.go [89:135]


func verifyBoilerplate(contents string) error {
	idx := 0
	foundBoilerplateStart := false
	lines := strings.Split(contents, "\n")
	for _, line := range lines {
		// handle leading comments
		line = trimLeadingComment(line, "//")
		line = trimLeadingComment(line, "#")

		// find the start of the boilerplate
		bpLine := boilerPlate[idx]
		if strings.Contains(line, boilerPlateStart) {
			foundBoilerplateStart = true

			// validate the year of the copyright
			yearWords := strings.Split(line, " ")
			expectedLen := len(strings.Split(boilerPlate[0], " "))
			if len(yearWords) != expectedLen {
				return fmt.Errorf("copyright line should contain exactly %d words", expectedLen)
			}
			if !yearRegexp.MatchString(yearWords[1]) {
				return fmt.Errorf("cannot parse the year in the copyright line")
			}
			bpLine = strings.ReplaceAll(bpLine, yearPlaceholder, yearWords[1])
		}

		// match line by line
		if foundBoilerplateStart {
			if line != bpLine {
				return fmt.Errorf("boilerplate line %d does not match\nexpected: %q\ngot: %q", idx+1, bpLine, line)
			}
			idx++
			// exit after the last line is found
			if strings.Index(line, boilerPlateEnd) == 0 {
				break
			}
		}
	}

	if !foundBoilerplateStart {
		return errors.New("the file is missing a boilerplate")
	}
	if idx < len(boilerPlate) {
		return errors.New("boilerplate has missing lines")
	}
	return nil
}