-
Notifications
You must be signed in to change notification settings - Fork 16
/
utils.go
90 lines (75 loc) · 2.27 KB
/
utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package command
import (
"bufio"
"fmt"
"os"
"os/signal"
"strings"
"time"
"net/url"
"github.com/cloudfoundry-incubator/bosh-backup-and-restore/factory"
"github.com/cloudfoundry-incubator/bosh-backup-and-restore/orchestrator"
"github.com/urfave/cli"
)
const defaultLogfilePermissions = 0644
func trapSigint(backup bool) {
sigintChan := make(chan os.Signal, 1)
signal.Notify(sigintChan, os.Interrupt)
var sigintQuestion, stdInErrorMessage, cleanupAdvisedNotice string
if backup {
sigintQuestion = backupSigintQuestion
stdInErrorMessage = backupStdinErrorMessage
cleanupAdvisedNotice = backupCleanupAdvisedNotice
} else {
sigintQuestion = restoreSigintQuestion
stdInErrorMessage = restoreStdinErrorMessage
cleanupAdvisedNotice = restoreCleanupAdvisedNotice
}
go func() {
for range sigintChan {
stdinReader := bufio.NewReader(os.Stdin)
factory.ApplicationLoggerStdout.Pause()
factory.ApplicationLoggerStderr.Pause()
fmt.Fprintln(os.Stdout, "\n"+sigintQuestion)
input, err := stdinReader.ReadString('\n')
if err != nil {
fmt.Println("\n" + stdInErrorMessage)
} else if strings.ToLower(strings.TrimSpace(input)) == "yes" {
fmt.Println(cleanupAdvisedNotice)
os.Exit(1)
}
factory.ApplicationLoggerStdout.Resume()
factory.ApplicationLoggerStderr.Resume()
}
}()
}
func processError(err orchestrator.Error) error {
return processErrorWithFooter(err, "")
}
func processErrorWithFooter(err orchestrator.Error, footer string) error {
errorCode := orchestrator.BuildExitCode(err)
errorMessage := err.Error()
errorWithStackTrace := err.PrettyError(true)
writeErr := writeStackTrace(errorWithStackTrace)
if writeErr != nil {
errorMessage = errorWithStackTrace
}
errorMessage = errorMessage + "\n" + footer
return cli.NewExitError(errorMessage, errorCode)
}
func writeStackTrace(errorWithStackTrace string) error {
if errorWithStackTrace != "" {
err := os.WriteFile(fmt.Sprintf("bbr-%s.err.log", time.Now().UTC().Format(time.RFC3339)), []byte(errorWithStackTrace), defaultLogfilePermissions)
if err != nil {
return err
}
}
return nil
}
func extractNameFromAddress(address string) string {
url, err := url.Parse(address)
if err == nil && url.Hostname() != "" {
address = url.Hostname()
}
return strings.Split(address, ":")[0]
}