github.com/ashleymcnamara/buffalo@v0.8.0/render/resolvers/rice_box.go (about)

     1  package resolvers
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"strings"
     7  	"sync"
     8  
     9  	rice "github.com/GeertJohan/go.rice"
    10  )
    11  
    12  var moot = &sync.Mutex{}
    13  
    14  // RiceBox uses the go.rice package to resolve files
    15  type RiceBox struct {
    16  	Box *rice.Box
    17  }
    18  
    19  // Read data from the rice.Box
    20  func (r *RiceBox) Read(name string) ([]byte, error) {
    21  	return r.Box.Bytes(name)
    22  }
    23  
    24  // Resolve the file from the rice.Box
    25  func (r *RiceBox) Resolve(name string) (string, error) {
    26  	var p string
    27  	var found bool
    28  	err := r.Box.Walk(".", func(path string, info os.FileInfo, err error) error {
    29  		if strings.HasSuffix(path, name) {
    30  			found = true
    31  			p = path
    32  			return err
    33  		}
    34  		return nil
    35  	})
    36  	if err != nil {
    37  		return p, err
    38  	}
    39  	if !found {
    40  		return p, fmt.Errorf("could not find file %s", name)
    41  	}
    42  	return p, nil
    43  }