package main import ( _ "embed" "errors" "flag" "fmt" "os" "strconv" "text/template" ) //go:embed bind.go.tmpl var bind string var tmpl *template.Template func main() { err := run() if err != nil { fmt.Fprint(os.Stderr, err) os.Exit(1) } } func run() error { outputPath := flag.String("output", "", "output 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 *maxBindLen == 0 { return errors.New("maxbind required") } if *pkg == "" { 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) } f, err := os.OpenFile(*outputPath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0777) if err != nil { return err } defer f.Close() data := File{ Package: *pkg, Counter: Counter(*maxBindLen), } return tmpl.ExecuteTemplate(f, "bind", data) } type File struct { Package string Counter } // func (f File) Funcs() []Func { // counters := f.Max.Count() // funcs := make([]Func, len(counters)) // for i, counter := range counters { // funcs[i] = Func{ // Max: f.Max, // Length: counter, // } // } // return funcs // } type Func struct { Max Counter Length Counter } type Counter int func (c Counter) String() string { if c == 1 { return "" } return strconv.Itoa(int(c)) } func (c Counter) Int() int { return int(c) } func (c Counter) Next() Counter { return c + 1 } func (c Counter) Prev() Counter { return c - 1 } func (c Counter) Count() []Counter { a := make([]Counter, c) for i := range a { a[i] = Counter(i + 1) } return a }