github.com/stolowski/snapd@v0.0.0-20210407085831-115137ce5a22/osutil/exitcode.go (about) 1 // -*- Mode: Go; indent-tabs-mode: t -*- 2 3 /* 4 * Copyright (C) 2014-2015 Canonical Ltd 5 * 6 * This program is free software: you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License version 3 as 8 * published by the Free Software Foundation. 9 * 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * 15 * You should have received a copy of the GNU General Public License 16 * along with this program. If not, see <http://www.gnu.org/licenses/>. 17 * 18 */ 19 20 package osutil 21 22 import ( 23 "os/exec" 24 "syscall" 25 ) 26 27 // ExitCode extract the exit code from the error of a failed cmd.Run() or the 28 // original error if its not a exec.ExitError 29 func ExitCode(runErr error) (e int, err error) { 30 // TODO: with golang-1.12 this becomes a bit nicer: 31 // https://github.com/golang/go/issues/26539 32 // golang, you are kidding me, right? 33 if exitErr, ok := runErr.(*exec.ExitError); ok { 34 waitStatus := exitErr.Sys().(syscall.WaitStatus) 35 e = waitStatus.ExitStatus() 36 return e, nil 37 } 38 return e, runErr 39 }