Documentation ¶
Overview ¶
Mule is a tool to be used with 'go generate' to embed external resources files into Go code and therefore into the resulting executable.
Scenario ¶
An often used scenario in developing go applications is to embed external resources to be able to create only one binary without any dependencies. There are a number of existing packages solving this problem, like bindata (https://github.com/a-urth/go-bindata), packr (https://github.com/gobuffalo/packr/tree/master/v2) or packger (https://github.com/markbates/pkger) and if you are looking for fancy features and unicorns you should probably better go there. Usually they are creating a kind of virtual file system. Generelly this really a lot more than I need for my simple usecase in including one or two files into a small cli program.
Compared to that mule is extremely simple. The only thing you need to embed a file to your code is one line in your code (a go generate command). And you need just another one line to access the embedded file from your code.
See https://github.com/wlbr/mule/blob/master/example/mulex.go for a very, very simple example.
It is intended to be run by go generate.
Usage ¶
Simply add a line
//go:generate mule mybinary.file
for each resource you want to embed. Every time you run a 'go generate' in the corresponding folder, the file 'mybinary.go' will be created. It contains a function 'mybinaryResource' returning the resource as a []byte.
You may use 'mule mytbinary.file' directly on the command line.
Switches ¶
Usage of mule: 'mule [switches] resourcefilename'
-e export the generated, the resource returning function. Default (false) means the function will not be exported. -f no formatting of the generated source. Default false means source will be formatted with gofmt. -n string name of generated, the resource returning function. Its name will have 'Resource' attached. Will be set to $(basename -s .ext outputfile) if empty (default). Take care of "-" within the name, especially when the name is calculated from the resources file name. A '-' would create an invalid go function name -o string name of output file. Defaults to name of resource file excluding extension + '.go'. -p string name of package to be used in generated code (default "main"). -t string name of alternate code generation template file. If empty (default), then the embedded template will be used. Template variables supplied are: .Name, .Package, .Content