forked from giwty/switch-library-manager
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
88 lines (71 loc) · 2.02 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
package main
import (
"fmt"
"net/url"
"os"
"path/filepath"
"runtime"
"strings"
"github.com/ordovice/switch-library-manager/settings"
"go.uber.org/zap"
)
func main() {
exePath, err := os.Executable()
if err != nil {
fmt.Println("failed to get executable directory, please ensure app has sufficient permissions. aborting")
return
}
workingFolder := filepath.Dir(exePath)
if runtime.GOOS == "darwin" {
if strings.Contains(workingFolder, ".app") {
appIndex := strings.Index(workingFolder, ".app")
sepIndex := strings.LastIndex(workingFolder[:appIndex], string(os.PathSeparator))
workingFolder = workingFolder[:sepIndex]
}
}
appSettings := settings.ReadSettings(workingFolder)
logger := createLogger(workingFolder, appSettings.Debug)
defer logger.Sync() // flushes buffer, if any
sugar := logger.Sugar()
sugar.Info("[SLM starts]")
sugar.Infof("[Executable: %v]", exePath)
sugar.Infof("[Working directory: %v]", workingFolder)
//appSettings.GUI = true
files, err := AssetDir(workingFolder)
if files == nil && err == nil {
appSettings.GUI = false
}
if appSettings.GUI {
CreateGUI(workingFolder, sugar).Start()
} else {
CreateConsole(workingFolder, sugar).Start()
}
}
func createLogger(workingFolder string, debug bool) *zap.Logger {
var config zap.Config
if debug {
config = zap.NewDevelopmentConfig()
} else {
config = zap.NewDevelopmentConfig()
config.Level = zap.NewAtomicLevelAt(zap.InfoLevel)
}
logPath := filepath.Join(workingFolder, "slm.log")
// delete old file
os.Remove(logPath)
if runtime.GOOS == "windows" {
zap.RegisterSink("winfile", func(u *url.URL) (zap.Sink, error) {
// Remove leading slash left by url.Parse()
return os.OpenFile(u.Path[1:], os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0644)
})
logPath = "winfile:///" + logPath
}
config.OutputPaths = []string{logPath}
config.ErrorOutputPaths = []string{logPath}
logger, err := config.Build()
if err != nil {
fmt.Printf("failed to create logger - %v", err)
panic(1)
}
zap.ReplaceGlobals(logger)
return logger
}