-
Notifications
You must be signed in to change notification settings - Fork 6
/
script.go
166 lines (141 loc) · 3.29 KB
/
script.go
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Cilium
package hive
import (
"context"
"errors"
"fmt"
"io"
"log/slog"
"os"
"os/signal"
"time"
"github.com/cilium/hive/cell"
"github.com/cilium/hive/script"
"golang.org/x/term"
)
func NewScriptCmd(name string, cmd script.Cmd) ScriptCmdOut {
return ScriptCmdOut{ScriptCmd: ScriptCmd{name, cmd}}
}
func NewScriptCmds(cmds map[string]script.Cmd) (out ScriptCmdsOut) {
out.ScriptCmds = make([]ScriptCmd, 0, len(cmds))
for name, cmd := range cmds {
out.ScriptCmds = append(out.ScriptCmds, ScriptCmd{name, cmd})
}
return out
}
type ScriptCmd struct {
Name string
Cmd script.Cmd
}
type ScriptCmds struct {
cell.In
ScriptCmds []ScriptCmd `group:"script-commands"`
}
func (sc ScriptCmds) Map() map[string]script.Cmd {
m := make(map[string]script.Cmd, len(sc.ScriptCmds))
for _, c := range sc.ScriptCmds {
if c.Name != "" {
m[c.Name] = c.Cmd
}
}
return m
}
type ScriptCmdOut struct {
cell.Out
ScriptCmd ScriptCmd `group:"script-commands"`
}
type ScriptCmdsOut struct {
cell.Out
ScriptCmds []ScriptCmd `group:"script-commands,flatten"`
}
func hiveScriptCmd(h *Hive, log *slog.Logger) script.Cmd {
const defaultTimeout = time.Minute
return script.Command(
script.CmdUsage{
Summary: "manipulate the hive",
Args: "cmd args...",
},
func(s *script.State, args ...string) (script.WaitFunc, error) {
if len(args) < 1 {
return nil, fmt.Errorf("hive cmd args...\n'cmd' is one of: start, stop, jobs")
}
switch args[0] {
case "start":
ctx, cancel := context.WithTimeout(context.Background(), defaultTimeout)
defer cancel()
return nil, h.Start(log, ctx)
case "stop":
ctx, cancel := context.WithTimeout(context.Background(), defaultTimeout)
defer cancel()
return nil, h.Stop(log, ctx)
}
return nil, fmt.Errorf("unknown hive command %q, expected one of: start, stop, jobs", args[0])
},
)
}
func RunRepl(h *Hive, in *os.File, out *os.File, prompt string) {
// Try to set the input into raw mode.
restore, err := script.MakeRaw(int(in.Fd()))
defer restore()
inout := struct {
io.Reader
io.Writer
}{in, out}
term := term.NewTerminal(inout, prompt)
log := slog.New(slog.NewTextHandler(term, nil))
cmds, err := h.ScriptCommands(log)
if err != nil {
log.Error("ScriptCommands()", "error", err)
return
}
for name, cmd := range script.DefaultCmds() {
cmds[name] = cmd
}
e := script.Engine{
Cmds: cmds,
Conds: nil,
}
stop := make(chan struct{})
defer close(stop)
sigs := make(chan os.Signal, 1)
defer signal.Stop(sigs)
signal.Notify(sigs, os.Interrupt)
newState := func() *script.State {
ctx, cancel := context.WithCancel(context.Background())
s, err := script.NewState(ctx, "/tmp", nil)
if err != nil {
panic(err)
}
go func() {
select {
case <-stop:
cancel()
case <-sigs:
cancel()
}
}()
return s
}
s := newState()
for {
line, err := term.ReadLine()
if err != nil {
if errors.Is(err, io.EOF) {
return
} else {
panic(err)
}
}
err = e.ExecuteLine(s, line, term)
if err != nil {
fmt.Fprintln(term, err.Error())
}
if s.Context().Err() != nil {
// Context was cancelled due to interrupt. Re-create the state
// to run more commands.
s = newState()
fmt.Fprintln(term, "^C (interrupted)")
}
}
}