-
Notifications
You must be signed in to change notification settings - Fork 29
/
config.go
198 lines (163 loc) · 5.16 KB
/
config.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
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package config
import (
"errors"
"fmt"
"os"
"path"
"path/filepath"
"gopkg.in/yaml.v3"
)
// AppDir is the name of the directory where the config file is stored.
const AppDir = "fm"
// ConfigFileName is the name of the config file that gets created.
const ConfigFileName = "config.yml"
// SyntaxThemeConfig represents light and dark syntax themes.
type SyntaxThemeConfig struct {
Light string `yaml:"light"`
Dark string `yaml:"dark"`
}
// SettingsConfig struct represents the config for the settings.
type SettingsConfig struct {
StartDir string `yaml:"start_dir"`
ShowIcons bool `yaml:"show_icons"`
EnableLogging bool `yaml:"enable_logging"`
PrettyMarkdown bool `yaml:"pretty_markdown"`
Borderless bool `yaml:"borderless"`
}
// ThemeConfig represents the config for themes.
type ThemeConfig struct {
AppTheme string `yaml:"app_theme"`
SyntaxTheme SyntaxThemeConfig `yaml:"syntax_theme"`
}
// Config represents the main config for the application.
type Config struct {
Settings SettingsConfig `yaml:"settings"`
Theme ThemeConfig `yaml:"theme"`
}
// configError represents an error that occurred while parsing the config file.
type configError struct {
configDir string
parser ConfigParser
err error
}
// ConfigParser is the parser for the config file.
type ConfigParser struct{}
// getDefaultConfig returns the default config for the application.
func (parser ConfigParser) getDefaultConfig() Config {
return Config{
Settings: SettingsConfig{
StartDir: ".",
ShowIcons: true,
EnableLogging: false,
PrettyMarkdown: true,
Borderless: false,
},
Theme: ThemeConfig{
AppTheme: "default",
SyntaxTheme: SyntaxThemeConfig{
Dark: "dracula",
Light: "pygments",
},
},
}
}
// getDefaultConfigYamlContents returns the default config file contents.
func (parser ConfigParser) getDefaultConfigYamlContents() string {
defaultConfig := parser.getDefaultConfig()
yaml, _ := yaml.Marshal(defaultConfig)
return string(yaml)
}
// Error returns the error message for when a config file is not found.
func (e configError) Error() string {
return fmt.Sprintf(
`Couldn't find a config.yml configuration file.
Create one under: %s
Example of a config.yml file:
%s
For more info, go to https://github.com/mistakenelf/fm
press q to exit.
Original error: %v`,
path.Join(e.configDir, AppDir, ConfigFileName),
e.parser.getDefaultConfigYamlContents(),
e.err,
)
}
// writeDefaultConfigContents writes the default config file contents to the given file.
func (parser ConfigParser) writeDefaultConfigContents(newConfigFile *os.File) error {
_, err := newConfigFile.WriteString(parser.getDefaultConfigYamlContents())
if err != nil {
return err
}
return nil
}
// createConfigFileIfMissing creates the config file if it doesn't exist.
func (parser ConfigParser) createConfigFileIfMissing(configFilePath string) error {
if _, err := os.Stat(configFilePath); errors.Is(err, os.ErrNotExist) {
newConfigFile, err := os.OpenFile(configFilePath, os.O_RDWR|os.O_CREATE|os.O_EXCL, 0666)
if err != nil {
return err
}
defer newConfigFile.Close()
return parser.writeDefaultConfigContents(newConfigFile)
}
return nil
}
// getConfigFileOrCreateIfMissing returns the config file path or creates the config file if it doesn't exist.
func (parser ConfigParser) getConfigFileOrCreateIfMissing() (*string, error) {
var err error
configDir := os.Getenv("XDG_CONFIG_HOME")
if configDir == "" {
configDir, err = os.UserConfigDir()
if err != nil {
return nil, configError{parser: parser, configDir: configDir, err: err}
}
}
prsConfigDir := filepath.Join(configDir, AppDir)
err = os.MkdirAll(prsConfigDir, os.ModePerm)
if err != nil {
return nil, configError{parser: parser, configDir: configDir, err: err}
}
configFilePath := filepath.Join(prsConfigDir, ConfigFileName)
err = parser.createConfigFileIfMissing(configFilePath)
if err != nil {
return nil, configError{parser: parser, configDir: configDir, err: err}
}
return &configFilePath, nil
}
// parsingError represents an error that occurred while parsing the config file.
type parsingError struct {
err error
}
// Error represents an error that occurred while parsing the config file.
func (e parsingError) Error() string {
return fmt.Sprintf("failed parsing config.yml: %v", e.err)
}
// readConfigFile reads the config file and returns the config.
func (parser ConfigParser) readConfigFile(path string) (Config, error) {
config := parser.getDefaultConfig()
data, err := os.ReadFile(path)
if err != nil {
return config, configError{parser: parser, configDir: path, err: err}
}
err = yaml.Unmarshal((data), &config)
return config, err
}
// initParser initializes the parser.
func initParser() ConfigParser {
return ConfigParser{}
}
// ParseConfig parses the config file and returns the config.
func ParseConfig() (Config, error) {
var config Config
var err error
parser := initParser()
configFilePath, err := parser.getConfigFileOrCreateIfMissing()
if err != nil {
return config, parsingError{err: err}
}
config, err = parser.readConfigFile(*configFilePath)
if err != nil {
return config, parsingError{err: err}
}
return config, nil
}