-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.go
More file actions
41 lines (34 loc) · 850 Bytes
/
cli.go
File metadata and controls
41 lines (34 loc) · 850 Bytes
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
package cli
import (
"bufio"
"crypto/rand"
"fmt"
"os"
"github.com/fsm/fsm"
)
const platform = "cli"
// Start begins the CLI target for a fsm.StateMachine
func Start(stateMachine fsm.StateMachine, store fsm.Store) {
uuid := uuid()
stateMap := fsm.GetStateMap(stateMachine)
reader := bufio.NewReader(os.Stdin)
emitter := &emitter{}
for {
fmt.Print("-> ")
text, _ := reader.ReadString('\n')
text = text[:len(text)-1]
// Step
fsm.Step(platform, uuid, text, fsm.TextInputTransformer, store, emitter, stateMap)
}
}
// uuid generates a UUID that isn't particularly compliant
// to any spec but is more than good enough for our use case
func uuid() string {
b := make([]byte, 16)
_, err := rand.Read(b)
if err != nil {
return ""
}
str := fmt.Sprintf("%X-%X-%X-%X-%X", b[0:4], b[4:6], b[6:8], b[8:10], b[10:])
return str
}