Skip to content

Commit

Permalink
Only replace headers in beginning of line
Browse files Browse the repository at this point in the history
fixes #427
  • Loading branch information
andreas-aman committed Mar 13, 2023
1 parent 8d0b00e commit ae90733
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 9 deletions.
16 changes: 13 additions & 3 deletions internal/types/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ limitations under the License.
package types

import (
"bytes"
"fmt"
"os"
"regexp"
"sort"
"strconv"
"strings"
Expand Down Expand Up @@ -89,7 +89,11 @@ func (a *Action) WriteDocumentation(inputTable, outputTable *strings.Builder) er
if hasInputsData {
output = utils.ReplaceBytesInBetween(input, inputStartIndex, inputEndIndex, []byte(inputsStr))
} else {
output = bytes.Replace(input, []byte(internal.InputsHeader), []byte(inputsStr), -1)
re, err := regexp.Compile(fmt.Sprintf("(?m)^%s", internal.InputsHeader))
if err != nil {
return err
}
output = re.ReplaceAll([]byte(output), []byte(inputsStr))
}

hasOutputsData, outputStartIndex, outputEndIndex := utils.HasBytesInBetween(
Expand All @@ -103,7 +107,13 @@ func (a *Action) WriteDocumentation(inputTable, outputTable *strings.Builder) er
if hasOutputsData {
output = utils.ReplaceBytesInBetween(output, outputStartIndex, outputEndIndex, []byte(outputsStr))
} else {
output = bytes.Replace(output, []byte(internal.OutputsHeader), []byte(outputsStr), -1)
re, _ := regexp.Compile(fmt.Sprintf("(?m)^%s", internal.OutputsHeader))
if err != nil {
return err
}
output = re.ReplaceAll([]byte(output), []byte(outputsStr))

//output = bytes.Replace(output, []byte(internal.OutputsHeader), []byte(outputsStr), -1)
}

if len(output) > 0 {
Expand Down
24 changes: 18 additions & 6 deletions internal/types/reusable.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ limitations under the License.
package types

import (
"bytes"
"fmt"
"os"
"regexp"
"sort"
"strconv"
"strings"
Expand Down Expand Up @@ -95,13 +95,17 @@ func (r *Reusable) WriteDocumentation(inputTable, outputTable, secretsTable *str
[]byte(internal.InputsHeader),
[]byte(internal.InputAutoDocEnd),
)

inputsStr := strings.TrimSpace(fmt.Sprintf("%s\n\n%v", internal.InputsHeader, inputTable.String()))

if hasInputsData {
output = utils.ReplaceBytesInBetween(input, inputStartIndex, inputEndIndex, []byte(inputsStr))
} else {
output = bytes.Replace(input, []byte(internal.InputsHeader), []byte(inputsStr), -1)
re, _ := regexp.Compile(fmt.Sprintf("(?m)^%s", internal.InputsHeader))
if err != nil {
return err
}
output = re.ReplaceAll([]byte(output), []byte(inputsStr))
}

hasOutputsData, outputStartIndex, outputEndIndex := utils.HasBytesInBetween(
Expand All @@ -115,21 +119,29 @@ func (r *Reusable) WriteDocumentation(inputTable, outputTable, secretsTable *str
if hasOutputsData {
output = utils.ReplaceBytesInBetween(output, outputStartIndex, outputEndIndex, []byte(outputsStr))
} else {
output = bytes.Replace(output, []byte(internal.OutputsHeader), []byte(outputsStr), -1)
re, _ := regexp.Compile(fmt.Sprintf("(?m)^%s", internal.OutputsHeader))
if err != nil {
return err
}
output = re.ReplaceAll([]byte(output), []byte(outputsStr))
}

hasSecretsData, secretsStartIndex, secretsEndIndex := utils.HasBytesInBetween(
output,
[]byte(internal.SecretsHeader),
[]byte(internal.SecretsAutoDocEnd),
)

secretsStr := strings.TrimSpace(fmt.Sprintf("%s\n\n%v", internal.SecretsHeader, secretsTable.String()))

if hasSecretsData {
output = utils.ReplaceBytesInBetween(output, secretsStartIndex, secretsEndIndex, []byte(secretsStr))
} else {
output = bytes.Replace(output, []byte(internal.SecretsHeader), []byte(secretsStr), -1)
re, _ := regexp.Compile(fmt.Sprintf("(?m)^%s", internal.SecretsHeader))
if err != nil {
return err
}
output = re.ReplaceAll([]byte(output), []byte(secretsStr))
}

if len(output) > 0 {
Expand Down

0 comments on commit ae90733

Please sign in to comment.