github.com/tickoalcantara12/micro/v3@v3.0.0-20221007104245-9d75b9bcbab9/service/runtime/local/process/os/os_windows.go (about)

     1  // Licensed under the Apache License, Version 2.0 (the "License");
     2  // you may not use this file except in compliance with the License.
     3  // You may obtain a copy of the License at
     4  //
     5  //     https://www.apache.org/licenses/LICENSE-2.0
     6  //
     7  // Unless required by applicable law or agreed to in writing, software
     8  // distributed under the License is distributed on an "AS IS" BASIS,
     9  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    10  // See the License for the specific language governing permissions and
    11  // limitations under the License.
    12  //
    13  // Original source: github.com/micro/go-micro/v3/runtime/local/process/os/os_windows.go
    14  
    15  // Package os runs processes locally
    16  package os
    17  
    18  import (
    19  	"fmt"
    20  	"os"
    21  	"os/exec"
    22  	"strconv"
    23  
    24  	"github.com/tickoalcantara12/micro/v3/service/runtime/local/process"
    25  )
    26  
    27  func (p *Process) Exec(exe *process.Binary) error {
    28  	cmd := exec.Command(exe.Package.Path)
    29  	return cmd.Run()
    30  }
    31  
    32  func (p *Process) Fork(exe *process.Binary) (*process.PID, error) {
    33  	// create command
    34  	cmd := exec.Command(exe.Package.Path, exe.Args...)
    35  	// set env vars
    36  	cmd.Env = append(cmd.Env, exe.Env...)
    37  
    38  	in, err := cmd.StdinPipe()
    39  	if err != nil {
    40  		return nil, err
    41  	}
    42  	out, err := cmd.StdoutPipe()
    43  	if err != nil {
    44  		return nil, err
    45  	}
    46  	er, err := cmd.StderrPipe()
    47  	if err != nil {
    48  		return nil, err
    49  	}
    50  
    51  	// start the process
    52  	if err := cmd.Start(); err != nil {
    53  		return nil, err
    54  	}
    55  
    56  	return &process.PID{
    57  		ID:     fmt.Sprintf("%d", cmd.Process.Pid),
    58  		Input:  in,
    59  		Output: out,
    60  		Error:  er,
    61  	}, nil
    62  }
    63  
    64  func (p *Process) Kill(pid *process.PID) error {
    65  	id, err := strconv.Atoi(pid.ID)
    66  	if err != nil {
    67  		return err
    68  	}
    69  
    70  	pr, err := os.FindProcess(id)
    71  	if err != nil {
    72  		return err
    73  	}
    74  
    75  	// now kill it
    76  	err = pr.Kill()
    77  
    78  	// return the kill error
    79  	return err
    80  }
    81  
    82  func (p *Process) Wait(pid *process.PID) error {
    83  	id, err := strconv.Atoi(pid.ID)
    84  	if err != nil {
    85  		return err
    86  	}
    87  
    88  	pr, err := os.FindProcess(id)
    89  	if err != nil {
    90  		return err
    91  	}
    92  
    93  	ps, err := pr.Wait()
    94  	if err != nil {
    95  		return err
    96  	}
    97  
    98  	if ps.Success() {
    99  		return nil
   100  	}
   101  
   102  	return fmt.Errorf(ps.String())
   103  }