2021-04-06 19:39:02 +00:00
|
|
|
// Copyright (c) 2021 The Srpmproc Authors
|
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
// of this software and associated documentation files (the "Software"), to deal
|
|
|
|
// in the Software without restriction, including without limitation the rights
|
|
|
|
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
// copies of the Software, and to permit persons to whom the Software is
|
|
|
|
// furnished to do so, subject to the following conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be included in all
|
|
|
|
// copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
// SOFTWARE.
|
|
|
|
|
2021-02-24 07:27:51 +00:00
|
|
|
package directives
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"git.rockylinux.org/release-engineering/public/srpmproc/internal/data"
|
|
|
|
srpmprocpb "git.rockylinux.org/release-engineering/public/srpmproc/pb"
|
|
|
|
"github.com/go-git/go-git/v5"
|
|
|
|
"io/ioutil"
|
|
|
|
"math"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"regexp"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2021-02-26 07:54:03 +00:00
|
|
|
const (
|
|
|
|
sectionChangelog = "%changelog"
|
|
|
|
)
|
|
|
|
|
2021-03-17 14:54:16 +00:00
|
|
|
var (
|
|
|
|
sections = []string{"%description", "%prep", "%build", "%install", "%files", "%changelog"}
|
|
|
|
)
|
|
|
|
|
2021-02-24 07:27:51 +00:00
|
|
|
type sourcePatchOperationInLoopRequest struct {
|
|
|
|
cfg *srpmprocpb.Cfg
|
|
|
|
field string
|
|
|
|
value *string
|
|
|
|
longestField int
|
|
|
|
lastNum *int
|
2021-03-03 21:17:12 +00:00
|
|
|
in *string
|
2021-02-24 07:27:51 +00:00
|
|
|
expectedField string
|
2021-02-24 07:53:31 +00:00
|
|
|
operation srpmprocpb.SpecChange_FileOperation_Type
|
2021-02-24 07:27:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type sourcePatchOperationAfterLoopRequest struct {
|
|
|
|
cfg *srpmprocpb.Cfg
|
|
|
|
inLoopNum int
|
|
|
|
lastNum *int
|
|
|
|
longestField int
|
|
|
|
newLines *[]string
|
2021-03-03 21:17:12 +00:00
|
|
|
in *string
|
2021-02-24 07:27:51 +00:00
|
|
|
expectedField string
|
|
|
|
operation srpmprocpb.SpecChange_FileOperation_Type
|
|
|
|
}
|
|
|
|
|
|
|
|
func sourcePatchOperationInLoop(req *sourcePatchOperationInLoopRequest) error {
|
|
|
|
if strings.HasPrefix(req.field, req.expectedField) {
|
|
|
|
for _, file := range req.cfg.SpecChange.File {
|
2021-02-24 07:53:31 +00:00
|
|
|
if file.Type != req.operation {
|
2021-02-24 07:27:51 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
switch file.Mode.(type) {
|
|
|
|
case *srpmprocpb.SpecChange_FileOperation_Delete:
|
|
|
|
if file.Name == *req.value {
|
|
|
|
*req.value = ""
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2021-02-26 13:06:02 +00:00
|
|
|
if req.field != req.expectedField {
|
|
|
|
sourceNum, err := strconv.Atoi(strings.Split(req.field, req.expectedField)[1])
|
|
|
|
if err != nil {
|
|
|
|
return errors.New(fmt.Sprintf("INVALID_%s_NUM:%s", strings.ToUpper(req.expectedField), req.field))
|
|
|
|
}
|
|
|
|
*req.lastNum = sourceNum
|
2021-02-24 07:27:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func sourcePatchOperationAfterLoop(req *sourcePatchOperationAfterLoopRequest) (bool, error) {
|
2021-03-03 21:17:12 +00:00
|
|
|
if req.inLoopNum == *req.lastNum && *req.in == req.expectedField {
|
2021-02-24 07:27:51 +00:00
|
|
|
for _, file := range req.cfg.SpecChange.File {
|
|
|
|
if file.Type != req.operation {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
switch file.Mode.(type) {
|
|
|
|
case *srpmprocpb.SpecChange_FileOperation_Add:
|
2021-03-17 14:54:16 +00:00
|
|
|
fieldNum := *req.lastNum + 1
|
|
|
|
field := fmt.Sprintf("%s%d", req.expectedField, fieldNum)
|
2021-02-24 07:27:51 +00:00
|
|
|
spaces := calculateSpaces(req.longestField, len(field))
|
|
|
|
*req.newLines = append(*req.newLines, fmt.Sprintf("%s:%s%s", field, spaces, file.Name))
|
2021-03-17 14:54:16 +00:00
|
|
|
|
|
|
|
if req.expectedField == "Patch" && file.AddToPrep {
|
|
|
|
val := fmt.Sprintf("%%patch%d", fieldNum)
|
|
|
|
if file.NPath > 0 {
|
|
|
|
val = fmt.Sprintf("%s -p%d", val, file.NPath)
|
|
|
|
}
|
|
|
|
|
|
|
|
req.cfg.SpecChange.Append = append(req.cfg.SpecChange.Append, &srpmprocpb.SpecChange_AppendOperation{
|
|
|
|
Field: "%prep",
|
|
|
|
Value: val,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-02-24 07:27:51 +00:00
|
|
|
*req.lastNum++
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2021-03-03 21:17:12 +00:00
|
|
|
*req.in = ""
|
2021-02-24 07:27:51 +00:00
|
|
|
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func calculateSpaces(longestField int, fieldLength int) string {
|
|
|
|
return strings.Repeat(" ", longestField+8-fieldLength)
|
|
|
|
}
|
|
|
|
|
|
|
|
func searchAndReplaceLine(line string, sar []*srpmprocpb.SpecChange_SearchAndReplaceOperation) string {
|
|
|
|
for _, searchAndReplace := range sar {
|
|
|
|
switch searchAndReplace.Identifier.(type) {
|
|
|
|
case *srpmprocpb.SpecChange_SearchAndReplaceOperation_Any:
|
|
|
|
line = strings.Replace(line, searchAndReplace.Find, searchAndReplace.Replace, int(searchAndReplace.N))
|
|
|
|
break
|
|
|
|
case *srpmprocpb.SpecChange_SearchAndReplaceOperation_StartsWith:
|
2021-02-26 07:54:03 +00:00
|
|
|
if strings.HasPrefix(strings.TrimSpace(line), searchAndReplace.Find) {
|
2021-02-24 07:27:51 +00:00
|
|
|
line = strings.Replace(line, searchAndReplace.Find, searchAndReplace.Replace, int(searchAndReplace.N))
|
|
|
|
}
|
|
|
|
break
|
|
|
|
case *srpmprocpb.SpecChange_SearchAndReplaceOperation_EndsWith:
|
2021-02-26 07:54:03 +00:00
|
|
|
if strings.HasSuffix(strings.TrimSpace(line), searchAndReplace.Find) {
|
2021-02-24 07:27:51 +00:00
|
|
|
line = strings.Replace(line, searchAndReplace.Find, searchAndReplace.Replace, int(searchAndReplace.N))
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return line
|
|
|
|
}
|
|
|
|
|
2021-03-03 21:17:12 +00:00
|
|
|
func isNextLineSection(lineNum int, lines []string) bool {
|
|
|
|
if len(lines)-1 > lineNum {
|
|
|
|
if strings.HasPrefix(strings.TrimSpace(lines[lineNum+1]), "%") {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func setFASlice(futureAdditions map[int][]string, key int, addition string) {
|
|
|
|
if futureAdditions[key] == nil {
|
|
|
|
futureAdditions[key] = []string{}
|
|
|
|
}
|
|
|
|
futureAdditions[key] = append(futureAdditions[key], addition)
|
|
|
|
}
|
|
|
|
|
2021-03-17 14:54:16 +00:00
|
|
|
func strSliceContains(slice []string, str string) bool {
|
|
|
|
for _, x := range slice {
|
|
|
|
if str == x {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-02-24 07:27:51 +00:00
|
|
|
func specChange(cfg *srpmprocpb.Cfg, pd *data.ProcessData, md *data.ModeData, _ *git.Worktree, pushTree *git.Worktree) error {
|
|
|
|
// no spec change operations present
|
|
|
|
// skip parsing spec
|
|
|
|
if cfg.SpecChange == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
specFiles, err := pushTree.Filesystem.ReadDir("SPECS")
|
|
|
|
if err != nil {
|
|
|
|
return errors.New("COULD_NOT_READ_SPECS_DIR")
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(specFiles) != 1 {
|
|
|
|
return errors.New("ONLY_ONE_SPEC_FILE_IS_SUPPORTED")
|
|
|
|
}
|
|
|
|
|
|
|
|
filePath := filepath.Join("SPECS", specFiles[0].Name())
|
|
|
|
stat, err := pushTree.Filesystem.Stat(filePath)
|
|
|
|
if err != nil {
|
|
|
|
return errors.New("COULD_NOT_STAT_SPEC_FILE")
|
|
|
|
}
|
|
|
|
|
|
|
|
specFile, err := pushTree.Filesystem.OpenFile(filePath, os.O_RDONLY, 0644)
|
|
|
|
if err != nil {
|
|
|
|
return errors.New("COULD_NOT_READ_SPEC_FILE")
|
|
|
|
}
|
|
|
|
|
|
|
|
specBts, err := ioutil.ReadAll(specFile)
|
|
|
|
if err != nil {
|
|
|
|
return errors.New("COULD_NOT_READ_ALL_BYTES")
|
|
|
|
}
|
|
|
|
|
|
|
|
specStr := string(specBts)
|
|
|
|
lines := strings.Split(specStr, "\n")
|
|
|
|
|
|
|
|
var newLines []string
|
2021-03-03 21:17:12 +00:00
|
|
|
futureAdditions := map[int][]string{}
|
|
|
|
newFieldMemory := map[string]map[string]int{}
|
2021-02-24 07:27:51 +00:00
|
|
|
lastSourceNum := 0
|
|
|
|
lastPatchNum := 0
|
2021-02-26 07:54:03 +00:00
|
|
|
inSection := ""
|
2021-03-03 21:17:12 +00:00
|
|
|
inField := ""
|
2021-02-24 07:27:51 +00:00
|
|
|
lastSource := ""
|
|
|
|
lastPatch := ""
|
2021-04-05 22:12:30 +00:00
|
|
|
hasPatch := false
|
2021-02-24 07:27:51 +00:00
|
|
|
|
|
|
|
version := ""
|
2021-02-26 07:54:03 +00:00
|
|
|
importName := strings.Replace(pd.Importer.ImportName(pd, md), md.RpmFile.Name(), "1", 1)
|
|
|
|
importNameSplit := strings.SplitN(importName, "-", 2)
|
2021-02-24 07:27:51 +00:00
|
|
|
if len(importNameSplit) == 2 {
|
|
|
|
versionSplit := strings.SplitN(importNameSplit[1], ".el", 2)
|
|
|
|
if len(versionSplit) == 2 {
|
|
|
|
version = versionSplit[0]
|
2021-02-26 07:54:03 +00:00
|
|
|
} else {
|
|
|
|
versionSplit := strings.SplitN(importNameSplit[1], ".module", 2)
|
|
|
|
if len(versionSplit) == 2 {
|
|
|
|
version = versionSplit[0]
|
|
|
|
}
|
2021-02-24 07:27:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fieldValueRegex := regexp.MustCompile("^[A-Z].+:")
|
|
|
|
|
|
|
|
longestField := 0
|
2021-03-03 21:17:12 +00:00
|
|
|
for lineNum, line := range lines {
|
2021-02-24 07:27:51 +00:00
|
|
|
if fieldValueRegex.MatchString(line) {
|
|
|
|
fieldValue := strings.SplitN(line, ":", 2)
|
|
|
|
field := strings.TrimSpace(fieldValue[0])
|
|
|
|
longestField = int(math.Max(float64(len(field)), float64(longestField)))
|
|
|
|
|
|
|
|
if strings.HasPrefix(field, "Source") {
|
|
|
|
lastSource = field
|
2021-03-03 21:17:12 +00:00
|
|
|
} else if strings.HasPrefix(field, "Patch") {
|
|
|
|
lastPatch = field
|
2021-04-05 22:12:30 +00:00
|
|
|
hasPatch = true
|
2021-03-03 21:17:12 +00:00
|
|
|
} else {
|
|
|
|
for _, nf := range cfg.SpecChange.NewField {
|
|
|
|
if field == nf.Key {
|
|
|
|
if newFieldMemory[field] == nil {
|
|
|
|
newFieldMemory[field] = map[string]int{}
|
|
|
|
}
|
|
|
|
newFieldMemory[field][nf.Value] = lineNum
|
|
|
|
}
|
|
|
|
}
|
2021-02-24 07:27:51 +00:00
|
|
|
}
|
2021-03-03 21:17:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, nf := range cfg.SpecChange.NewField {
|
|
|
|
if newFieldMemory[nf.Key] == nil {
|
|
|
|
newFieldMemory[nf.Key] = map[string]int{}
|
|
|
|
newFieldMemory[nf.Key][nf.Value] = 0
|
|
|
|
}
|
|
|
|
}
|
2021-02-24 07:27:51 +00:00
|
|
|
|
2021-03-03 21:17:12 +00:00
|
|
|
for field, nfm := range newFieldMemory {
|
|
|
|
for value, lineNum := range nfm {
|
|
|
|
if lineNum != 0 {
|
|
|
|
newLine := fmt.Sprintf("%s:%s%s", field, calculateSpaces(longestField, len(field)), value)
|
|
|
|
setFASlice(futureAdditions, lineNum+1, newLine)
|
2021-02-24 07:27:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-26 07:54:03 +00:00
|
|
|
for lineNum, line := range lines {
|
2021-02-24 07:27:51 +00:00
|
|
|
inLoopSourceNum := lastSourceNum
|
|
|
|
inLoopPatchNum := lastPatchNum
|
|
|
|
prefixLine := strings.TrimSpace(line)
|
|
|
|
|
2021-03-03 21:17:12 +00:00
|
|
|
for i, additions := range futureAdditions {
|
2021-02-26 07:54:03 +00:00
|
|
|
if lineNum == i {
|
2021-03-03 21:17:12 +00:00
|
|
|
for _, addition := range additions {
|
|
|
|
newLines = append(newLines, addition)
|
|
|
|
}
|
2021-02-26 07:54:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-24 07:27:51 +00:00
|
|
|
if fieldValueRegex.MatchString(line) {
|
|
|
|
line = searchAndReplaceLine(line, cfg.SpecChange.SearchAndReplace)
|
|
|
|
fieldValue := strings.SplitN(line, ":", 2)
|
|
|
|
field := strings.TrimSpace(fieldValue[0])
|
|
|
|
value := strings.TrimSpace(fieldValue[1])
|
|
|
|
|
|
|
|
if field == lastSource {
|
2021-03-03 21:17:12 +00:00
|
|
|
inField = "Source"
|
2021-02-24 07:27:51 +00:00
|
|
|
} else if field == lastPatch {
|
2021-03-03 21:17:12 +00:00
|
|
|
inField = "Patch"
|
2021-02-24 07:27:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if field == "Version" && version == "" {
|
|
|
|
version = value
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, searchAndReplace := range cfg.SpecChange.SearchAndReplace {
|
|
|
|
switch identifier := searchAndReplace.Identifier.(type) {
|
|
|
|
case *srpmprocpb.SpecChange_SearchAndReplaceOperation_Field:
|
|
|
|
if field == identifier.Field {
|
|
|
|
value = strings.Replace(value, searchAndReplace.Find, searchAndReplace.Replace, int(searchAndReplace.N))
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-25 05:29:46 +00:00
|
|
|
for _, appendOp := range cfg.SpecChange.Append {
|
|
|
|
if field == appendOp.Field {
|
|
|
|
value = value + appendOp.Value
|
2021-02-26 07:54:03 +00:00
|
|
|
|
|
|
|
if field == "Release" {
|
|
|
|
version = version + appendOp.Value
|
|
|
|
}
|
2021-02-25 05:29:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-24 07:27:51 +00:00
|
|
|
spaces := calculateSpaces(longestField, len(field))
|
|
|
|
|
|
|
|
err := sourcePatchOperationInLoop(&sourcePatchOperationInLoopRequest{
|
|
|
|
cfg: cfg,
|
|
|
|
field: field,
|
|
|
|
value: &value,
|
|
|
|
lastNum: &lastSourceNum,
|
|
|
|
longestField: longestField,
|
2021-03-03 21:17:12 +00:00
|
|
|
in: &inField,
|
2021-02-24 07:53:31 +00:00
|
|
|
expectedField: "Source",
|
|
|
|
operation: srpmprocpb.SpecChange_FileOperation_Source,
|
2021-02-24 07:27:51 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = sourcePatchOperationInLoop(&sourcePatchOperationInLoopRequest{
|
|
|
|
cfg: cfg,
|
|
|
|
field: field,
|
|
|
|
value: &value,
|
|
|
|
longestField: longestField,
|
|
|
|
lastNum: &lastPatchNum,
|
2021-03-03 21:17:12 +00:00
|
|
|
in: &inField,
|
2021-02-24 07:27:51 +00:00
|
|
|
expectedField: "Patch",
|
2021-02-24 07:53:31 +00:00
|
|
|
operation: srpmprocpb.SpecChange_FileOperation_Patch,
|
2021-02-24 07:27:51 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if value != "" {
|
|
|
|
newLines = append(newLines, fmt.Sprintf("%s:%s%s", field, spaces, value))
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
executed, err := sourcePatchOperationAfterLoop(&sourcePatchOperationAfterLoopRequest{
|
|
|
|
cfg: cfg,
|
|
|
|
inLoopNum: inLoopSourceNum,
|
|
|
|
lastNum: &lastSourceNum,
|
|
|
|
longestField: longestField,
|
|
|
|
newLines: &newLines,
|
|
|
|
expectedField: "Source",
|
2021-03-03 21:17:12 +00:00
|
|
|
in: &inField,
|
2021-02-24 07:27:51 +00:00
|
|
|
operation: srpmprocpb.SpecChange_FileOperation_Source,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-04-05 22:12:30 +00:00
|
|
|
if executed && !hasPatch {
|
2021-02-24 07:27:51 +00:00
|
|
|
newLines = append(newLines, "")
|
2021-03-03 21:17:12 +00:00
|
|
|
inField = "Patch"
|
2021-02-24 07:27:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
executed, err = sourcePatchOperationAfterLoop(&sourcePatchOperationAfterLoopRequest{
|
|
|
|
cfg: cfg,
|
|
|
|
inLoopNum: inLoopPatchNum,
|
|
|
|
lastNum: &lastPatchNum,
|
|
|
|
longestField: longestField,
|
|
|
|
newLines: &newLines,
|
|
|
|
expectedField: "Patch",
|
2021-03-03 21:17:12 +00:00
|
|
|
in: &inField,
|
2021-02-24 07:27:51 +00:00
|
|
|
operation: srpmprocpb.SpecChange_FileOperation_Patch,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-03-03 21:17:12 +00:00
|
|
|
if executed {
|
|
|
|
var innerNewLines []string
|
|
|
|
for field, nfm := range newFieldMemory {
|
|
|
|
for value, ln := range nfm {
|
|
|
|
newLine := fmt.Sprintf("%s:%s%s", field, calculateSpaces(longestField, len(field)), value)
|
|
|
|
if ln == 0 {
|
|
|
|
if isNextLineSection(lineNum, lines) {
|
|
|
|
innerNewLines = append(innerNewLines, newLine)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(innerNewLines) > 0 {
|
|
|
|
newLines = append(newLines, "")
|
|
|
|
for _, il := range innerNewLines {
|
|
|
|
newLines = append(newLines, il)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-24 07:27:51 +00:00
|
|
|
if executed && !strings.Contains(specStr, "%changelog") {
|
|
|
|
newLines = append(newLines, "")
|
|
|
|
newLines = append(newLines, "%changelog")
|
2021-02-26 07:54:03 +00:00
|
|
|
inSection = sectionChangelog
|
2021-02-24 07:27:51 +00:00
|
|
|
}
|
|
|
|
|
2021-02-26 07:54:03 +00:00
|
|
|
if inSection == sectionChangelog {
|
2021-02-24 07:27:51 +00:00
|
|
|
now := time.Now().Format("Mon Jan 02 2006")
|
|
|
|
for _, changelog := range cfg.SpecChange.Changelog {
|
|
|
|
newLines = append(newLines, fmt.Sprintf("* %s %s <%s> - %s", now, changelog.AuthorName, changelog.AuthorEmail, version))
|
|
|
|
for _, msg := range changelog.Message {
|
|
|
|
newLines = append(newLines, fmt.Sprintf("- %s", msg))
|
|
|
|
}
|
|
|
|
newLines = append(newLines, "")
|
|
|
|
}
|
2021-02-26 07:54:03 +00:00
|
|
|
inSection = ""
|
2021-02-24 07:27:51 +00:00
|
|
|
} else {
|
|
|
|
line = searchAndReplaceLine(line, cfg.SpecChange.SearchAndReplace)
|
|
|
|
}
|
|
|
|
|
2021-02-26 07:54:03 +00:00
|
|
|
if strings.HasPrefix(prefixLine, "%") {
|
|
|
|
inSection = prefixLine
|
|
|
|
|
|
|
|
for _, appendOp := range cfg.SpecChange.Append {
|
|
|
|
if inSection == appendOp.Field {
|
|
|
|
insertedLine := 0
|
|
|
|
for i, x := range lines[lineNum+1:] {
|
2021-03-17 14:54:16 +00:00
|
|
|
if strSliceContains(sections, strings.TrimSpace(x)) {
|
|
|
|
insertedLine = lineNum + i
|
2021-03-03 21:17:12 +00:00
|
|
|
setFASlice(futureAdditions, insertedLine, appendOp.Value)
|
2021-02-26 07:54:03 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if insertedLine == 0 {
|
|
|
|
for i, x := range lines[lineNum+1:] {
|
|
|
|
if strings.TrimSpace(x) == "" {
|
|
|
|
insertedLine = lineNum + i + 2
|
2021-03-03 21:17:12 +00:00
|
|
|
setFASlice(futureAdditions, insertedLine, appendOp.Value)
|
2021-02-26 07:54:03 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-02-24 07:27:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
newLines = append(newLines, line)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
err = pushTree.Filesystem.Remove(filePath)
|
|
|
|
if err != nil {
|
|
|
|
return errors.New(fmt.Sprintf("COULD_NOT_REMOVE_OLD_SPEC_FILE:%s", filePath))
|
|
|
|
}
|
|
|
|
|
|
|
|
f, err := pushTree.Filesystem.OpenFile(filePath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, stat.Mode())
|
|
|
|
if err != nil {
|
|
|
|
return errors.New(fmt.Sprintf("COULD_NOT_OPEN_REPLACEMENT_SPEC_FILE:%s", filePath))
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = f.Write([]byte(strings.Join(newLines, "\n")))
|
|
|
|
if err != nil {
|
|
|
|
return errors.New("COULD_NOT_WRITE_NEW_SPEC_FILE")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|