func renderCommon(buf *bytes.Buffer, pkgName, importsString string) error {
	pkgName = removeWhiteSpaces(pkgName)
	if pkgName == "" {
		return errors.New("pkg is required")
	}
	importTemplate := `{{if .Imports}}
import (
{{range $imp := .Imports}}
	"{{$imp}}"
{{end}}
)
{{end}}`
	var imports []string = nil
	importsString = removeWhiteSpaces(importsString)
	if importsString != "" {
		imports = strings.Split(importsString, ";")
	}
	return renderTemplates([]templateSpec{
		{name: "pkg", template: "package {{index .PackageName 0}}\n"},
		{name: "imports", template: importTemplate},
		{name: "common", template: templates.CommonTemplate}},
		map[string][]string{"PackageName": {pkgName}, "Imports": imports}, buf)
}
////////////
/ Common ///
////////////
package main
import (
	"bytes"
	"flag"
	"fmt"
	"go/format"
	"io"
	"os"
	"regexp"
	"strings"
	"text/tabwriter"
	"text/template"
	"github.com/pkg/errors"
	"github.com/tobgu/peds/internal/templates"
)
func usage(fs *flag.FlagSet) func() {
	return func() {
		os.Stderr.WriteString("Generate statically type safe code for persistent data structures.\n\n")
		os.Stderr.WriteString("USAGE\n")
		os.Stderr.WriteString("peds\n\n")
		os.Stderr.WriteString("FLAGS        EXAMPLE\n")
		w := tabwriter.NewWriter(os.Stderr, 0, 2, 2, ' ', 0)
		fs.VisitAll(func(f *flag.Flag) {
			fmt.Fprintf(w, "\t-%s %s\t%s\n", f.Name, f.DefValue, f.Usage)
		})
		w.Flush()
		os.Stderr.WriteString("\n")
	}
}
func logAndExit(err error, flagSet *flag.FlagSet) {
	fmt.Fprint(os.Stderr, "Error: ", err, "\n\n")
	flagSet.Usage()
	os.Exit(1)
}
func main() {
	flagSet := flag.NewFlagSet("server", flag.ExitOnError)
	var (
		vectors = flagSet.String("vectors", "", "Vec1<int>")
		maps    = flagSet.String("maps", "", "Map1<int,string>;Map2<float,int>")
		sets    = flagSet.String("sets", "", "Set1<int>")
		file    = flagSet.String("file", "", "path/to/file.go")
		imports = flagSet.String("imports", "", "import1;import2")
		pkg     = flagSet.String("pkg", "", "package_name")
	)
	flagSet.Usage = usage(flagSet)
	if err := flagSet.Parse(os.Args[1:]); err != nil {
		logAndExit(err, flagSet)
	}
	buf := &bytes.Buffer{}
	if err := renderCommon(buf, *pkg, *imports); err != nil {
		logAndExit(err, flagSet)
	}
	if err := renderVectors(buf, *vectors); err != nil {
		logAndExit(err, flagSet)
	}
	if err := renderMaps(buf, *maps); err != nil {
		logAndExit(err, flagSet)
	}
	if err := renderSet(buf, *sets); err != nil {
		logAndExit(err, flagSet)
	}
	if err := writeFile(buf, *file); err != nil {
		logAndExit(err, flagSet)
	}
}
func removeWhiteSpaces(s string) string {
	return strings.Join(strings.Fields(s), "")
}
type templateSpec struct {
	name     string
	template string
}
func renderTemplates(specs []templateSpec, templateData interface{}, dst io.Writer) error {
	for _, s := range specs {
		t := template.New(s.name)
		t, err := t.Parse(s.template)
		if err != nil {
			return err
		}
		err = t.Execute(dst, templateData)
		if err != nil {
			return err
		}
	}
	return nil
}