protgen/main.go
2025-05-19 21:04:54 +02:00

79 lines
1.6 KiB
Go

package main
import (
"encoding/json"
"flag"
"fmt"
"log"
"os"
"alox.protogen/proto"
"github.com/knadh/koanf"
"github.com/knadh/koanf/parsers/yaml"
"github.com/knadh/koanf/providers/file"
)
var appConfig = koanf.New(".")
func main() {
confPath := flag.String("c", "testdata/conf.yaml", "Config File")
outFileName := flag.String("o", "", "Name of the Output File")
inputFile := flag.String("i", "testdata/prot1.json", "Name of the Input File")
flag.Parse()
if err := appConfig.Load(file.Provider(*confPath), yaml.Parser()); err != nil {
log.Printf("error loading config: %v", err)
} else {
log.Printf("CONF: %v", appConfig)
}
file, err := os.ReadFile(*inputFile)
if err != nil {
log.Printf("ERROR: %v", err)
return
}
var cfg proto.Proto
json.Unmarshal(file, &cfg)
// if outFileName is set use it, if is not set set it to default value
if *outFileName != "" {
cfg.FileName = *outFileName
} else {
*outFileName = "proto"
}
// If filename not set in json take it from cli or default
if cfg.FileName != "" {
cfg.FileName = *outFileName
}
proto.InitTemplates()
header, err := proto.CreateCStyleHeader(&cfg)
if err != nil {
log.Printf("ERROR: %v", err)
}
err = os.MkdirAll("build", 0755)
if err != nil {
log.Printf("ERROR: %v", err)
return
}
err = os.WriteFile(fmt.Sprintf("build/%s.h", *outFileName), []byte(header), 0666)
if err != nil {
log.Printf("ERROR: %v", err)
return
}
source, err := proto.CreateCStyleSource(&cfg)
if err != nil {
log.Printf("ERROR: %v", err)
}
err = os.WriteFile(fmt.Sprintf("build/%s.c", *outFileName), []byte(source), 0666)
if err != nil {
log.Printf("ERROR: %v", err)
return
}
}