Node specific overlay and templating now functioning with other minor fixups
This commit is contained in:
@@ -2,40 +2,44 @@
|
||||
package util
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"io"
|
||||
"math/rand"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
// "strings"
|
||||
)
|
||||
|
||||
func FileCopyReplace(sourceFile string, destFile string, replace map[string]string) error{
|
||||
|
||||
sourceFD, err := os.Open(sourceFile)
|
||||
if err != nil {
|
||||
return err
|
||||
func RandomString(n int) string {
|
||||
var letter = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
|
||||
|
||||
b := make([]rune, n)
|
||||
for i := range b {
|
||||
b[i] = letter[rand.Intn(len(letter))]
|
||||
}
|
||||
|
||||
destFD, err := os.Create(destFile)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer sourceFD.Close()
|
||||
defer destFD.Close()
|
||||
|
||||
scanner := bufio.NewScanner(sourceFD)
|
||||
w := bufio.NewWriter(destFD)
|
||||
|
||||
for scanner.Scan() {
|
||||
newLine := scanner.Text()
|
||||
for k, v := range replace {
|
||||
replaceString := fmt.Sprintf("@{%s}", strings.ToUpper(k))
|
||||
newLine = strings.Replace(newLine, replaceString, v, -1)
|
||||
|
||||
}
|
||||
w.WriteString(newLine + "\n")
|
||||
|
||||
}
|
||||
|
||||
return w.Flush()
|
||||
return string(b)
|
||||
}
|
||||
|
||||
|
||||
func CopyFile(source string, dest string) error {
|
||||
sourceFD, err := os.Open(source)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
finfo, err := sourceFD.Stat()
|
||||
|
||||
destFD, err := os.OpenFile(dest, os.O_RDWR|os.O_CREATE, finfo.Mode())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = io.Copy(destFD, sourceFD)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
sourceFD.Close()
|
||||
destFD.Close()
|
||||
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user