forked from sourcegraph/sourcegraph-public-snapshot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstringdata.go
More file actions
51 lines (43 loc) · 1.12 KB
/
stringdata.go
File metadata and controls
51 lines (43 loc) · 1.12 KB
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
// +build ignore
package main
import (
"flag"
"fmt"
"io/ioutil"
"os"
"strings"
)
var (
inputFile = flag.String("i", "", "input file")
outputFile = flag.String("o", "", "output file")
constName = flag.String("name", "stringdata", "name of Go const")
pkgName = flag.String("pkg", "main", "Go package name")
)
func main() {
flag.Parse()
if *inputFile == "" || *outputFile == "" {
flag.Usage()
os.Exit(1)
}
data, err := ioutil.ReadFile(*inputFile)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
output, err := os.Create(*outputFile)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
defer output.Close()
fmt.Fprintln(output, "// Code generated by stringdata. DO NOT EDIT.")
fmt.Fprintln(output)
fmt.Fprintf(output, "package %s\n", *pkgName)
fmt.Fprintln(output)
fmt.Fprintf(output, "// %s is the content of the file %q.\n", *constName, *inputFile)
fmt.Fprintf(output, "const %s = %s", *constName, backtickStringLiteral(string(data)))
fmt.Fprintln(output)
}
func backtickStringLiteral(data string) string {
return "`" + strings.Replace(data, "`", "` + \"`\" + `", -1) + "`"
}