import { PuppetASTObject , PuppetASTContainerContext , Resolver , ResolveError , PuppetASTReturn , PuppetASTResourcesDeclaration , PuppetASTVariable , OrderedDictionary , PuppetASTValue , PuppetASTPrimitive , PuppetASTList , PuppetASTKeyedEntry } from "./ast" ; import { isArray , isObject } from "util" ; import { GlobalVariableResolverResults } from "./util" ; type BuiltinFunctionCallback = ( caller : PuppetASTObject , context : PuppetASTContainerContext , resolver : Resolver , args : any [ ] ) => Promise < any > ; const BuiltInFunctions = { "alert" : async function ( caller , context , resolver , args ) : Promise < any > { console . log ( args [ 0 ] ) ; } , "create_resources" : async function ( caller , context , resolver , args ) : Promise < any > { } , "ensure_packages" : async function ( caller , context , resolver , args ) : Promise < any > { } , "fail" : async function ( caller , context , resolver , args ) { throw new ResolveError ( caller , args [ 0 ] ) ; } , "defined" : async function ( caller , context , resolver , args ) { const obj = args [ 0 ] ; if ( obj instanceof PuppetASTVariable ) { const var_ = < PuppetASTVariable > obj ; const def = await var_ . defined ( context , resolver ) ; return def != GlobalVariableResolverResults . MISSING ; } const def = await obj . resolve ( context , resolver ) ; return < boolean > ( def ) ; } , "fact" : async function ( caller , context , resolver , args ) { const obj = args [ 0 ] ; const factName = await obj . resolve ( context , resolver ) ; const facts = resolver . getGlobalVariable ( "facts" ) ; return facts [ factName ] ; } , "template" : async function ( caller , context , resolver , args ) { return "" ; } , "require" : async function ( caller , context , resolver , args ) { await resolver . resolveClass ( args [ 0 ] , true ) ; } , "contain" : async function ( caller , context , resolver , args ) { await resolver . resolveClass ( args [ 0 ] , true ) ; } , "include" : async function ( caller , context , resolver , args ) { const className = args [ 0 ] ; await resolver . resolveClass ( className , true ) ; } , "hiera_include" : async function ( caller , context , resolver , args ) { const key = args [ 0 ] ; const resolve = async ( ) : Promise < number > => { const hierarchy = resolver . hasGlobalVariable ( key ) ; if ( hierarchy == GlobalVariableResolverResults . MISSING ) { return hierarchy ; } const classes = resolver . getGlobalVariable ( key ) ; if ( isArray ( classes ) ) { for ( const className of classes ) { try { const resolved = await resolver . resolveClass ( className , true ) ; resolved . setOption ( "hiera_include" , key ) ; } catch ( e ) { console . log ( "Failed to resolve class " + className + " from hiera_include('" + key + "'): " + e . toString ( ) ) ; } } } return hierarchy ; } ; await resolver . resolveHieraSource ( "hiera_include" , key , resolve ) ; } , "hiera_resources" : async function ( caller , context , resolver , args ) { const key = args [ 0 ] ; const resolve = async ( ) : Promise < number > => { const hierarchy = resolver . hasGlobalVariable ( key ) ; if ( hierarchy == GlobalVariableResolverResults . MISSING ) { return hierarchy ; } const resources = resolver . getGlobalVariable ( key ) ; if ( isObject ( resources ) ) { for ( const definedTypeName in resources ) { const titles = resources [ definedTypeName ] ; const bodies = new Array < PuppetASTObject > ( ) ; for ( const title in titles ) { const entry = new OrderedDictionary ( ) ; entry . put ( "title" , new PuppetASTValue ( title ) ) ; const ops = new Array < PuppetASTObject > ( ) ; entry . put ( "ops" , new PuppetASTList ( ops ) ) ; const properties = titles [ title ] ; for ( const propertyName in properties ) { const value = properties [ propertyName ] ; ops . push ( new PuppetASTKeyedEntry ( [ new PuppetASTValue ( propertyName ) , new PuppetASTValue ( value ) ] ) ) ; } bodies . push ( entry ) ; } const options = new OrderedDictionary ( ) ; options . put ( "type" , new PuppetASTPrimitive ( definedTypeName ) ) ; options . put ( "bodies" , new PuppetASTList ( bodies ) ) ; const resourcesEntry = new PuppetASTResourcesDeclaration ( [ options ] , true ) ; resourcesEntry . hierarchy = hierarchy ; try { await resourcesEntry . resolve ( context , resolver ) ; } catch ( e ) { console . log ( e . toString ( ) ) ; continue ; } for ( const resource of resourcesEntry . entries . getValues ( ) ) { resource . setOption ( "hiera_resources" , key ) ; } } } return hierarchy ; } ; await resolver . resolveHieraSource ( "hiera_resources" , key , resolve ) ; } , "return" : async function ( caller , context , resolver , args ) { throw new PuppetASTReturn ( args [ 0 ] ) ; } , "all" : async function ( caller , context , resolver , args ) { } , "annotate" : async function ( caller , context , resolver , args ) { } , "any" : async function ( caller , context , resolver , args ) { } , "assert_type" : async function ( caller , context , resolver , args ) { } , "binary_file" : async function ( caller , context , resolver , args ) { } , "break" : async function ( caller , context , resolver , args ) { } , "call" : async function ( caller , context , resolver , args ) { } , "convert_to" : async function ( caller , context , resolver , args ) { } , "crit" : async function ( caller , context , resolver , args ) { } , "debug" : async function ( caller , context , resolver , args ) { } , "dig" : async function ( caller , context , resolver , args ) { } , "digest" : async function ( caller , context , resolver , args ) { } , "each" : async function ( caller , context , resolver , args ) { } , "emerg" : async function ( caller , context , resolver , args ) { } , "empty" : async function ( caller , context , resolver , args ) { } , "epp" : async function ( caller , context , resolver , args ) { } , "err" : async function ( caller , context , resolver , args ) { } , "eyaml_lookup_key" : async function ( caller , context , resolver , args ) { } , "file" : async function ( caller , context , resolver , args ) { } , "filter" : async function ( caller , context , resolver , args ) { } , "find_file" : async function ( caller , context , resolver , args ) { } , "flatten" : async function ( caller , context , resolver , args ) { } , "fqdn_rand" : async function ( caller , context , resolver , args ) { } , "generate" : async function ( caller , context , resolver , args ) { } , "hiera" : async function ( caller , context , resolver , args ) { } , "hiera_array" : async function ( caller , context , resolver , args ) { } , "hiera_hash" : async function ( caller , context , resolver , args ) { } , "hocon_data" : async function ( caller , context , resolver , args ) { } , "import" : async function ( caller , context , resolver , args ) { } , "info" : async function ( caller , context , resolver , args ) { } , "inline_epp" : async function ( caller , context , resolver , args ) { } , "inline_template" : async function ( caller , context , resolver , args ) { } , "join" : async function ( caller , context , resolver , args ) { } , "json_data" : async function ( caller , context , resolver , args ) { } , "keys" : async function ( caller , context , resolver , args ) { } , "length" : async function ( caller , context , resolver , args ) { } , "lest" : async function ( caller , context , resolver , args ) { } , "lookup" : async function ( caller , context , resolver , args ) { } , "map" : async function ( caller , context , resolver , args ) { } , "match" : async function ( caller , context , resolver , args ) { } , "md5" : async function ( caller , context , resolver , args ) { } , "module_directory" : async function ( caller , context , resolver , args ) { } , "new" : async function ( caller , context , resolver , args ) { } , "next" : async function ( caller , context , resolver , args ) { } , "notice" : async function ( caller , context , resolver , args ) { } , "realize" : async function ( caller , context , resolver , args ) { } , "reduce" : async function ( caller , context , resolver , args ) { } , "regsubst" : async function ( caller , context , resolver , args ) { } , "reverse_each" : async function ( caller , context , resolver , args ) { } , "scanf" : async function ( caller , context , resolver , args ) { } , "sha1" : async function ( caller , context , resolver , args ) { } , "sha256" : async function ( caller , context , resolver , args ) { } , "shellquote" : async function ( caller , context , resolver , args ) { } , "slice" : async function ( caller , context , resolver , args ) { } , "split" : async function ( caller , context , resolver , args ) { } , "sprintf" : async function ( caller , context , resolver , args ) { } , "step" : async function ( caller , context , resolver , args ) { } , "strftime" : async function ( caller , context , resolver , args ) { } , "tag" : async function ( caller , context , resolver , args ) { } , "tagged" : async function ( caller , context , resolver , args ) { } , "then" : async function ( caller , context , resolver , args ) { } , "tree_each" : async function ( caller , context , resolver , args ) { } , "type" : async function ( caller , context , resolver , args ) { } , "unique" : async function ( caller , context , resolver , args ) { } , "unwrap" : async function ( caller , context , resolver , args ) { } , "values" : async function ( caller , context , resolver , args ) { } , "versioncmp" : async function ( caller , context , resolver , args ) { } , "warning" : async function ( caller , context , resolver , args ) { } , "with" : async function ( caller , context , resolver , args ) { } , "yaml_data" : async function ( caller , context , resolver , args ) { } } ; export function GetBuiltinFunction ( name ) { return BuiltInFunctions [ name ] ; }