/
generate.go
68 lines (53 loc) · 1.34 KB
/
generate.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
package main
import (
"fmt"
"github.com/nwpc-oper/nwpc-data-client/common"
"io/ioutil"
"log"
"os"
"path/filepath"
)
//go:generate go run generate.go
func main() {
fmt.Printf("This a generate tool to embed YAML config files into nwpc-data-client.\n")
configDir := filepath.Join("../../../", "conf")
file, err := os.Create("../config.autogen.go")
if err != nil {
log.Fatal(err)
}
defer file.Close()
type configItem struct {
Name string
Content string
}
var configList []string
_ = filepath.Walk(configDir, func(path string, info os.FileInfo, err error) error {
if info.IsDir() {
return nil
}
if filepath.Ext(path) != common.ConfigFileBasename {
return nil
}
relFilePath, _ := filepath.Rel(configDir, path)
dataTypeFile := filepath.ToSlash(relFilePath)
dataType := dataTypeFile[:len(dataTypeFile)-5]
fmt.Printf("%s\n", dataType)
content, err := ioutil.ReadFile(path)
if err != nil {
return err
}
configList = append(configList, configString(dataType, string(content)))
return nil
})
fmt.Fprint(file, `// this is a auto generated file.
package config
var EmbeddedConfigs = [][2]string{
`)
for _, item := range configList {
fmt.Fprint(file, " ", item, " ,\n")
}
fmt.Fprintf(file, "}\n")
}
func configString(name, content string) string {
return fmt.Sprintf("{`%s`, `%s`}", name, content)
}