github.com/shashank-sachan/gqlgen@v0.7.2/codegen/templates/inliner/inliner.go (about)

     1  package main
     2  
     3  import (
     4  	"bytes"
     5  	"go/format"
     6  	"io/ioutil"
     7  	"strconv"
     8  	"strings"
     9  )
    10  
    11  func main() {
    12  	dir := "./"
    13  
    14  	files, err := ioutil.ReadDir(dir)
    15  	if err != nil {
    16  		panic(err)
    17  	}
    18  
    19  	out := bytes.Buffer{}
    20  	out.WriteString("package templates\n\n")
    21  	out.WriteString("var data = map[string]string{\n")
    22  
    23  	for _, f := range files {
    24  		if !strings.HasSuffix(f.Name(), ".gotpl") {
    25  			continue
    26  		}
    27  
    28  		b, err := ioutil.ReadFile(dir + f.Name())
    29  		if err != nil {
    30  			panic(err)
    31  		}
    32  
    33  		out.WriteString(strconv.Quote(f.Name()))
    34  		out.WriteRune(':')
    35  		out.WriteString(strconv.Quote(string(b)))
    36  		out.WriteString(",\n")
    37  	}
    38  
    39  	out.WriteString("}\n")
    40  
    41  	formatted, err2 := format.Source(out.Bytes())
    42  	if err2 != nil {
    43  		panic(err2)
    44  	}
    45  
    46  	ioutil.WriteFile(dir+"data.go", formatted, 0644)
    47  }