-
Notifications
You must be signed in to change notification settings - Fork 116
Expand file tree
/
Copy pathsetup.go
More file actions
61 lines (52 loc) · 1.44 KB
/
setup.go
File metadata and controls
61 lines (52 loc) · 1.44 KB
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
package httpapi
import (
"context"
"fmt"
"os"
"os/signal"
"strings"
"syscall"
"time"
"github.com/coder/agentapi/lib/logctx"
mf "github.com/coder/agentapi/lib/msgfmt"
"github.com/coder/agentapi/lib/termexec"
)
type SetupProcessConfig struct {
Program string
ProgramArgs []string
TerminalWidth uint16
TerminalHeight uint16
AgentType mf.AgentType
}
func SetupProcess(ctx context.Context, config SetupProcessConfig) (*termexec.Process, error) {
logger := logctx.From(ctx)
logger.Info(fmt.Sprintf("Running: %s %s", config.Program, strings.Join(config.ProgramArgs, " ")))
process, err := termexec.StartProcess(ctx, termexec.StartProcessConfig{
Program: config.Program,
Args: config.ProgramArgs,
TerminalWidth: config.TerminalWidth,
TerminalHeight: config.TerminalHeight,
AgentType: config.AgentType,
})
if err != nil {
logger.Error(fmt.Sprintf("Error starting process: %v", err))
os.Exit(1)
}
// Hack for sourcegraph amp to stop the animation.
if config.AgentType == mf.AgentTypeAmp {
_, err = process.Write([]byte(" \b"))
if err != nil {
return nil, err
}
}
// Handle SIGINT (Ctrl+C) and send it to the process
signalCh := make(chan os.Signal, 1)
signal.Notify(signalCh, os.Interrupt, syscall.SIGTERM)
go func() {
<-signalCh
if err := process.Close(logger, 5*time.Second); err != nil {
logger.Error("Error closing process", "error", err)
}
}()
return process, nil
}