diff --git a/cmd/hz/generator/custom_files.go b/cmd/hz/generator/custom_files.go index 1315948f1..5c7fcb0b9 100644 --- a/cmd/hz/generator/custom_files.go +++ b/cmd/hz/generator/custom_files.go @@ -369,7 +369,7 @@ func (pkgGen *HttpPackageGenerator) genLoopService(tplInfo *Template, filePathRe } logs.Infof("append content for file '%s', because the update behavior is 'Append' and appendKey is 'method'", filePath) pkgGen.files = append(pkgGen.files, File{filePath, buf.String(), false, ""}) - } else { // 'append location', append new content after 'append location' + } else { // 'append location', append new content before or after 'append location' part := bytes.Split(fileContent, []byte(tplInfo.UpdateBehavior.AppendLocation)) if len(part) == 0 { return fmt.Errorf("can not find append location '%s' for file '%s'\n", tplInfo.UpdateBehavior.AppendLocation, filePath) @@ -378,7 +378,11 @@ func (pkgGen *HttpPackageGenerator) genLoopService(tplInfo *Template, filePathRe return fmt.Errorf("do not support multiple append location '%s' for file '%s'\n", tplInfo.UpdateBehavior.AppendLocation, filePath) } buf := bytes.NewBuffer(nil) - err = writeBytes(buf, part[0], []byte(tplInfo.UpdateBehavior.AppendLocation), appendContent, part[1]) + if tplInfo.UpdateBehavior.AppendDirection == Before { + err = writeBytes(buf, part[0], appendContent, []byte(tplInfo.UpdateBehavior.AppendLocation), part[1]) + } else { + err = writeBytes(buf, part[0], []byte(tplInfo.UpdateBehavior.AppendLocation), appendContent, part[1]) + } if err != nil { return fmt.Errorf("write file(%s) failed, err: %v", tplInfo.Path, err) } @@ -546,7 +550,7 @@ func (pkgGen *HttpPackageGenerator) genSingleCustomizedFile(tplInfo *Template, f } logs.Infof("append content for file '%s', because the update behavior is 'Append' and appendKey is 'method'", filePath) pkgGen.files = append(pkgGen.files, File{filePath, buf.String(), false, ""}) - } else { // 'append location', append new content after 'append location' + } else { // 'append location', append new content before or after 'append location' part := bytes.Split(fileContent, []byte(tplInfo.UpdateBehavior.AppendLocation)) if len(part) == 0 { return fmt.Errorf("can not find append location '%s' for file '%s'\n", tplInfo.UpdateBehavior.AppendLocation, filePath) @@ -555,7 +559,11 @@ func (pkgGen *HttpPackageGenerator) genSingleCustomizedFile(tplInfo *Template, f return fmt.Errorf("do not support multiple append location '%s' for file '%s'\n", tplInfo.UpdateBehavior.AppendLocation, filePath) } buf := bytes.NewBuffer(nil) - err = writeBytes(buf, part[0], []byte(tplInfo.UpdateBehavior.AppendLocation), appendContent, part[1]) + if tplInfo.UpdateBehavior.AppendDirection == Before { + err = writeBytes(buf, part[0], appendContent, []byte(tplInfo.UpdateBehavior.AppendLocation), part[1]) + } else { + err = writeBytes(buf, part[0], []byte(tplInfo.UpdateBehavior.AppendLocation), appendContent, part[1]) + } if err != nil { return fmt.Errorf("write file(%s) failed, err: %v", tplInfo.Path, err) } @@ -608,7 +616,7 @@ func (pkgGen *HttpPackageGenerator) genSingleCustomizedFile(tplInfo *Template, f } logs.Infof("append content for file '%s', because the update behavior is 'Append' and appendKey is 'service'", filePath) pkgGen.files = append(pkgGen.files, File{filePath, buf.String(), false, ""}) - } else { // 'append location', append new content after 'append location' + } else { // 'append location', append new content before or after 'append location' part := bytes.Split(fileContent, []byte(tplInfo.UpdateBehavior.AppendLocation)) if len(part) == 0 { return fmt.Errorf("can not find append location '%s' for file '%s'\n", tplInfo.UpdateBehavior.AppendLocation, filePath) @@ -617,7 +625,11 @@ func (pkgGen *HttpPackageGenerator) genSingleCustomizedFile(tplInfo *Template, f return fmt.Errorf("do not support multiple append location '%s' for file '%s'\n", tplInfo.UpdateBehavior.AppendLocation, filePath) } buf := bytes.NewBuffer(nil) - err = writeBytes(buf, part[0], []byte(tplInfo.UpdateBehavior.AppendLocation), appendContent, part[1]) + if tplInfo.UpdateBehavior.AppendDirection == Before { + err = writeBytes(buf, part[0], appendContent, []byte(tplInfo.UpdateBehavior.AppendLocation), part[1]) + } else { + err = writeBytes(buf, part[0], []byte(tplInfo.UpdateBehavior.AppendLocation), appendContent, part[1]) + } if err != nil { return fmt.Errorf("write file(%s) failed, err: %v", tplInfo.Path, err) } diff --git a/cmd/hz/generator/template.go b/cmd/hz/generator/template.go index b339fbbd2..ee520b7b7 100644 --- a/cmd/hz/generator/template.go +++ b/cmd/hz/generator/template.go @@ -40,6 +40,8 @@ const ( Skip = "skip" Cover = "cover" Append = "append" + Before = "before" + After = "after" ) type Template struct { @@ -56,11 +58,12 @@ type Template struct { type UpdateBehavior struct { Type string `yaml:"type"` // Update behavior type: skip/cover/append // the following variables are used for append update - AppendKey string `yaml:"append_key"` // Append content based in key; for example: 'method'/'service' - InsertKey string `yaml:"insert_key"` // Insert content by "insert_key" - AppendTpl string `yaml:"append_content_tpl"` // Append content if UpdateBehavior is "append" - ImportTpl []string `yaml:"import_tpl"` // Import insert template - AppendLocation string `yaml:"append_location"` // AppendLocation specifies the location of append, the default is the end of the file + AppendKey string `yaml:"append_key"` // Append content based in key; for example: 'method'/'service' + InsertKey string `yaml:"insert_key"` // Insert content by "insert_key" + AppendTpl string `yaml:"append_content_tpl"` // Append content if UpdateBehavior is "append" + ImportTpl []string `yaml:"import_tpl"` // Import insert template + AppendLocation string `yaml:"append_location"` // AppendLocation specifies the location of append, the default is the end of the file + AppendDirection string `yaml:"append_direction"` // AppendDirection specifies the direction of append, the default is "after", and the other option is "before" } // TemplateGenerator contains information about the output template