go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/common/system/exitcode/exitcode.go (about)

     1  // Copyright 2015 The LUCI Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  // Package exitcode provides common methods to extract exit codes from errors
    16  // returned by exec.Cmd.
    17  package exitcode
    18  
    19  import (
    20  	"os/exec"
    21  	"syscall"
    22  
    23  	"go.chromium.org/luci/common/errors"
    24  )
    25  
    26  // Get returns the process process exit return code given an error returned by
    27  // exec.Cmd's Wait or Run methods. If no exit code is present, Get will return
    28  // false.
    29  func Get(err error) (int, bool) {
    30  	err = errors.Unwrap(err)
    31  	if err == nil {
    32  		return 0, true
    33  	}
    34  
    35  	if ee, ok := err.(*exec.ExitError); ok {
    36  		return ee.Sys().(syscall.WaitStatus).ExitStatus(), true
    37  	}
    38  	return 0, false
    39  }