-
Notifications
You must be signed in to change notification settings - Fork 118
Expand file tree
/
Copy pathsetup.go
More file actions
50 lines (42 loc) · 1.16 KB
/
setup.go
File metadata and controls
50 lines (42 loc) · 1.16 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
package httpapi
import (
"context"
"fmt"
"os"
"os/signal"
"strings"
"syscall"
"time"
"github.com/coder/agentapi/lib/logctx"
"github.com/coder/agentapi/lib/termexec"
)
type SetupProcessConfig struct {
Program string
ProgramArgs []string
TerminalWidth uint16
TerminalHeight uint16
}
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,
})
if err != nil {
logger.Error(fmt.Sprintf("Error starting process: %v", err))
os.Exit(1)
}
// 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
}