-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
118 lines (100 loc) · 3.3 KB
/
main.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/******************************************************************************\
* Copyright (C) 2024-2024 The Molibackup Authors. All rights reserved. *
* Licensed under the Apache version 2.0 License *
* Homepage: https://github.com/fdupoux/molibackup *
\******************************************************************************/
package main
import (
_ "embed"
"flag"
"fmt"
"os"
"runtime"
"sort"
"strings"
"github.com/gookit/slog"
)
//go:embed VERSION
var progversion string
const (
ExitStatusSuccessfulExecution = 0
ExitStatusInvalidConfiguration = 1
ExitStatusFailedToExecuteJobs = 2
)
func main() {
var jobnames []string
errcount := 0
jobcount := 0
version := strings.TrimSpace(progversion)
// Process options specified on the command line
configfile := flag.String("c", "", "path to the yaml configuration file")
showversion := flag.Bool("v", false, "show program version and exit")
flag.Parse()
// Show version number if requested
if *showversion {
fmt.Printf("molibackup version %s built with %s\n", version, runtime.Version())
os.Exit(ExitStatusSuccessfulExecution)
}
// Initialise the logging library
logfmtTemplateShort := "[{{datetime}}] [{{level}}] {{message}} {{data}} {{extra}}\n"
logfmtTemplateDebug := "[{{datetime}}] [{{level}}] [{{caller}}] {{message}} {{data}} {{extra}}\n"
logfmt := slog.NewTextFormatter()
logfmt.SetTemplate(logfmtTemplateShort)
logfmt.EnableColor = true
slog.SetFormatter(logfmt)
slog.SetLogLevel(slog.InfoLevel)
// Print version number
slog.Infof("molibackup version %s built with %s starting ...", version, runtime.Version())
// Read the configuration file
err := readConfiguration(*configfile)
if err != nil {
slog.Errorf("Failed to read configuration: %v", err)
os.Exit(ExitStatusInvalidConfiguration)
}
// Configure the logging library
loglevel := progconfig.Global["loglevel"]
switch loglevel {
case "error":
slog.SetLogLevel(slog.ErrorLevel)
logfmt.SetTemplate(logfmtTemplateShort)
case "warn":
slog.SetLogLevel(slog.WarnLevel)
logfmt.SetTemplate(logfmtTemplateShort)
case "info":
slog.SetLogLevel(slog.InfoLevel)
logfmt.SetTemplate(logfmtTemplateShort)
case "debug":
slog.SetLogLevel(slog.DebugLevel)
logfmt.SetTemplate(logfmtTemplateDebug)
default:
slog.Errorf("Invalid loglevel in configuration: \"%s\"", loglevel)
os.Exit(ExitStatusInvalidConfiguration)
}
// Create list of jobs sorted alphabetically
for jobname := range jobmetadefs {
jobnames = append(jobnames, jobname)
}
sort.Strings(jobnames)
// Execute all jobs defined in the configuration
for _, jobname := range jobnames {
jobconfig := jobmetadefs[jobname]
jobenabled := fmt.Sprintf("%v", jobconfig.Enabled)
if jobenabled != "false" {
slog.Infof("Running job \"%s\" ...", jobname)
err = runJob(jobname)
if err != nil {
errcount++
slog.Errorf("Failed to execute job \"%s\": %v", jobname, err)
}
jobcount++
} else {
slog.Infof("Skipping job \"%s\" as it is disabled in the configuration", jobname)
}
}
if errcount > 0 {
slog.Errorf("Have finished running jobs with %d failures out of %d jobs", errcount, jobcount)
os.Exit(ExitStatusFailedToExecuteJobs)
}
slog.Infof("Have successfully executed %d jobs", jobcount)
os.Exit(ExitStatusSuccessfulExecution)
}