2021-01-05 12:11:06 +01:00
|
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"io/ioutil"
|
|
|
|
|
"os"
|
|
|
|
|
"sort"
|
|
|
|
|
|
|
|
|
|
"gopkg.in/yaml.v2"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func loadBangs() (bangs map[string]string) {
|
2021-01-05 12:14:00 +01:00
|
|
|
|
data, err := ioutil.ReadFile("bangs.yml")
|
2021-01-05 12:11:06 +01:00
|
|
|
|
if err != nil {
|
|
|
|
|
panic(fmt.Sprintf("Failed to load bangs.yml: %v", err))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err = yaml.Unmarshal(data, &bangs)
|
|
|
|
|
if err != nil {
|
|
|
|
|
panic(fmt.Sprintf("Failed to decode bangs: %v", err))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
|
bangs := loadBangs()
|
|
|
|
|
|
|
|
|
|
// generating sorted keys to have stable generated code
|
|
|
|
|
// this is not optimal, performance-wise but it runs only once at build
|
|
|
|
|
// time, so it doesn’t matter that much)
|
|
|
|
|
bangNames := []string{}
|
|
|
|
|
for bang := range bangs {
|
|
|
|
|
bangNames = append(bangNames, bang)
|
|
|
|
|
}
|
|
|
|
|
sort.Strings(bangNames)
|
|
|
|
|
|
|
|
|
|
out, err := os.Create("bangs.go")
|
|
|
|
|
if err != nil {
|
|
|
|
|
panic(fmt.Sprintf("Failed to create bangs.go: %v", err))
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-05 12:14:00 +01:00
|
|
|
|
out.Write([]byte("// Code generated from ../bangs.yml DO NOT EDIT.\n"))
|
2021-01-05 12:11:06 +01:00
|
|
|
|
out.Write([]byte("package main \n\nfunc getBang(name string) string {\n\tswitch name {\n"))
|
|
|
|
|
|
|
|
|
|
for _, bangName := range bangNames {
|
|
|
|
|
out.Write([]byte("\t\tcase `" + bangName + "`: return `" + bangs[bangName] + "`\n"))
|
|
|
|
|
}
|
|
|
|
|
out.Write([]byte("\t\tdefault: return \"\"\n"))
|
|
|
|
|
out.Write([]byte("\t}\n}\n"))
|
|
|
|
|
}
|