package httpfs

import (
	"io"
	"net/http"
	"os"

	"github.com/CloudyKit/jet"
)

type httpFileSystemLoader struct {
	fs http.FileSystem
}

// NewLoader returns an initialized loader serving the passed http.FileSystem.
func NewLoader(fs http.FileSystem) jet.Loader {
	return &httpFileSystemLoader{fs: fs}
}

// Open opens the file via the internal http.FileSystem. It is the callers duty to close the file.
func (l *httpFileSystemLoader) Open(name string) (io.ReadCloser, error) {
	return l.fs.Open(name)
}

// Exists checks if the template name exists by walking the list of template paths
// returns string with the full path of the template and bool true if the template file was found
func (l *httpFileSystemLoader) Exists(name string) (string, bool) {
	if l.fs == nil {
		return "", false
	}
	if f, err := l.Open(name); err == nil {
		f.Close()
		return name, true
	}
	return "", false
}

package httpfs

import (
	"io"
	"net/http"
	"os"

	"github.com/CloudyKit/jet"
)

type httpFileSystemLoader struct {
	fs http.FileSystem
}

// NewLoader returns an initialized loader serving the passed http.FileSystem.
func NewLoader(fs http.FileSystem) jet.Loader {
	return &httpFileSystemLoader{fs: fs}
}

// Open opens the file via the internal http.FileSystem. It is the callers duty to close the file.
func (l *httpFileSystemLoader) Open(name string) (io.ReadCloser, error) {
	if l.fs == nil {
		return nil, &os.PathError{Op: "open", Path: name, Err: os.ErrNotExist}
	}
	return l.fs.Open(name)
}

// Exists checks if the template name exists by walking the list of template paths
// returns string with the full path of the template and bool true if the template file was found
func (l *httpFileSystemLoader) Exists(name string) (string, bool) {
	if l.fs == nil {
		return "", false
	}
	if f, err := l.Open(name); err == nil {
		f.Close()
		return name, true
	}
	return "", false
}

Fix `httpFileSystemLoader.Open` to not crash on nil
Fix `httpFileSystemLoader.Open` to not crash on nil

The reason is that in certain cases (mainly in development) this loader
might be configured with an empty file system – it accepts an interface
so passing nil just works. If `Open` is called it shouldn’t crash.
