Skip to content

Commit

Permalink
feat: [CDS-85561]: processing string replacement input from cli (#90)
Browse files Browse the repository at this point in the history
  • Loading branch information
RajBaviskar authored Jan 8, 2024
1 parent 141d8fc commit 8a153d0
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 4 deletions.
23 changes: 23 additions & 0 deletions docs/docs/advanced/string-replace.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
---
sidebar_position: 2
---

# Provide custom string replacement

You can provide custom string replacement information using the `--replace` flag. This flag takes a YAML file as input.

The YAML file should have the following structure:

```yaml
- old: "this is old"
new: "this is new string"
- old: "another old string"
new: "another new string"
```
The usage of the flag is as follows:
```shell
harness-upgrade --load file.yaml --replace replace.yaml app --all
```

51 changes: 47 additions & 4 deletions helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ import (
"encoding/json"
"errors"
"fmt"
"os"
"regexp"
"strings"
"unicode"

"github.com/AlecAivazis/survey/v2"
"github.com/iancoleman/strcase"
log "github.com/sirupsen/logrus"
Expand All @@ -12,10 +17,6 @@ import (
"golang.org/x/text/transform"
"golang.org/x/text/unicode/norm"
"gopkg.in/yaml.v3"
"os"
"regexp"
"strings"
"unicode"
)

const (
Expand Down Expand Up @@ -468,6 +469,48 @@ func LoadOverridesFromFile(filePath string) map[string]EntityOverrideInput {
return overrides
}

type ReplaceSection struct {
Old string `yaml:"old"`
New string `yaml:"new"`
}

func LoadCustomeStringsFromFile(filePath string) map[string]string {
filePath = strings.TrimSpace(filePath)
if len(filePath) == 0 {
return nil
}
// Read the entire file
content, err := os.ReadFile(filePath)
if err != nil {
fmt.Sprintf("error reading file: %v", err)
}

// Unmarshal YAML content into a slice of maps
var replaceSections []map[string]string
err = yaml.Unmarshal(content, &replaceSections)
if err != nil {
fmt.Sprintf("error unmarshalling YAML: %v", err)
}

// Create a map to store the accumulated sections
mergedSections := make(map[string]string)

// Process each section
for _, replaceSection := range replaceSections {
oldValue, oldExists := replaceSection["old"]
newValue, newExists := replaceSection["new"]

if oldExists && newExists {
delete(replaceSection, "old")
delete(replaceSection, "new")
mergedSections[oldValue] = newValue
} else {
fmt.Sprintf("'old' or 'new' keys not found in a section")
}
}
return mergedSections
}

func GetEndpointFromType(entityType string) string {
if entityType == UserGroups {
return "usergroups"
Expand Down
7 changes: 7 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ var migrationReq = struct {
DryRun bool `survey:"dryRun"`
FileExtensions string `survey:"fileExtensions"`
CustomExpressionsFile string `survey:"customExpressionsFile"`
CustomStringsFile string `survey:"customStringsFile"`
OverrideFile string `survey:"overrideFile"`
ExportFolderPath string `survey:"export"`
CsvFile string `survey:"csv"`
Expand All @@ -63,6 +64,7 @@ var migrationReq = struct {
func getReqBody(entityType EntityType, filter Filter) RequestBody {
inputs := Inputs{
Overrides: LoadOverridesFromFile(migrationReq.OverrideFile),
Replace: LoadCustomeStringsFromFile(migrationReq.CustomStringsFile),
Expressions: LoadYamlFromFile(migrationReq.CustomExpressionsFile),
Settings: LoadSettingsFromFile(migrationReq.OverrideFile),
Defaults: Defaults{
Expand Down Expand Up @@ -282,6 +284,11 @@ func main() {
Usage: "provide a `FILE` to load custom expressions from",
Destination: &migrationReq.CustomExpressionsFile,
}),
altsrc.NewStringFlag(&cli.StringFlag{
Name: "replace",
Usage: "provide a `FILE` to load strings from",
Destination: &migrationReq.CustomStringsFile,
}),
altsrc.NewStringFlag(&cli.StringFlag{
Name: "override",
Usage: "provide a `FILE` to load overrides",
Expand Down
1 change: 1 addition & 0 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ type Setting struct {
}

type Inputs struct {
Replace map[string]string `json:"replace"`
Overrides map[string]EntityOverrideInput `json:"overrides"`
Expressions map[string]string `json:"expressions"`
Defaults Defaults `json:"defaults"`
Expand Down

0 comments on commit 8a153d0

Please sign in to comment.