-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.go
70 lines (62 loc) · 1.5 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
package main
import (
"flag"
"fmt"
"log"
"log/slog"
"strings"
"fyne.io/fyne/v2/app"
"github.com/ErikKalkoken/janice/internal/ui"
)
const (
appID = "io.github.erikkalkoken.janice"
)
type logLevelFlag struct {
value slog.Level
}
func (l logLevelFlag) String() string {
return l.value.String()
}
func (l *logLevelFlag) Set(value string) error {
m := map[string]slog.Level{
"DEBUG": slog.LevelDebug,
"INFO": slog.LevelInfo,
"WARN": slog.LevelWarn,
"ERROR": slog.LevelError,
}
v, ok := m[strings.ToUpper(value)]
if !ok {
return fmt.Errorf("unknown log level")
}
l.value = v
return nil
}
func main() {
levelFlag := logLevelFlag{value: slog.LevelWarn}
flag.Var(&levelFlag, "loglevel", "set log level")
versionFlag := flag.Bool("v", false, "show current version")
flag.Usage = myUsage
flag.Parse()
log.SetFlags(log.LstdFlags | log.Llongfile)
slog.SetLogLoggerLevel(levelFlag.value)
a := app.NewWithID(appID)
if *versionFlag {
fmt.Printf("Current version is: %s", a.Metadata().Version)
return
}
u, err := ui.NewUI(a)
if err != nil {
log.Fatalf("Failed to initialize application: %s", err)
}
source := flag.Arg(0)
u.ShowAndRun(source)
}
// myUsage writes a custom usage message to configured output stream.
func myUsage() {
s := "Usage: janice [options] [<inputfile>]\n\n" +
"A desktop app for viewing large JSON files.\n" +
"For more information please see: https://github.com/ErikKalkoken/janice\n\n" +
"Options:\n"
fmt.Fprint(flag.CommandLine.Output(), s)
flag.PrintDefaults()
}