Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(hz): add append_direction into update_behavior #1071

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 18 additions & 6 deletions cmd/hz/generator/custom_files.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
}
Expand Down Expand Up @@ -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)
Expand All @@ -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)
}
Expand Down Expand Up @@ -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)
Expand All @@ -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)
}
Expand Down
13 changes: 8 additions & 5 deletions cmd/hz/generator/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ const (
Skip = "skip"
Cover = "cover"
Append = "append"
Before = "before"
After = "after"
)

type Template struct {
Expand All @@ -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
Expand Down
Loading