-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
88 lines (73 loc) · 1.79 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package main
import (
"bufio"
"flag"
"fmt"
"os"
"regexp"
"strings"
)
var startToken, endToken string
var version, commit, date string
func main() {
fileInput := flag.String("f", "", "file input")
showHelp := flag.Bool("h", false, "show help")
showVersion := flag.Bool("v", false, "version")
flag.StringVar(&startToken, "startToken", "#{", "start token")
flag.StringVar(&endToken, "endToken", "}#", "end token")
flag.Parse()
if *showVersion {
fmt.Println("Token Replacer")
fmt.Printf("Version: %v\n", version)
fmt.Printf("Commit: %v\n", commit)
fmt.Printf("Date: %v\n", date)
os.Exit(0)
}
if *showHelp {
fmt.Println("Token replacer syntax:")
flag.PrintDefaults()
os.Exit(0)
}
// If the file is being piped using "-f -" then we read from Pipe
// Else we just take the path of the file being defined
var output string
var err error
if *fileInput == "-" || *fileInput == "" {
output, err = ReadFromPipe()
} else {
output, err = ReadFromFile(*fileInput)
}
if err != nil {
panic(err)
}
// Output translated string
fmt.Println(output)
}
func ReadFromFile(s string) (o string, err error) {
data, err := os.ReadFile(s)
if err != nil {
return
}
o = RegexReplace(strings.TrimRight(string(data), "\n"))
return
}
func ReadFromPipe() (o string, err error) {
reader := bufio.NewScanner(os.Stdin)
for reader.Scan() {
o += fmt.Sprintln(RegexReplace(reader.Text()))
}
if err = reader.Err(); err != nil {
return
}
o = strings.TrimSuffix(o, "\n")
return
}
func ReplaceWithEnv(s string) string {
s = strings.ReplaceAll(s, startToken, "")
s = strings.ReplaceAll(s, endToken, "")
return os.Getenv(s)
}
func RegexReplace(s string) string {
regex := regexp.MustCompile(fmt.Sprintf("%s([^}]+)%s", startToken, endToken))
return regex.ReplaceAllStringFunc(s, ReplaceWithEnv)
}