forked from SoMuchForSubtlety/f1viewer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.go
More file actions
156 lines (143 loc) · 3.99 KB
/
config.go
File metadata and controls
156 lines (143 loc) · 3.99 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
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"os"
"runtime"
"time"
)
type config struct {
LiveRetryTimeout int `json:"live_retry_timeout"`
Lang string `json:"preferred_language"`
CheckUpdate bool `json:"check_updates"`
SaveLogs bool `json:"save_logs"`
LogLocation string `json:"log_location"`
CustomPlaybackOptions []command `json:"custom_playback_options"`
MultiCommand []multiCommand `json:"multi_commands"`
HorizontalLayout bool `json:"horizontal_layout"`
Theme theme `json:"theme"`
TreeRatio int `json:"tree_ratio"`
OutputRatio int `json:"output_ratio"`
}
type theme struct {
BackgroundColor string `json:"background_color"`
BorderColor string `json:"border_color"`
CategoryNodeColor string `json:"category_node_color"`
FolderNodeColor string `json:"folder_node_color"`
ItemNodeColor string `json:"item_node_color"`
ActionNodeColor string `json:"action_node_color"`
LoadingColor string `json:"loading_color"`
LiveColor string `json:"live_color"`
UpdateColor string `json:"update_color"`
NoContentColor string `json:"no_content_color"`
InfoColor string `json:"info_color"`
ErrorColor string `json:"error_color"`
TerminalAccentColor string `json:"terminal_accent_color"`
TerminalTextColor string `json:"terminal_text_color"`
MultiCommandColor string `json:"multi_command_color"`
}
func loadConfig() (config, error) {
var cfg config
path, err := getConfigPath()
if err != nil {
return cfg, err
}
if _, err = os.Stat(path + "config.json"); os.IsNotExist(err) {
cfg.LiveRetryTimeout = 60
cfg.Lang = "en"
cfg.CheckUpdate = true
cfg.SaveLogs = true
cfg.TreeRatio = 1
cfg.OutputRatio = 1
err = cfg.save()
return cfg, err
}
var data []byte
data, err = ioutil.ReadFile(path + "config.json")
if err != nil {
return cfg, err
}
err = json.Unmarshal(data, &cfg)
if cfg.TreeRatio < 1 {
cfg.TreeRatio = 1
}
if cfg.OutputRatio < 1 {
cfg.OutputRatio = 1
}
cfg.Theme.apply()
return cfg, err
}
func getConfigPath() (string, error) {
path, err := os.UserConfigDir()
if err != nil {
return "", err
}
path += string(os.PathSeparator) + "f1viewer" + string(os.PathSeparator)
_, err = os.Stat(path)
if os.IsNotExist(err) {
err = os.MkdirAll(path, os.ModePerm)
}
return path, err
}
func getLogPath(cfg config) (string, error) {
var path string
if cfg.LogLocation == "" {
// windows, macos
switch runtime.GOOS {
case "windows", "darwin":
configPath, err := getConfigPath()
if err != nil {
return "", err
}
path = configPath + string(os.PathSeparator) + "logs" + string(os.PathSeparator)
default:
// linux, etc.
home, err := os.UserHomeDir()
if err != nil {
return "", err
}
path = home + "/.local/share/f1viewer/"
}
} else {
path = cfg.LogLocation
}
_, err := os.Stat(path)
if os.IsNotExist(err) {
err = os.MkdirAll(path, os.ModePerm)
}
return path, err
}
func (cfg config) save() error {
path, err := getConfigPath()
if err != nil {
return err
}
data, err := json.MarshalIndent(&cfg, "", "\t")
if err != nil {
return fmt.Errorf("error marshaling config: %v", err)
}
err = ioutil.WriteFile(path+"config.json", data, os.ModePerm)
if err != nil {
return fmt.Errorf("error saving config: %v", err)
}
return nil
}
func configureLogging(cfg config) (*os.File, error) {
if !cfg.SaveLogs {
log.SetOutput(ioutil.Discard)
return nil, nil
}
logPath, err := getLogPath(cfg)
if err != nil {
return nil, fmt.Errorf("Could not get log path: %w", err)
}
completePath := fmt.Sprint(logPath, time.Now().Format("2006-01-02"), ".log")
logFile, err := os.OpenFile(completePath, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
return nil, fmt.Errorf("Could not open log file: %w", err)
}
log.SetOutput(logFile)
return logFile, nil
}