11 Create the Audit File Set at Runtime

Use the JafLib API when your rule pack needs to audit a dynamically derived file set, for example, by inspecting the current state of other data sets.

First, the configuration file should be setup to specify no files.

{
   "files"   : [],
   "exclude" : [],     // if needed
 
 
   "rulePacks" : {
                       // your rulepack reference
                       // this pack will contain the fileset generating rule
                 }          
}

Next you need to create the rule that will generate the file lists. The rule needs to listen for the JAF lifecycle phase startaudit. This phase occurs immediately before the general auditing phase begins.

This rule uses setFileset() in ruleCtx.utils.jafLib to set the file set. Note that full file paths (not relative) must be used.

let  verboseMode;
 
function  register(regCtx)
{
   // optional feedback
   verboseMode = regCtx.sysOpts.verboseMode;
 
   return { "startaudit" : _fn };
};
 
 
function  _fn(ruleCtx)
{
   // create an array of full filepaths
   let fileset = _computeFileSet(ruleCtx);
   let exclude = _computeExcludeSet(ruleCtx);
 
 
  if (fileset.length)
   {
      if (verboseMode) { console.log(`Rule 'my-fileset-generator': injecting ${fileset.length} files into configuration 'files'`); }
 
      // param exclude may be omitted
      ruleCtx.utils.jafLib.setFileset(ruleCtx, fileset, exclude);
   }
};