func()

in mmv1/provider/terraform.go [514:578]


func (t Terraform) addHashicorpCopyRightHeader(outputFolder, target string) {
	if !expectedOutputFolder(outputFolder) {
		log.Printf("Unexpected output folder (%s) detected "+
			"when deciding to add HashiCorp copyright headers.\n"+
			"Watch out for unexpected changes to copied files", outputFolder)
	}
	// only add copyright headers when generating TPG and TPGB
	if !(strings.HasSuffix(outputFolder, "terraform-provider-google") || strings.HasSuffix(outputFolder, "terraform-provider-google-beta")) {
		return
	}

	// Prevent adding copyright header to files with paths or names matching the strings below
	// NOTE: these entries need to match the content of the .copywrite.hcl file originally
	//       created in https://github.com/GoogleCloudPlatform/magic-modules/pull/7336
	//       The test-fixtures folder is not included here as it's copied as a whole,
	//       not file by file
	ignoredFolders := []string{".release/", ".changelog/", "examples/", "scripts/", "META.d/"}
	ignoredFiles := []string{"go.mod", ".goreleaser.yml", ".golangci.yml", "terraform-registry-manifest.json", "_meta.yaml"}
	shouldAddHeader := true
	for _, folder := range ignoredFolders {
		// folder will be path leading to file
		if strings.HasPrefix(target, folder) {
			shouldAddHeader = false
			break
		}
	}
	if !shouldAddHeader {
		return
	}

	for _, file := range ignoredFiles {
		// file will be the filename and extension, with no preceding path
		if strings.HasSuffix(target, file) {
			shouldAddHeader = false
			break
		}
	}
	if !shouldAddHeader {
		return
	}

	lang := languageFromFilename(target)
	// Some file types we don't want to add headers to
	// e.g. .sh where headers are functional
	// Also, this guards against new filetypes being added and triggering build errors
	if lang == "unsupported" {
		return
	}

	// File is not ignored and is appropriate file type to add header to
	copyrightHeader := []string{"Copyright (c) HashiCorp, Inc.", "SPDX-License-Identifier: MPL-2.0"}
	header := commentBlock(copyrightHeader, lang)

	targetFile := filepath.Join(outputFolder, target)
	sourceByte, err := os.ReadFile(targetFile)
	if err != nil {
		log.Fatalf("Cannot read file %s to add Hashicorp copy right: %s", targetFile, err)
	}

	sourceByte = google.Concat([]byte(header), sourceByte)
	err = os.WriteFile(targetFile, sourceByte, 0644)
	if err != nil {
		log.Fatalf("Cannot write file %s to add Hashicorp copy right: %s", target, err)
	}
}