github.com/vic3lord/terraform@v0.8.0-rc1.0.20170626102919-16c6dd2cb372/terraform/graph_walk.go (about)

     1  package terraform
     2  
     3  import (
     4  	"github.com/hashicorp/terraform/dag"
     5  )
     6  
     7  // GraphWalker is an interface that can be implemented that when used
     8  // with Graph.Walk will invoke the given callbacks under certain events.
     9  type GraphWalker interface {
    10  	EnterPath([]string) EvalContext
    11  	ExitPath([]string)
    12  	EnterVertex(dag.Vertex)
    13  	ExitVertex(dag.Vertex, error)
    14  	EnterEvalTree(dag.Vertex, EvalNode) EvalNode
    15  	ExitEvalTree(dag.Vertex, interface{}, error) error
    16  }
    17  
    18  // GrpahWalkerPanicwrapper can be optionally implemented to catch panics
    19  // that occur while walking the graph. This is not generally recommended
    20  // since panics should crash Terraform and result in a bug report. However,
    21  // this is particularly useful for situations like the shadow graph where
    22  // you don't ever want to cause a panic.
    23  type GraphWalkerPanicwrapper interface {
    24  	GraphWalker
    25  
    26  	// Panic is called when a panic occurs. This will halt the panic from
    27  	// propogating so if the walker wants it to crash still it should panic
    28  	// again. This is called from within a defer so runtime/debug.Stack can
    29  	// be used to get the stack trace of the panic.
    30  	Panic(dag.Vertex, interface{})
    31  }
    32  
    33  // GraphWalkerPanicwrap wraps an existing Graphwalker to wrap and swallow
    34  // the panics. This doesn't lose the panics since the panics are still
    35  // returned as errors as part of a graph walk.
    36  func GraphWalkerPanicwrap(w GraphWalker) GraphWalkerPanicwrapper {
    37  	return &graphWalkerPanicwrapper{
    38  		GraphWalker: w,
    39  	}
    40  }
    41  
    42  type graphWalkerPanicwrapper struct {
    43  	GraphWalker
    44  }
    45  
    46  func (graphWalkerPanicwrapper) Panic(dag.Vertex, interface{}) {}
    47  
    48  // NullGraphWalker is a GraphWalker implementation that does nothing.
    49  // This can be embedded within other GraphWalker implementations for easily
    50  // implementing all the required functions.
    51  type NullGraphWalker struct{}
    52  
    53  func (NullGraphWalker) EnterPath([]string) EvalContext                  { return new(MockEvalContext) }
    54  func (NullGraphWalker) ExitPath([]string)                               {}
    55  func (NullGraphWalker) EnterVertex(dag.Vertex)                          {}
    56  func (NullGraphWalker) ExitVertex(dag.Vertex, error)                    {}
    57  func (NullGraphWalker) EnterEvalTree(v dag.Vertex, n EvalNode) EvalNode { return n }
    58  func (NullGraphWalker) ExitEvalTree(dag.Vertex, interface{}, error) error {
    59  	return nil
    60  }