mirror of
https://github.com/Luzifer/korvike.git
synced 2024-12-20 11:11:19 +00:00
Add ability to include files
This commit is contained in:
parent
479789eef2
commit
555bca47f4
2 changed files with 23 additions and 0 deletions
|
@ -12,3 +12,5 @@
|
|||
`echo "{{ .foo }}" | korvike -v foo=bar => "bar"`
|
||||
- Read environment variables and replace them inside the template:
|
||||
`export FOO=bar; echo '{{env "FOO"}}' | korvike => "bar"`
|
||||
- Read a file and place it inside the template:
|
||||
`echo "Hello World" > hello; echo '{{file "hello"}}' | korvike => "Hello World"`
|
||||
|
|
21
func_file.go
Normal file
21
func_file.go
Normal file
|
@ -0,0 +1,21 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"os"
|
||||
)
|
||||
|
||||
func init() {
|
||||
registerFunction("file", func(name string, v ...string) string {
|
||||
defaultValue := ""
|
||||
if len(v) > 0 {
|
||||
defaultValue = v[0]
|
||||
}
|
||||
if _, err := os.Stat(name); err == nil {
|
||||
if rawValue, err := ioutil.ReadFile(name); err == nil {
|
||||
return string(rawValue)
|
||||
}
|
||||
}
|
||||
return defaultValue
|
||||
})
|
||||
}
|
Loading…
Reference in a new issue