Generate Seq
This commit is contained in:
@ -13,7 +13,10 @@ import (
|
||||
//go:embed bind.go.tmpl
|
||||
var bind string
|
||||
|
||||
var tmpl *template.Template
|
||||
//go:embed seq.go.tmpl
|
||||
var seq string
|
||||
|
||||
var bindTmpl, seqTmpl *template.Template
|
||||
|
||||
func main() {
|
||||
err := run()
|
||||
@ -24,13 +27,17 @@ func main() {
|
||||
}
|
||||
|
||||
func run() error {
|
||||
outputPath := flag.String("output", "", "output file path")
|
||||
bindPath := flag.String("bindpath", "", "bind file path")
|
||||
seqPath := flag.String("seqpath", "", "seq file path")
|
||||
maxBindLen := flag.Int("max", 0, "max bind length")
|
||||
pkg := flag.String("pkg", "", "output package")
|
||||
flag.Parse()
|
||||
|
||||
if *outputPath == "" {
|
||||
return errors.New("output required")
|
||||
if *bindPath == "" {
|
||||
return errors.New("bindpath required")
|
||||
}
|
||||
if *seqPath == "" {
|
||||
return errors.New("seqpath required")
|
||||
}
|
||||
if *maxBindLen == 0 {
|
||||
return errors.New("maxbind required")
|
||||
@ -39,23 +46,31 @@ func run() error {
|
||||
return errors.New("pkg required")
|
||||
}
|
||||
|
||||
tmpl = template.New("")
|
||||
_, err := tmpl.New("bind").Parse(bind)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to parse bind: %w", err)
|
||||
}
|
||||
bindTmpl = template.Must(template.New("bind").Parse(bind))
|
||||
seqTmpl = template.Must(template.New("seq").Parse(seq))
|
||||
|
||||
f, err := os.OpenFile(*outputPath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0777)
|
||||
fbind, err := os.OpenFile(*bindPath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0777)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer f.Close()
|
||||
defer fbind.Close()
|
||||
|
||||
data := File{
|
||||
Package: *pkg,
|
||||
Counter: Counter(*maxBindLen),
|
||||
}
|
||||
return tmpl.ExecuteTemplate(f, "bind", data)
|
||||
err = bindTmpl.Execute(fbind, data)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
fseq, err := os.OpenFile(*seqPath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0777)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer fseq.Close()
|
||||
|
||||
return seqTmpl.Execute(fseq, data)
|
||||
}
|
||||
|
||||
type File struct {
|
||||
|
Reference in New Issue
Block a user