Created
September 8, 2022 23:44
-
-
Save yozel/bef38a9554df2fdb071da46379cc01b6 to your computer and use it in GitHub Desktop.
wrapper
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"flag" | |
"io" | |
"log" | |
"os" | |
"os/exec" | |
"os/signal" | |
) | |
func main() { | |
logPath := flag.String("log", "", "path to log file") | |
flag.Parse() | |
if *logPath == "" { | |
log.Fatal("log path is required") | |
} | |
args := flag.Args() | |
process := exec.Command(args[0], args[1:]...) | |
process.Stdin = os.Stdin | |
process.Stdout = os.Stdout | |
process.Stderr = os.Stderr | |
process.Env = os.Environ() | |
logFile, err := os.OpenFile(*logPath, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0644) | |
if err != nil { | |
log.Fatalf("failed to open log file: %v", err) | |
} | |
defer logFile.Close() | |
process.Stdout = io.MultiWriter(process.Stdout, logFile) | |
process.Stderr = io.MultiWriter(process.Stderr, logFile) | |
sigs := make(chan os.Signal, 1) | |
signal.Notify(sigs) | |
go func() { | |
process.Process.Signal(<-sigs) | |
}() | |
err = process.Run() | |
if err != nil && err.(*exec.ExitError) == nil { | |
log.Fatalf("failed to run command: %v", err) | |
} | |
os.Exit(process.ProcessState.ExitCode()) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment