Skip to content

Latest commit

 

History

History
192 lines (152 loc) · 5.56 KB

File metadata and controls

192 lines (152 loc) · 5.56 KB

Experimental library to convert arbitrary files to gocode

DO NOT USE!

If you still want to use it, you could try:

go get github.com/pscn/file2go

And add something like this to your go file:

//go:generate file2go -v -t -o template/files.go template/*.tmpl

Finally call:

go generate

This will generate a file template/files.go and template/files_test.go that contains all *.tmpl in the template directory. To get the content use template.Content("template/files.tmpl") or template.Content("template/files_test.tmpl").

The generated file contains all the code required to decode the gzip'd & base64 encoded string.

This is how the generated files looks like.

template/files.go:

// Code generated by "file2go -v -d -t -o template/files.go template/*.tmpl"; DO NOT EDIT.

// Encoded files:
// → template/files.tmpl
// → template/files_test.tmpl

package template

import (
	"bytes"
	"compress/gzip"
	"encoding/base64"
	"fmt"
	"io/ioutil"
	"log"
)

var container map[string]*[]byte

// decode the base64Encoded string and return a *[]byte for the decoded data
func decode(base64Encoded string) *[]byte {
	gzipEncoded, err := base64.StdEncoding.DecodeString(base64Encoded)
	if err != nil {
		log.Panicf("failed to decode data(BASE64): %s", err)
	}

	var buf bytes.Buffer
	_, err = buf.Write(gzipEncoded)
	if err != nil {
		log.Panicf("failed buffer decode data: %s", err)
	}

	zr, err := gzip.NewReader(&buf)
	if err != nil {
		log.Panicf("failed to create reader from buffer: %s", err)
	}
	defer zr.Close()

	decoded, err := ioutil.ReadAll(zr)
	if err != nil {
		log.Panicf("failed to decode data(GZIP): %s", err)
	}

	return &decoded
}

func init() {
	container = make(map[string]*[]byte)

	container["template/files.tmpl"] = decode(`` +
		`H4sICAAAAAAA/2RHVnRjR3hoZEdWY1ptbHNaWE11ZEcxd2JBPT0AnFTRattKEH3Wfs` +
		`VcQYJ0MRJcQi4k+CFxzCUPNwlNoNBgytqaVRZLu2a1SonFvvYD+on9kjKrlWLHbWn7` +
		`kuBZnTPnnJ3ZPIeZLhBKVGi4xQKWLxALWeE/pYauyy5M2daobONcfA5Xt3Bz+wDzq+` +
		`uHjLE8h7la6QILIERzxrrOcF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D7129p8UpuOEaaZzYqRtrK` +
		`7k4OnF4+3g0xEunN1rqAkgoR7l0EVUT9ZLpsm7ayCUvLfmXD5WftQxnwaj9bryvMVN` +
		`y8BjuHdoH9eClERYXu8+/FAkaPaa4I9TsZ5NiXYj/YwnGYnGEFY9O0dUkZKR4zZHN/` +
		`5A7spMtGWf5UHw+EB4AP5RyQnv2BFNePkmsBYwN6OxnV4oUXeXvi/ln0E/gfJv+XGh` +
		`X37JB0Zf5OWLlVx53dsSurc/xS5zFlMmnhb+00KtzKHWMrje5eqpSyiFTEsqLi5rGI` +
		`JLi/1n/tOpHvWcHO/jlYKv9EuBPn/z3wwfUPTtY0YNvTjwAAAP//DZuQ9K8DAAA=`)
}

// Content for the given filename
func Content(filename string) (*[]byte, error) {
	if content, ok := container[filename]; ok {
		return content, nil
	}
	return nil, fmt.Errorf("file2go %s not found", filename)
}

// ContentMust for the given filename, panics if filename not found
func ContentMust(filename string) *[]byte {
	content, err := Content(filename)
	if err != nil {
		log.Panic(err)
	}
	return content
}

// eof

template/files_test.go:

// Code generated by "file2go -v -d -t -o template/files.go template/*.tmpl"; DO NOT EDIT.

// Testing for included files:
// → template\files.tmpl
// → template\files_test.tmpl

package template

import (
	"testing"
)

func TestContentDoesNotExist(t *testing.T) {
	_, err := Content("")
	if err == nil {
		t.Fatalf("Content: returned no error")
	}
}

func TestContentExists(t *testing.T) {
  var err error
	_, err = Content("template\files.tmpl")
	if err != nil {
		t.Fatalf("Content \"template\files.tmpl\" not found: %s", err)
	}
	_, err = Content("template\files_test.tmpl")
	if err != nil {
		t.Fatalf("Content \"template\files_test.tmpl\" not found: %s", err)
	}
}

func TestContentMustDoesNotExist(t *testing.T) {
	defer func() {
		if r := recover(); r == nil {
			t.Fatalf("ContentMust: should have panic'd")
		}
	}()
	_ = ContentMust("")
	t.Fatalf("ContentMust: should have panic'd")
}

func TestContentMustExist(t *testing.T) {
	defer func() {
		if r := recover(); r != nil {
			t.Fatalf("ContentMust: should not have panic'd")
		}
	}()
	_ = ContentMust("template\files.tmpl")
	_ = ContentMust("template\files_test.tmpl")
}

// eof

Content is GZIPed and BASE64 encoded.

This is likely to change without further notice so => DO NOT USE ;)

Better Alternatives

https://github.com/a-urth/go-bindata might provide a more production ready approach?