-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add the first side-effectful filters behind --enable-filters.
`--enable-filters` takes a comma-separated list of non-standard filters to enable in `p2`. Notably this implements `write_file` and `make_dirs`, which allow a template execution to have filesystem side-effects. The write_file filter allows using a single template to output multiple files, including customization of filenames from other templated values. The write_file filter returns it's literal input, so stdout / file output can be used as a log of all templated values. make_dirs will create the directory path given as it's argument. It is the natural companion of write_files, but separate in order to be explicit about expected template operations. `--enable-noop-filters` loads all custom filters but disables their side-effects, in order to allow easy debugging since filter use is not necessarily obvious. This feature is considered experimental at the moment, and may be deprecated or changed in future releases.
- Loading branch information
Showing
3 changed files
with
201 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"github.com/flosch/pongo2" | ||
"os" | ||
) | ||
|
||
// This noop filter is registered in place of custom filters which otherwise | ||
// passthru their input (our file filters). This allows debugging and testing | ||
// without running file operations. | ||
func filterNoopPassthru(in *pongo2.Value, param *pongo2.Value) (*pongo2.Value, *pongo2.Error) { | ||
return in, nil | ||
} | ||
|
||
// This noop filter is registered in place of custom filters which otherwise | ||
// produce no output. | ||
func filterNoop(in *pongo2.Value, param *pongo2.Value) (*pongo2.Value, *pongo2.Error) { | ||
return nil, nil | ||
} | ||
|
||
// This filter writes the content of its input to the filename specified as its | ||
// argument. The templated content is returned verbatim. | ||
func filterWriteFile(in *pongo2.Value, param *pongo2.Value) (*pongo2.Value, *pongo2.Error) { | ||
if !in.IsString() { | ||
return nil, &pongo2.Error{ | ||
Sender: "filter:write_file", | ||
ErrorMsg: "Filter input must be of type 'string'.", | ||
} | ||
} | ||
|
||
if !param.IsString() { | ||
return nil, &pongo2.Error{ | ||
Sender: "filter:write_file", | ||
ErrorMsg: "Filter parameter must be of type 'string'.", | ||
} | ||
} | ||
|
||
f, err := os.OpenFile(param.String(), os.O_CREATE|os.O_TRUNC|os.O_WRONLY, os.FileMode(0777)) | ||
if err != nil { | ||
return nil, &pongo2.Error{ | ||
Sender: "filter:write_file", | ||
ErrorMsg: fmt.Sprintf("Could not open file for output: %s", err.Error()), | ||
} | ||
} | ||
defer f.Close() | ||
|
||
_, werr := f.WriteString(in.String()) | ||
if werr != nil { | ||
return nil, &pongo2.Error{ | ||
Sender: "filter:write_file", | ||
ErrorMsg: fmt.Sprintf("Could not write file for output: %s", werr.Error()), | ||
} | ||
} | ||
|
||
return in, nil | ||
} | ||
|
||
// This filter makes a directory based on the value of its argument. It passes | ||
// through any content without alteration. This allows chaining with write-file. | ||
func filterMakeDirs(in *pongo2.Value, param *pongo2.Value) (*pongo2.Value, *pongo2.Error) { | ||
if !param.IsString() { | ||
return nil, &pongo2.Error{ | ||
Sender: "filter:write_file", | ||
ErrorMsg: "Filter parameter must be of type 'string'.", | ||
} | ||
} | ||
|
||
err := os.MkdirAll(in.String(), os.FileMode(0777)) | ||
if err != nil { | ||
return nil, &pongo2.Error{ | ||
Sender: "filter:make_dirs", | ||
ErrorMsg: fmt.Sprintf("Could not create directories: %s %s", in.String(), err.Error()), | ||
} | ||
} | ||
|
||
return in, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters