You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
65 lines
1.3 KiB
65 lines
1.3 KiB
6 years ago
|
package utils
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"io"
|
||
|
"os"
|
||
|
"path/filepath"
|
||
|
|
||
|
"github.com/iancoleman/strcase"
|
||
|
)
|
||
|
|
||
|
// EmbedFile Text file embed script for go:generate.
|
||
|
// This script embeds a text file located at filePath into a string constant
|
||
|
// named as constName defined in a golang source file located at the current
|
||
|
// go:generate path.
|
||
|
func EmbedFile(filePath string, constName string) {
|
||
|
// validate inputs
|
||
|
if _, err := os.Stat(filePath); os.IsNotExist(err) {
|
||
|
panic(fmt.Sprintf("File %s does not exist", filePath))
|
||
|
}
|
||
|
|
||
|
if strcase.ToLowerCamel(constName) != constName {
|
||
|
panic(fmt.Sprintf("constName %s is not in lower camel-case", constName))
|
||
|
}
|
||
|
|
||
|
// generate go file
|
||
|
var err error
|
||
|
fileName := filepath.Base(filePath)
|
||
|
generatedFileName := "generated_" + strcase.ToSnake(fileName) + ".go"
|
||
|
out, err := os.Create(generatedFileName)
|
||
|
if err != nil {
|
||
|
panic(err)
|
||
|
}
|
||
|
|
||
|
_, err = out.Write([]byte(fmt.Sprintf("package %s\n\nconst (\n", os.Getenv("GOPACKAGE"))))
|
||
|
if err != nil {
|
||
|
panic(err)
|
||
|
}
|
||
|
|
||
|
_, err = out.Write([]byte("\t" + constName + " = `"))
|
||
|
if err != nil {
|
||
|
panic(err)
|
||
|
}
|
||
|
|
||
|
f, err := os.Open(filePath)
|
||
|
if err != nil {
|
||
|
panic(err)
|
||
|
}
|
||
|
|
||
|
_, err = io.Copy(out, f)
|
||
|
if err != nil {
|
||
|
panic(err)
|
||
|
}
|
||
|
|
||
|
_, err = out.Write([]byte("`\n"))
|
||
|
if err != nil {
|
||
|
panic(err)
|
||
|
}
|
||
|
|
||
|
_, err = out.Write([]byte(")\n"))
|
||
|
if err != nil {
|
||
|
panic(err)
|
||
|
}
|
||
|
}
|