File: debug_runner.go

package info (click to toggle)
golang-github-mitchellh-multistep 0.0~git20170316.391576a-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, buster, forky, sid, trixie
  • size: 84 kB
  • sloc: makefile: 2
file content (123 lines) | stat: -rw-r--r-- 3,217 bytes parent folder | download
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package multistep

import (
	"fmt"
	"reflect"
	"sync"
)

// DebugLocation is the location where the pause is occuring when debugging
// a step sequence. "DebugLocationAfterRun" is after the run of the named
// step. "DebugLocationBeforeCleanup" is before the cleanup of the named
// step.
type DebugLocation uint

const (
	DebugLocationAfterRun DebugLocation = iota
	DebugLocationBeforeCleanup
)

// StepWrapper is an interface that wrapped steps can implement to expose their
// inner step names to the debug runner.
type StepWrapper interface {
	// InnerStepName should return the human readable name of the wrapped step.
	InnerStepName() string
}

// DebugPauseFn is the type signature for the function that is called
// whenever the DebugRunner pauses. It allows the caller time to
// inspect the state of the multi-step sequence at a given step.
type DebugPauseFn func(DebugLocation, string, StateBag)

// DebugRunner is a Runner that runs the given set of steps in order,
// but pauses between each step until it is told to continue.
type DebugRunner struct {
	// Steps is the steps to run. These will be run in order.
	Steps []Step

	// PauseFn is the function that is called whenever the debug runner
	// pauses. The debug runner continues when this function returns.
	// The function is given the state so that the state can be inspected.
	PauseFn DebugPauseFn

	l      sync.Mutex
	runner *BasicRunner
}

func (r *DebugRunner) Run(state StateBag) {
	r.l.Lock()
	if r.runner != nil {
		panic("already running")
	}
	r.runner = new(BasicRunner)
	r.l.Unlock()

	pauseFn := r.PauseFn

	// If no PauseFn is specified, use the default
	if pauseFn == nil {
		pauseFn = DebugPauseDefault
	}

	// Rebuild the steps so that we insert the pause step after each
	steps := make([]Step, len(r.Steps)*2)
	for i, step := range r.Steps {
		steps[i*2] = step
		name := ""
		if wrapped, ok := step.(StepWrapper); ok {
			name = wrapped.InnerStepName()
		} else {
			name = reflect.Indirect(reflect.ValueOf(step)).Type().Name()
		}
		steps[(i*2)+1] = &debugStepPause{
			name,
			pauseFn,
		}
	}

	// Then just use a basic runner to run it
	r.runner.Steps = steps
	r.runner.Run(state)
}

func (r *DebugRunner) Cancel() {
	r.l.Lock()
	defer r.l.Unlock()

	if r.runner != nil {
		r.runner.Cancel()
	}
}

// DebugPauseDefault is the default pause function when using the
// DebugRunner if no PauseFn is specified. It outputs some information
// to stderr about the step and waits for keyboard input on stdin before
// continuing.
func DebugPauseDefault(loc DebugLocation, name string, state StateBag) {
	var locationString string
	switch loc {
	case DebugLocationAfterRun:
		locationString = "after run of"
	case DebugLocationBeforeCleanup:
		locationString = "before cleanup of"
	}

	fmt.Printf("Pausing %s step '%s'. Press any key to continue.\n", locationString, name)

	var line string
	fmt.Scanln(&line)
}

type debugStepPause struct {
	StepName string
	PauseFn  DebugPauseFn
}

func (s *debugStepPause) Run(state StateBag) StepAction {
	s.PauseFn(DebugLocationAfterRun, s.StepName, state)
	return ActionContinue
}

func (s *debugStepPause) Cleanup(state StateBag) {
	s.PauseFn(DebugLocationBeforeCleanup, s.StepName, state)
}