-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
115 lines (94 loc) · 2.39 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
package main
import (
"flag"
"fmt"
"log"
"log/slog"
"os"
"path/filepath"
"runtime"
"strings"
"time"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
"github.com/MatusOllah/gecfg-editor/internal/config"
"github.com/MatusOllah/slogcolor"
)
func getLogLevel(s string) slog.Leveler {
switch strings.ToLower(s) {
case "debug":
return slog.LevelDebug
case "info":
return slog.LevelInfo
case "warn":
return slog.LevelWarn
case "error":
return slog.LevelError
default:
return slog.LevelInfo
}
}
func showVersion() {
a := app.New()
fmt.Printf("%s version v%s\n", a.Metadata().Name, a.Metadata().Version)
fmt.Printf("Go version %s\n", runtime.Version())
}
func main() {
path := flag.String("open-file", "", "Open a file")
logLevel := flag.String("log-level", "info", "Log level (\"debug\", \"info\", \"warn\", \"error\")")
version := flag.Bool("version", false, "Show version and exit")
flag.Parse()
slog.SetDefault(slog.New(slogcolor.NewHandler(os.Stderr, &slogcolor.Options{
Level: getLogLevel(*logLevel),
TimeFormat: time.DateTime,
SrcFileMode: slogcolor.ShortFile,
})))
log.SetFlags(log.Ldate | log.Ltime | log.Lmicroseconds | log.Lshortfile)
if *version {
showVersion()
os.Exit(0)
}
slog.Info("Initializing")
beforeInit := time.Now()
a := app.New()
a.Lifecycle().SetOnStarted(func() {
slog.Info("Lifecycle: Started")
})
a.Lifecycle().SetOnStopped(func() {
slog.Info("Lifecycle: Stopped")
})
a.Lifecycle().SetOnEnteredForeground(func() {
slog.Info("Lifecycle: Entered Foreground")
})
a.Lifecycle().SetOnExitedForeground(func() {
slog.Info("Lifecycle: Exited Foreground")
})
slog.Info(fmt.Sprintf("gecfg-editor version %s", a.Metadata().Version))
slog.Info(fmt.Sprintf("Go version %s", runtime.Version()))
reloadListItems()
w := a.NewWindow(openFileName + " - " + a.Metadata().Name)
w.SetMaster()
w.Resize(fyne.NewSize(1280, 720))
w.SetMainMenu(makeMainMenu(a, w))
w.SetContent(makeUI(a, w))
slog.Info(fmt.Sprintf("Initialization took %s", time.Since(beforeInit)))
// open file
if *path != "" {
slog.Info("opening file", "path", *path)
cfg, err := config.Open(*path)
if err != nil {
slog.Error(err.Error())
os.Exit(1)
}
defer cfg.Close()
openFileName = filepath.Base(*path)
openFilePath = *path
theMap = cfg.Data()
reloadListItems()
updateWindowTitle(a, w)
}
w.ShowAndRun()
slog.Info("exiting")
runtime.GC()
os.Exit(0)
}