-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
86 lines (71 loc) · 2.04 KB
/
main.go
File metadata and controls
86 lines (71 loc) · 2.04 KB
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
package main
import (
"SettingsSentry/interfaces"
"SettingsSentry/logger"
"SettingsSentry/pkg/backup"
"SettingsSentry/pkg/command"
"SettingsSentry/pkg/config"
"SettingsSentry/pkg/printer"
"SettingsSentry/pkg/util"
"embed"
"flag"
"fmt"
"os"
"runtime/debug"
)
//go:embed configs/*.cfg
var embeddedConfigsFiles embed.FS
var (
Version string = "1.2.0"
)
func main() {
if err := run(os.Args[1:]); err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}
}
// run is the main application logic that returns an error instead of calling os.Exit
func run(args []string) error {
// Parse logfile flag first
logFilePath := flag.String("logfile", "", "Optional: Path to log file. If provided, logs will be written to this file in addition to console output")
flag.Parse()
// Initialize logger
appLogger, err := logger.NewLogger(*logFilePath)
if err != nil {
return fmt.Errorf("error initializing logger: %w", err)
}
// Set up panic recovery
defer func() {
if r := recover(); r != nil {
appLogger.Logf("Panic recovered in run: %v\nStack trace: %s", r, string(debug.Stack()))
}
}()
appLogger.Logf("SettingsSentry v%s", Version)
// Initialize file system and command executor
osFs := interfaces.NewOsFileSystem()
osCmdExecutor := interfaces.NewOsCommandExecutor()
// Initialize global utilities
util.InitGlobals(appLogger, osFs, osCmdExecutor, false)
// Set package-level dependencies
config.AppLogger = appLogger
config.Fs = util.Fs
backup.AppLogger = appLogger
backup.Fs = util.Fs
command.CmdExecutor = util.CmdExecutor
printer.AppLogger = util.AppLogger
// Create CLI instance
cli := NewCLI(appLogger, osFs, osCmdExecutor, embeddedConfigsFiles, Version)
// Check for help flags
if len(args) == 0 || (len(args) == 1 && (args[0] == "-h" || args[0] == "--help")) {
cli.ShowHelp()
return nil
}
// Parse flags and get action
action, flags, err := cli.ParseFlags(args)
if err != nil {
cli.ShowHelp()
return fmt.Errorf("flag parsing error: %w", err)
}
// Execute the action
return cli.ExecuteAction(action, flags)
}