github.phpd.cn/hashicorp/packer@v1.3.2/template/interpolate/parse.go (about) 1 package interpolate 2 3 import ( 4 "fmt" 5 "text/template" 6 "text/template/parse" 7 ) 8 9 // functionsCalled returns a map (to be used as a set) of the functions 10 // that are called from the given text template. 11 func functionsCalled(t *template.Template) map[string]struct{} { 12 result := make(map[string]struct{}) 13 functionsCalledWalk(t.Tree.Root, result) 14 return result 15 } 16 17 func functionsCalledWalk(raw parse.Node, r map[string]struct{}) { 18 switch node := raw.(type) { 19 case *parse.ActionNode: 20 functionsCalledWalk(node.Pipe, r) 21 case *parse.CommandNode: 22 if in, ok := node.Args[0].(*parse.IdentifierNode); ok { 23 r[in.Ident] = struct{}{} 24 } 25 26 for _, n := range node.Args[1:] { 27 functionsCalledWalk(n, r) 28 } 29 case *parse.ListNode: 30 for _, n := range node.Nodes { 31 functionsCalledWalk(n, r) 32 } 33 case *parse.PipeNode: 34 for _, n := range node.Cmds { 35 functionsCalledWalk(n, r) 36 } 37 case *parse.StringNode, *parse.TextNode: 38 // Ignore 39 default: 40 panic(fmt.Sprintf("unknown type: %T", node)) 41 } 42 }