Last active
August 29, 2015 14:26
-
-
Save martingehrke/3e8012ff205044ab6795 to your computer and use it in GitHub Desktop.
Reading a file in cfengine can be expensive. The code below shows how do it right.
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 requires cfengine 3.7 for the if and unless | |
vars: | |
"file" string => "/full/path/to/file.json"; | |
classname:: #always gate file reads as tightly as possible | |
"data" data => readjson("$(file)", inf), | |
if => fileexists("$(file)"), #don't try to read if file is missing | |
unless => isvariable("$(this.promiser)"); #don't read file more than once | |
#you can't do all that in pre 3.7 but you can do some | |
# sometimes I prefer an error if the file is missing | |
vars: | |
"file" string => "/full/path/to/file.json"; | |
classname:: #same as above | |
"data" data => readjson("$(file)", inf), | |
ifvarclass => not(isvariable("$(this.promiser)")); # unless == if not |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
#1 is up for discussion. Sometimes I prefer to see an error if the file is absent. It is easier to troubleshoot