Browse Source

works

master
arnaucode 7 years ago
parent
commit
add587888c
4 changed files with 109 additions and 0 deletions
  1. +1
    -0
      .gitignore
  2. +14
    -0
      README.md
  3. +88
    -0
      main.go
  4. +6
    -0
      replacerConfigDEMO.txt

+ 1
- 0
.gitignore

@ -0,0 +1 @@
replacerConfig.txt

+ 14
- 0
README.md

@ -0,0 +1,14 @@
# goStringsReplacer
Replaces the strings configured on the replacerConfig.txt, in all the files in the directory and subdirectories configured
replacerConfig.txt example:
```
wordOriginal1 wordToReplace1
wordOriginal2 wordToReplace2
potatoes carrot
green blue
Hola Hello
zumo juice
```

+ 88
- 0
main.go

@ -0,0 +1,88 @@
package main
import (
"bufio"
"fmt"
"io/ioutil"
"os"
"strings"
)
const rawFolderPath = "./originalFiles"
const newFolderPath = "./newFiles"
func check(err error) {
if err != nil {
fmt.Println(err)
}
}
func replaceFromTable(table map[string]string, originalContent string) string {
var newContent string
newContent = originalContent
for i := 0; i < len(table); i++ {
//first, get the map keys
var keys []string
for key, _ := range table {
keys = append(keys, key)
}
//now, replace the keys with the values
for i := 0; i < len(keys); i++ {
newContent = strings.Replace(newContent, keys[i], table[keys[i]], -1)
}
}
return newContent
}
func readConfigTable(path string) map[string]string {
table := make(map[string]string)
f, err := os.Open(path)
check(err)
scanner := bufio.NewScanner(f)
for scanner.Scan() {
currentLine := scanner.Text()
if currentLine != "" {
val1 := strings.Split(currentLine, " ")[0]
val2 := strings.Split(currentLine, " ")[1]
table[val1] = val2
}
}
return table
}
func readFile(folderPath string, filename string) string {
dat, err := ioutil.ReadFile(folderPath + "/" + filename)
check(err)
return string(dat)
}
func writeFile(path string, newContent string) {
err := ioutil.WriteFile(path, []byte(newContent), 0644)
check(err)
}
func parseDir(folderPath string, newDir string, table map[string]string) {
files, _ := ioutil.ReadDir(folderPath)
for _, f := range files {
fileNameSplitted := strings.Split(f.Name(), ".")
if len(fileNameSplitted) == 1 {
newDir := newDir + "/" + f.Name()
oldDir := rawFolderPath + "/" + f.Name()
if _, err := os.Stat(newDir); os.IsNotExist(err) {
_ = os.Mkdir(newDir, 0700)
}
parseDir(oldDir, newDir, table)
} else {
fileContent := readFile(folderPath, f.Name())
newContent := replaceFromTable(table, fileContent)
writeFile(newDir+"/"+f.Name(), newContent)
fmt.Println(newDir + "/" + f.Name())
}
}
}
func main() {
table := readConfigTable("replacerConfig.txt")
fmt.Println(table)
parseDir(rawFolderPath, newFolderPath, table)
}

+ 6
- 0
replacerConfigDEMO.txt

@ -0,0 +1,6 @@
wordOriginal1 wordToReplace1
wordOriginal2 wordToReplace2
potatoes carrot
green blue
Hola Hello
zumo juice

Loading…
Cancel
Save