zombiezen.com/go/lua@v0.0.0-20231013005828-290725fb9140/execute_unix.go (about)

     1  // Copyright 2023 Ross Light
     2  //
     3  // Permission is hereby granted, free of charge, to any person obtaining a copy of
     4  // this software and associated documentation files (the “Software”), to deal in
     5  // the Software without restriction, including without limitation the rights to
     6  // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
     7  // the Software, and to permit persons to whom the Software is furnished to do so,
     8  // subject to the following conditions:
     9  //
    10  // The above copyright notice and this permission notice shall be included in all
    11  // copies or substantial portions of the Software.
    12  //
    13  // THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
    15  // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
    16  // COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
    17  // IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
    18  // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
    19  //
    20  // SPDX-License-Identifier: MIT
    21  
    22  //go:build unix
    23  
    24  package lua
    25  
    26  import (
    27  	"errors"
    28  	"os/exec"
    29  	"syscall"
    30  )
    31  
    32  func osCommand(command string) *exec.Cmd {
    33  	return &exec.Cmd{
    34  		Path: "/bin/sh",
    35  		Args: []string{"sh", "-c", command},
    36  	}
    37  }
    38  
    39  func execError(err error) (result string, status int) {
    40  	var e *exec.ExitError
    41  	if !errors.As(err, &e) {
    42  		return "exit", -1
    43  	}
    44  	if w, ok := e.Sys().(syscall.WaitStatus); ok && w.Signaled() {
    45  		return "signal", int(w.Signal())
    46  	}
    47  	return "exit", e.ExitCode()
    48  }
    49  
    50  func hasShell() bool {
    51  	_, err := exec.LookPath("/bin/sh")
    52  	return err == nil
    53  }