-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
50 lines (40 loc) · 895 Bytes
/
main.go
File metadata and controls
50 lines (40 loc) · 895 Bytes
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
package main
import (
"context"
"errors"
"fmt"
"os"
"os/signal"
"syscall"
"github.com/lupppig/dbackup/cmd"
apperrors "github.com/lupppig/dbackup/internal/errors"
"github.com/lupppig/dbackup/internal/logger"
)
const (
EXIT_SUCCESS = iota
EXIT_FAILURE
)
func main() {
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
defer stop()
if err := cmd.ExecuteContext(ctx); err != nil {
exitOnError(err)
}
os.Exit(EXIT_SUCCESS)
}
func exitOnError(err error) {
if err == nil {
return
}
l := logger.New(logger.Config{})
var appErr *apperrors.AppError
if errors.As(err, &appErr) {
l.Error(fmt.Sprintf("[%s] %s", appErr.Type, appErr.Message), "error", appErr.Err)
if appErr.Hint != "" {
fmt.Fprintf(os.Stderr, "\n💡 HINT: %s\n\n", appErr.Hint)
}
} else {
l.Error("Command failed", "error", err)
}
os.Exit(EXIT_FAILURE)
}