-
Notifications
You must be signed in to change notification settings - Fork 87
/
main.go
44 lines (36 loc) · 977 Bytes
/
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
package main
import (
"fmt"
"io/ioutil"
"os"
"strings"
)
// usage:
// go run cmd/texttosource/main.go packagename constantname inputfile outputfile
func main() {
packageName := os.Args[1]
constantName := os.Args[2]
inputFilepath := os.Args[3]
outputFilepath := os.Args[4]
data, err := ioutil.ReadFile(inputFilepath)
if err != nil {
fmt.Fprintf(os.Stderr, "cannot read file %s: %v\n", inputFilepath, err)
os.Exit(1)
}
fout, err := os.Create(outputFilepath)
if err != nil {
fmt.Fprintf(os.Stderr, "cannot open %s for writing: %v\n", outputFilepath, err)
os.Exit(1)
}
body := "`" + strings.ReplaceAll(string(data), "`", "\\u0060") + "`"
format := `// Code generated from source %s via go generate. DO NOT EDIT.
package %s
const %s = %s
`
_, err = fmt.Fprintf(fout, format, inputFilepath, packageName, constantName, body)
if err != nil {
fmt.Fprintf(os.Stderr, "failed to write to %s: %v\n", outputFilepath, err)
os.Exit(1)
}
fout.Close()
}