fix error messaging in wrappedmain. Stderr gets eaten by panicwrap, so we need to write to stdout, which then gets unpacked into error and output messages using the ErrorPrefix and OutputPrefix (#10766)

This commit is contained in:
Megan Marsh 2021-03-16 03:21:00 -07:00 committed by GitHub
parent 60c961c021
commit 64a3219f69
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 8 additions and 8 deletions

16
main.go
View File

@ -152,7 +152,7 @@ func wrappedMain() int {
// passed into commands like `packer build` // passed into commands like `packer build`
config, err := loadConfig() config, err := loadConfig()
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, "Error loading configuration: \n\n%s\n", err) fmt.Fprintf(os.Stdout, "%s Error loading configuration: \n\n%s\n", ErrorPrefix, err)
return 1 return 1
} }
@ -166,7 +166,7 @@ func wrappedMain() int {
cacheDir, err := packersdk.CachePath() cacheDir, err := packersdk.CachePath()
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, "Error preparing cache directory: \n\n%s\n", err) fmt.Fprintf(os.Stdout, "%s Error preparing cache directory: \n\n%s\n", ErrorPrefix, err)
return 1 return 1
} }
log.Printf("[INFO] Setting cache directory: %s", cacheDir) log.Printf("[INFO] Setting cache directory: %s", cacheDir)
@ -187,7 +187,7 @@ func wrappedMain() int {
// Set this so that we don't get colored output in our machine- // Set this so that we don't get colored output in our machine-
// readable UI. // readable UI.
if err := os.Setenv("PACKER_NO_COLOR", "1"); err != nil { if err := os.Setenv("PACKER_NO_COLOR", "1"); err != nil {
fmt.Fprintf(os.Stderr, "Packer failed to initialize UI: %s\n", err) fmt.Fprintf(os.Stdout, "%s Packer failed to initialize UI: %s\n", ErrorPrefix, err)
return 1 return 1
} }
} else { } else {
@ -202,13 +202,13 @@ func wrappedMain() int {
currentPID := os.Getpid() currentPID := os.Getpid()
backgrounded, err := checkProcess(currentPID) backgrounded, err := checkProcess(currentPID)
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, "cannot determine if process is in "+ fmt.Fprintf(os.Stdout, "%s cannot determine if process is in "+
"background: %s\n", err) "background: %s\n", ErrorPrefix, err)
} }
if backgrounded { if backgrounded {
fmt.Fprint(os.Stderr, "Running in background, not using a TTY\n") fmt.Fprintf(os.Stdout, "%s Running in background, not using a TTY\n", ErrorPrefix)
} else if TTY, err := openTTY(); err != nil { } else if TTY, err := openTTY(); err != nil {
fmt.Fprintf(os.Stderr, "No tty available: %s\n", err) fmt.Fprintf(os.Stdout, "%s No tty available: %s\n", ErrorPrefix, err)
} else { } else {
basicUi.TTY = TTY basicUi.TTY = TTY
basicUi.PB = &packer.UiProgressBar{} basicUi.PB = &packer.UiProgressBar{}
@ -246,7 +246,7 @@ func wrappedMain() int {
} }
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, "Error executing CLI: %s\n", err) fmt.Fprintf(os.Stdout, "%s Error executing CLI: %s\n", ErrorPrefix, err)
return 1 return 1
} }