-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmain.go
72 lines (61 loc) · 1.94 KB
/
main.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package main
import (
"fmt"
"log"
"os"
"strings"
"github.com/bitrise-io/go-utils/fileutil"
)
func main() {
var (
inputFile = os.Getenv("file")
inputOldValue = os.Getenv("old_value")
inputNewValue = os.Getenv("new_value")
inputIsShowFileContent = os.Getenv("show_file") == "true"
inputIsFailIfOldValueNotFound = os.Getenv("notfound_exit") == "true"
)
if inputFile == "" {
log.Fatal("No file input specified")
}
if inputOldValue == "" {
log.Fatal("No old_value input specified")
}
if inputNewValue == "" {
log.Fatal("No new_value input specified")
}
origContent, err := fileutil.ReadStringFromFile(inputFile)
if err != nil {
log.Fatalf("Failed to read from specified file, error: %s", err)
}
if inputIsShowFileContent {
fmt.Println()
fmt.Println("------------------------------------------")
fmt.Println("-------------OLD FILE--------------------")
fmt.Println("------------------------------------------")
fmt.Print(origContent)
fmt.Println()
fmt.Println("------------------------------------------")
}
if inputIsFailIfOldValueNotFound {
if !strings.Contains(origContent, inputOldValue) {
log.Fatalf("Provided old value (%s) was not found in the file (%s)", inputOldValue, inputFile)
}
}
// replace
fmt.Println(" (i) Replacing...")
replacedContent := strings.Replace(origContent, inputOldValue, inputNewValue, -1)
if inputIsShowFileContent {
fmt.Println()
fmt.Println("------------------------------------------")
fmt.Println("-------------NEW FILE--------------------")
fmt.Println("------------------------------------------")
fmt.Print(replacedContent)
fmt.Println()
fmt.Println("------------------------------------------")
}
// write back to file
if err := fileutil.WriteStringToFile(inputFile, replacedContent); err != nil {
log.Printf("Failed to write replaced content back to file, error: %s", err)
}
fmt.Println(" (i) Done")
}