Skip to content

Commit d6bf766

Browse files
iyabchenBBBmau
authored andcommitted
Add a copy file header to state the source file for handwritten files (GoogleCloudPlatform#13725)
1 parent 8ffa156 commit d6bf766

File tree

1 file changed

+63
-1
lines changed

1 file changed

+63
-1
lines changed

mmv1/provider/terraform.go

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,9 @@ func (t Terraform) CopyFileList(outputFolder string, files map[string]string, ge
423423
if filepath.Ext(target) == ".go" || (filepath.Ext(target) == ".mod" && generateCode) {
424424
t.replaceImportPath(outputFolder, target)
425425
}
426-
426+
if filepath.Ext(target) == ".go" || filepath.Ext(target) == ".markdown" {
427+
t.addCopyfileHeader(source, outputFolder, target)
428+
}
427429
if filepath.Ext(target) == ".go" {
428430
t.addHashicorpCopyRightHeader(outputFolder, target)
429431
}
@@ -523,10 +525,70 @@ func (t Terraform) CompileFileList(outputFolder string, files map[string]string,
523525
continue
524526
}
525527
t.replaceImportPath(outputFolder, target)
528+
if filepath.Ext(targetFile) == ".go" || filepath.Ext(targetFile) == ".markdown" {
529+
t.addCopyfileHeader(source, outputFolder, target)
530+
}
526531
t.addHashicorpCopyRightHeader(outputFolder, target)
527532
}
528533
}
529534

535+
func (t Terraform) addCopyfileHeader(srcpath, outputFolder, target string) {
536+
githubPrefix := "https://github.com/GoogleCloudPlatform/magic-modules/tree/main/mmv1/"
537+
if !strings.HasPrefix(srcpath, githubPrefix) {
538+
srcpath = githubPrefix + srcpath
539+
}
540+
541+
targetFile := filepath.Join(outputFolder, target)
542+
sourceByte, err := os.ReadFile(targetFile)
543+
if err != nil {
544+
log.Fatalf("Cannot read file %s to add copy file header: %s", targetFile, err)
545+
}
546+
547+
srcStr := string(sourceByte)
548+
if strings.Contains(srcStr, "*** AUTO GENERATED CODE *** Type: Handwritten ***") {
549+
return
550+
}
551+
552+
templateFormat := `// ----------------------------------------------------------------------------
553+
//
554+
// *** AUTO GENERATED CODE *** Type: Handwritten ***
555+
//
556+
// ----------------------------------------------------------------------------
557+
//
558+
// This code is generated by Magic Modules using the following:
559+
//
560+
// Source file: %s
561+
//
562+
// DO NOT EDIT this file directly. Any changes made to this file will be
563+
// overwritten during the next generation cycle.
564+
//
565+
// ----------------------------------------------------------------------------
566+
%s`
567+
content := srcStr
568+
if filepath.Ext(target) == ".markdown" {
569+
// insert the header after ---
570+
templateFormat = "---\n" + strings.Replace(templateFormat, "//", "#", -1)
571+
content = strings.TrimPrefix(srcStr, "---\n")
572+
}
573+
574+
fileStr := fmt.Sprintf(templateFormat, srcpath, content)
575+
576+
sourceByte = []byte(fileStr)
577+
// format go file
578+
if filepath.Ext(targetFile) == ".go" {
579+
sourceByte, err = format.Source(sourceByte)
580+
if err != nil {
581+
log.Printf("error formatting %s: %s\n", targetFile, err)
582+
return
583+
}
584+
}
585+
586+
err = os.WriteFile(targetFile, sourceByte, 0644)
587+
if err != nil {
588+
log.Fatalf("Cannot write file %s to add copy file header: %s", target, err)
589+
}
590+
}
591+
530592
func (t Terraform) addHashicorpCopyRightHeader(outputFolder, target string) {
531593
if !expectedOutputFolder(outputFolder) {
532594
log.Printf("Unexpected output folder (%s) detected "+

0 commit comments

Comments
 (0)