utils.EmbedFile is a text file embed script for go:generate to embed text files into generated golang source. related to #830pull/833/head
parent
566c3aeb60
commit
7d101692aa
@ -0,0 +1,64 @@ |
||||
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) |
||||
} |
||||
} |
@ -1,30 +1,8 @@ |
||||
package main |
||||
|
||||
import ( |
||||
"io" |
||||
"io/ioutil" |
||||
"os" |
||||
"path" |
||||
"strings" |
||||
import "github.com/harmony-one/harmony/internal/utils" |
||||
|
||||
"github.com/iancoleman/strcase" |
||||
) |
||||
|
||||
// Reads all .ini files in the `.hmy` folder of root path
|
||||
// then encodes them as strings literals in `cmd/client/wallet/generated_ini.go`
|
||||
// Embed the default wallet.ini file into defaultWalletIni string literal constant
|
||||
func main() { |
||||
const root = "../../../" |
||||
hmy := path.Join(root, ".hmy") |
||||
fs, _ := ioutil.ReadDir(hmy) |
||||
out, _ := os.Create("generated_ini.go") |
||||
out.Write([]byte("package main\n\nconst (\n")) |
||||
for _, f := range fs { |
||||
if strings.HasSuffix(f.Name(), ".ini") { |
||||
out.Write([]byte("\t" + strcase.ToLowerCamel("default_"+strings.TrimSuffix(f.Name(), ".ini")+"_ini") + " = `")) |
||||
f, _ := os.Open(path.Join(hmy, f.Name())) |
||||
io.Copy(out, f) |
||||
out.Write([]byte("`\n")) |
||||
} |
||||
} |
||||
out.Write([]byte(")\n")) |
||||
utils.EmbedFile("../../../.hmy/wallet.ini", "defaultWalletIni") |
||||
} |
||||
|
Loading…
Reference in new issue