Example #1
0
func templateSource(fs vfs.Opener, fName string) (string, error) {
	tmplFile, err := fs.Open(fName)
	if err != nil {
		return "", err
	}
	tmplB := &bytes.Buffer{}
	_, err = tmplB.ReadFrom(tmplFile)
	return tmplB.String(), err
}
Example #2
0
File: util.go Project: 2722/lantern
// IsTextFile reports whether the file has a known extension indicating
// a text file, or if a significant chunk of the specified file looks like
// correct UTF-8; that is, if it is likely that the file contains human-
// readable text.
func IsTextFile(fs vfs.Opener, filename string) bool {
	// if the extension is known, use it for decision making
	if isText, found := textExt[pathpkg.Ext(filename)]; found {
		return isText
	}

	// the extension is not known; read an initial chunk
	// of the file and check if it looks like text
	f, err := fs.Open(filename)
	if err != nil {
		return false
	}
	defer f.Close()

	var buf [1024]byte
	n, err := f.Read(buf[0:])
	if err != nil {
		return false
	}

	return IsText(buf[0:n])
}
Example #3
0
func MakeFnIncludeFile(opener vfs.Opener, rules *template.Rules) template.Rule {
	return func(path []interface{}, node interface{}) (interface{}, interface{}) {
		key := interface{}(nil)
		if len(path) > 0 {
			key = path[len(path)-1]
		}

		argInterface, ok := singleKey(node, "Fn::IncludeFile")
		if !ok {
			return key, node //passthru
		}

		var argString string
		if argString, ok = argInterface.(string); !ok {
			return key, node //passthru
		}

		var absPath string
		var err error
		if absPath, err = filepath.Abs(argString); err != nil {
			panic(fmt.Errorf("Error opening imported file '%s': %s", argString, err))
		}

		var jsonStream io.Reader
		if jsonStream, err = opener.Open(absPath); err != nil {
			panic(fmt.Errorf("Error opening imported file '%s': %s", absPath, err))
		}

		dec := json.NewDecoder(jsonStream)
		includedTemplate := interface{}(nil)
		if err := dec.Decode(&includedTemplate); err != nil {
			panic(fmt.Errorf("Error loading imported file '%s': %s", argString, err))
		}

		key, generated := template.Walk(path, includedTemplate, rules)
		return key, interface{}(generated)
	}
}
Example #4
0
func MakeFnIncludeFileRaw(opener vfs.Opener) template.Rule {
	return func(path []interface{}, node interface{}) (interface{}, interface{}) {
		key := interface{}(nil)
		if len(path) > 0 {
			key = path[len(path)-1]
		}

		argInterface, ok := singleKey(node, "Fn::IncludeFileRaw")
		if !ok {
			return key, node //passthru
		}

		var argString string
		if argString, ok = argInterface.(string); !ok {
			return key, node //passthru
		}

		var absPath string
		var err error
		if absPath, err = filepath.Abs(argString); err != nil {
			panic(fmt.Errorf("Error opening imported file '%s': %s", argString, err))
		}

		var dataStream io.Reader
		if dataStream, err = opener.Open(absPath); err != nil {
			panic(fmt.Errorf("Error opening imported file '%s': %s", absPath, err))
		}

		var data []byte
		if data, err = ioutil.ReadAll(dataStream); err != nil {
			panic(fmt.Errorf("Error loading imported file '%s': %s", argString, err))
		}

		return key, interface{}(string(data))
	}
}