mirror of
https://github.com/opentffoundation/opentf.git
synced 2025-12-25 01:00:16 -05:00
Setup panicwrap
This commit is contained in:
76
main.go
76
main.go
@@ -6,7 +6,9 @@ import (
|
||||
"log"
|
||||
"os"
|
||||
|
||||
"github.com/ActiveState/tail"
|
||||
"github.com/mitchellh/cli"
|
||||
"github.com/mitchellh/panicwrap"
|
||||
)
|
||||
|
||||
func main() {
|
||||
@@ -14,7 +16,79 @@ func main() {
|
||||
}
|
||||
|
||||
func realMain() int {
|
||||
log.SetOutput(ioutil.Discard)
|
||||
var wrapConfig panicwrap.WrapConfig
|
||||
|
||||
if !panicwrap.Wrapped(&wrapConfig) {
|
||||
// Determine where logs should go in general (requested by the user)
|
||||
logWriter, err := logOutput()
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Couldn't setup log output: %s", err)
|
||||
return 1
|
||||
}
|
||||
|
||||
// We always send logs to a temporary file that we use in case
|
||||
// there is a panic. Otherwise, we delete it.
|
||||
logTempFile, err := ioutil.TempFile("", "terraform-log")
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Couldn't setup logging tempfile: %s", err)
|
||||
return 1
|
||||
}
|
||||
logTempFile.Close()
|
||||
defer os.Remove(logTempFile.Name())
|
||||
|
||||
// Tell the logger to log to this file
|
||||
os.Setenv(EnvLog, "1")
|
||||
os.Setenv(EnvLogFile, logTempFile.Name())
|
||||
|
||||
if logWriter != nil {
|
||||
// Start tailing the file beforehand to get the data
|
||||
t, err := tail.TailFile(logTempFile.Name(), tail.Config{
|
||||
Follow: true,
|
||||
Logger: tail.DiscardingLogger,
|
||||
MustExist: true,
|
||||
})
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Couldn't setup logging tempfile: %s", err)
|
||||
return 1
|
||||
}
|
||||
go func() {
|
||||
for line := range t.Lines {
|
||||
logWriter.Write([]byte(line.Text + "\n"))
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
// Create the configuration for panicwrap and wrap our executable
|
||||
wrapConfig.Handler = panicHandler(logTempFile)
|
||||
exitStatus, err := panicwrap.Wrap(&wrapConfig)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Couldn't start Terraform: %s", err)
|
||||
return 1
|
||||
}
|
||||
|
||||
// If >= 0, we're the parent, so just exit
|
||||
if exitStatus >= 0 {
|
||||
return exitStatus
|
||||
}
|
||||
|
||||
// We're the child, so just close the tempfile we made in order to
|
||||
// save file handles since the tempfile is only used by the parent.
|
||||
logTempFile.Close()
|
||||
}
|
||||
|
||||
// Call the real main
|
||||
return wrappedMain()
|
||||
}
|
||||
|
||||
func wrappedMain() int {
|
||||
logOutput, err := logOutput()
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Error starting Terraform: %s", err)
|
||||
return 1
|
||||
}
|
||||
if logOutput != nil {
|
||||
log.SetOutput(logOutput)
|
||||
}
|
||||
|
||||
// Get the command line args. We shortcut "--version" and "-v" to
|
||||
// just show the version.
|
||||
|
||||
Reference in New Issue
Block a user