File: parse.go

package info (click to toggle)
packer 0.10.2%2Bdfsg-6
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 4,728 kB
  • ctags: 4,626
  • sloc: sh: 321; makefile: 73
file content (42 lines) | stat: -rw-r--r-- 998 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package interpolate

import (
	"fmt"
	"text/template"
	"text/template/parse"
)

// functionsCalled returns a map (to be used as a set) of the functions
// that are called from the given text template.
func functionsCalled(t *template.Template) map[string]struct{} {
	result := make(map[string]struct{})
	functionsCalledWalk(t.Tree.Root, result)
	return result
}

func functionsCalledWalk(raw parse.Node, r map[string]struct{}) {
	switch node := raw.(type) {
	case *parse.ActionNode:
		functionsCalledWalk(node.Pipe, r)
	case *parse.CommandNode:
		if in, ok := node.Args[0].(*parse.IdentifierNode); ok {
			r[in.Ident] = struct{}{}
		}

		for _, n := range node.Args[1:] {
			functionsCalledWalk(n, r)
		}
	case *parse.ListNode:
		for _, n := range node.Nodes {
			functionsCalledWalk(n, r)
		}
	case *parse.PipeNode:
		for _, n := range node.Cmds {
			functionsCalledWalk(n, r)
		}
	case *parse.StringNode, *parse.TextNode:
		// Ignore
	default:
		panic(fmt.Sprintf("unknown type: %T", node))
	}
}