github.com/HashDataInc/packer@v1.3.2/helper/multistep/doc.go (about)

     1  /*
     2  multistep is a Go library for building up complex actions using discrete,
     3  individual "steps." These steps are strung together and run in sequence
     4  to achieve a more complex goal. The runner handles cleanup, cancelling, etc.
     5  if necessary.
     6  
     7  ## Basic Example
     8  
     9  Make a step to perform some action. The step can access your "state",
    10  which is passed between steps by the runner.
    11  
    12  ```go
    13  type stepAdd struct{}
    14  
    15  func (s *stepAdd) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
    16      // Read our value and assert that it is they type we want
    17      value := state.Get("value").(int)
    18      fmt.Printf("Value is %d\n", value)
    19  
    20      // Store some state back
    21      state.Put("value", value + 1)
    22      return multistep.ActionContinue
    23  }
    24  
    25  func (s *stepAdd) Cleanup(multistep.StateBag) {
    26  	// This is called after all the steps have run or if the runner is
    27  	// cancelled so that cleanup can be performed.
    28  }
    29  ```
    30  
    31  Make a runner and call your array of Steps.
    32  
    33  ```go
    34  func main() {
    35      // Our "bag of state" that we read the value from
    36      state := new(multistep.BasicStateBag)
    37      state.Put("value", 0)
    38  
    39      steps := []multistep.Step{
    40          &stepAdd{},
    41          &stepAdd{},
    42          &stepAdd{},
    43      }
    44  
    45      runner := &multistep.BasicRunner{Steps: steps}
    46  
    47      // Executes the steps
    48      runner.Run(context.Background(), state)
    49  }
    50  ```
    51  
    52  This will produce:
    53  
    54  ```
    55  Value is 0
    56  Value is 1
    57  Value is 2
    58  ```
    59  */
    60  
    61  package multistep