/
proc-starter.go
83 lines (75 loc) · 1.87 KB
/
proc-starter.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
package tui
import (
"context"
"fmt"
"github.com/f1bonacc1/process-compose/src/command"
"github.com/f1bonacc1/process-compose/src/types"
"github.com/rs/zerolog/log"
"os"
"os/exec"
"os/signal"
"syscall"
)
func (pv *pcView) startProcess() {
name := pv.getSelectedProcName()
info, err := pv.project.GetProcessInfo(name)
if err != nil {
pv.showError(err.Error())
return
}
if info.IsForeground {
pv.runForeground(info)
return
}
err = pv.project.StartProcess(name)
if err != nil {
pv.showError(err.Error())
}
}
func (pv *pcView) runShellProcess() {
shell := command.DefaultShellConfig()
shellCmd := &types.ProcessConfig{
Executable: shell.ShellCommand,
RestartPolicy: types.RestartPolicyConfig{
Restart: types.RestartPolicyNo,
},
}
pv.runForeground(shellCmd)
}
func (pv *pcView) runForeground(info *types.ProcessConfig) bool {
pv.halt()
defer pv.resume()
return pv.appView.Suspend(func() {
err := pv.execute(info)
if err != nil {
log.Err(err).Msgf("Command failed")
}
})
}
func (pv *pcView) execute(info *types.ProcessConfig) error {
ctx, cancel := context.WithCancel(context.Background())
defer func() {
cancel()
//clearScreen()
}()
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, os.Interrupt, syscall.SIGTERM)
go func(cancel context.CancelFunc) {
select {
case sig := <-sigChan:
log.Debug().Msgf("Command canceled with signal %#v", sig)
cancel()
case <-ctx.Done():
log.Debug().Msgf("Foreground process context canceled")
}
}(cancel)
cmd := exec.CommandContext(ctx, info.Executable, info.Args...)
log.Debug().Str("exec", info.Executable).Strs("args", info.Args).Msg("running start")
cmd.Stdin, cmd.Stdout, cmd.Stderr = os.Stdin, os.Stdout, os.Stderr
err := cmd.Run()
log.Debug().Str("exec", info.Executable).Strs("args", info.Args).Msg("running end")
return err
}
func clearScreen() {
fmt.Print("\033[H\033[2J")
}