github.com/annwntech/go-micro/v2@v2.9.5/runtime/local/process/os/os_windows.go (about) 1 // Package os runs processes locally 2 package os 3 4 import ( 5 "fmt" 6 "os" 7 "os/exec" 8 "strconv" 9 10 "github.com/annwntech/go-micro/v2/runtime/local/process" 11 ) 12 13 func (p *Process) Exec(exe *process.Executable) error { 14 cmd := exec.Command(exe.Package.Path) 15 return cmd.Run() 16 } 17 18 func (p *Process) Fork(exe *process.Executable) (*process.PID, error) { 19 // create command 20 cmd := exec.Command(exe.Package.Path, exe.Args...) 21 // set env vars 22 cmd.Env = append(cmd.Env, exe.Env...) 23 24 in, err := cmd.StdinPipe() 25 if err != nil { 26 return nil, err 27 } 28 out, err := cmd.StdoutPipe() 29 if err != nil { 30 return nil, err 31 } 32 er, err := cmd.StderrPipe() 33 if err != nil { 34 return nil, err 35 } 36 37 // start the process 38 if err := cmd.Start(); err != nil { 39 return nil, err 40 } 41 42 return &process.PID{ 43 ID: fmt.Sprintf("%d", cmd.Process.Pid), 44 Input: in, 45 Output: out, 46 Error: er, 47 }, nil 48 } 49 50 func (p *Process) Kill(pid *process.PID) error { 51 id, err := strconv.Atoi(pid.ID) 52 if err != nil { 53 return err 54 } 55 56 pr, err := os.FindProcess(id) 57 if err != nil { 58 return err 59 } 60 61 // now kill it 62 err = pr.Kill() 63 64 // return the kill error 65 return err 66 } 67 68 func (p *Process) Wait(pid *process.PID) error { 69 id, err := strconv.Atoi(pid.ID) 70 if err != nil { 71 return err 72 } 73 74 pr, err := os.FindProcess(id) 75 if err != nil { 76 return err 77 } 78 79 ps, err := pr.Wait() 80 if err != nil { 81 return err 82 } 83 84 if ps.Success() { 85 return nil 86 } 87 88 return fmt.Errorf(ps.String()) 89 }