mirror of
https://github.com/wailsapp/wails.git
synced 2025-05-03 12:01:42 +08:00
75 lines
1.4 KiB
Go
75 lines
1.4 KiB
Go
package process
|
|
|
|
import (
|
|
"os"
|
|
"os/exec"
|
|
|
|
"github.com/wailsapp/wails/v2/pkg/clilogger"
|
|
)
|
|
|
|
// Process defines a process that can be executed
|
|
type Process struct {
|
|
logger *clilogger.CLILogger
|
|
cmd *exec.Cmd
|
|
exitChannel chan bool
|
|
Running bool
|
|
}
|
|
|
|
// NewProcess creates a new process struct
|
|
func NewProcess(logger *clilogger.CLILogger, cmd string, args ...string) *Process {
|
|
result := &Process{
|
|
logger: logger,
|
|
cmd: exec.Command(cmd, args...),
|
|
exitChannel: make(chan bool, 1),
|
|
}
|
|
result.cmd.Stdout = os.Stdout
|
|
result.cmd.Stderr = os.Stderr
|
|
return result
|
|
}
|
|
|
|
// Start the process
|
|
func (p *Process) Start() error {
|
|
|
|
err := p.cmd.Start()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
p.Running = true
|
|
|
|
go func(cmd *exec.Cmd, running *bool, logger *clilogger.CLILogger, exitChannel chan bool) {
|
|
logger.Println("Starting process (PID: %d)", cmd.Process.Pid)
|
|
_ = cmd.Wait()
|
|
logger.Println("Exiting process (PID: %d)", cmd.Process.Pid)
|
|
*running = false
|
|
exitChannel <- true
|
|
}(p.cmd, &p.Running, p.logger, p.exitChannel)
|
|
|
|
return nil
|
|
}
|
|
|
|
// Kill the process
|
|
func (p *Process) Kill() error {
|
|
if !p.Running {
|
|
return nil
|
|
}
|
|
err := p.cmd.Process.Kill()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
err = p.cmd.Process.Release()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// Wait for command to exit properly
|
|
<-p.exitChannel
|
|
|
|
return err
|
|
}
|
|
|
|
// PID returns the process PID
|
|
func (p *Process) PID() int {
|
|
return p.cmd.Process.Pid
|
|
}
|