github.com/posener/gitfs@v1.2.2-0.20200410105819-ea4e48d73ab9/examples/templates/templates.go (about)

     1  // An example that shows how gitfs helps using template files with Go code smoothly.
     2  package main
     3  
     4  import (
     5  	"context"
     6  	"log"
     7  	"os"
     8  
     9  	"github.com/posener/gitfs"
    10  	"github.com/posener/gitfs/fsutil"
    11  )
    12  
    13  // Add debug mode environment variable. When running with `LOCAL_DEBUG=.`, the
    14  // local git repository will be used instead of the remote github.
    15  var localDebug = os.Getenv("LOCAL_DEBUG")
    16  
    17  func main() {
    18  	ctx := context.Background()
    19  	// Open repository 'github.com/posener/gitfs' at path
    20  	// 'examples/templates' with the local option from
    21  	// environment variable.
    22  	fs, err := gitfs.New(ctx,
    23  		"github.com/posener/gitfs/examples/templates",
    24  		gitfs.OptLocal(localDebug))
    25  	if err != nil {
    26  		log.Fatalf("Failed initializing git filesystem: %s.", err)
    27  	}
    28  	// Parse templates from the loaded filesystem using a glob
    29  	// pattern.
    30  	tmpls, err := fsutil.TmplParseGlob(fs, nil, "*.gotmpl")
    31  	if err != nil {
    32  		log.Fatalf("Failed parsing templates.")
    33  	}
    34  	// Execute a template according to its file name.
    35  	tmpls.ExecuteTemplate(os.Stdout, "tmpl1.gotmpl", "Foo")
    36  }