Add template function isFile (#2045)
* Add template function isFile * Update context_funcs.go
This commit is contained in:
		
							parent
							
								
									45be24da53
								
							
						
					
					
						commit
						ed436ba68b
					
				|  | @ -1,6 +1,7 @@ | ||||||
| package tmpl | package tmpl | ||||||
| 
 | 
 | ||||||
| import ( | import ( | ||||||
|  | 	"errors" | ||||||
| 	"fmt" | 	"fmt" | ||||||
| 	"io" | 	"io" | ||||||
| 	"os" | 	"os" | ||||||
|  | @ -20,6 +21,7 @@ type Values = map[string]interface{} | ||||||
| func (c *Context) createFuncMap() template.FuncMap { | func (c *Context) createFuncMap() template.FuncMap { | ||||||
| 	funcMap := template.FuncMap{ | 	funcMap := template.FuncMap{ | ||||||
| 		"exec":             c.Exec, | 		"exec":             c.Exec, | ||||||
|  | 		"isFile":           c.IsFile, | ||||||
| 		"readFile":         c.ReadFile, | 		"readFile":         c.ReadFile, | ||||||
| 		"readDir":          c.ReadDir, | 		"readDir":          c.ReadDir, | ||||||
| 		"toYaml":           ToYaml, | 		"toYaml":           ToYaml, | ||||||
|  | @ -117,6 +119,24 @@ func (c *Context) Exec(command string, args []interface{}, inputs ...string) (st | ||||||
| 	return string(bytes), nil | 	return string(bytes), nil | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | func (c *Context) IsFile(filename string) (bool, error) { | ||||||
|  | 	var path string | ||||||
|  | 	if filepath.IsAbs(filename) { | ||||||
|  | 		path = filename | ||||||
|  | 	} else { | ||||||
|  | 		path = filepath.Join(c.basePath, filename) | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	stat, err := os.Stat(path) | ||||||
|  | 	if err == nil { | ||||||
|  | 		return !stat.IsDir(), nil | ||||||
|  | 	} | ||||||
|  | 	if errors.Is(err, os.ErrNotExist) { | ||||||
|  | 		return false, nil | ||||||
|  | 	} | ||||||
|  | 	return false, err | ||||||
|  | } | ||||||
|  | 
 | ||||||
| func (c *Context) ReadFile(filename string) (string, error) { | func (c *Context) ReadFile(filename string) (string, error) { | ||||||
| 	var path string | 	var path string | ||||||
| 	if filepath.IsAbs(filename) { | 	if filepath.IsAbs(filename) { | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue