-
Notifications
You must be signed in to change notification settings - Fork 0
/
grt.go
44 lines (38 loc) · 1.19 KB
/
grt.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package grt
import (
"io/ioutil"
"log"
"os"
"path/filepath"
"strings"
"github.com/bitfield/script"
)
// ReplaceTokens replaces tokens in input file and outputs to new file
func ReplaceTokens(inputFilePath, outputFilePath, repoHost, repoOrg, repoName, newRepo string) {
parts := strings.Split(newRepo, "/")
newRepoHost := parts[0]
newRepoOrg := parts[1]
newRepoName := parts[2]
// log.Printf("repoHost: %s, repoOrg: %s, repoName: %s, newRepoHost: %s, newRepoOrg: %s, newRepoName: %s\n", repoHost, repoOrg, repoName, newRepoHost, newRepoOrg, newRepoName)
inplace := false
if outputFilePath == "" {
inplace = true
tempFileH, err := ioutil.TempFile(filepath.Dir(inputFilePath), "")
if err != nil {
log.Fatal(err.Error())
}
outputFilePath = tempFileH.Name()
}
_, err := script.File(inputFilePath).Replace(repoHost, newRepoHost).Replace(repoOrg, newRepoOrg).Replace(repoName, newRepoName).WriteFile(outputFilePath)
if err != nil {
log.Fatal(err.Error())
}
if inplace {
script.File(outputFilePath).Stdout()
e := os.Rename(outputFilePath, inputFilePath)
if e != nil {
log.Fatal(e)
}
}
log.Printf("Replaced tokens in %s and created %s\n", inputFilePath, outputFilePath)
}