-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.go
More file actions
116 lines (99 loc) · 2.97 KB
/
Copy pathconfig.go
File metadata and controls
116 lines (99 loc) · 2.97 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
package main
import (
"fmt"
"os"
"path/filepath"
"github.com/spf13/viper"
)
// Config holds the application configuration.
type Config struct {
ShowHidden bool `mapstructure:"show_hidden"`
ShowIcons bool `mapstructure:"show_icons"`
PreviewEnabled bool `mapstructure:"preview_enabled"`
PreviewMaxLines int `mapstructure:"preview_max_lines"`
GitEnabled bool `mapstructure:"git_enabled"`
Theme string `mapstructure:"theme"`
SortBy string `mapstructure:"sort_by"`
SortReverse bool `mapstructure:"sort_reverse"`
Bookmarks []string `mapstructure:"bookmarks"`
}
// DefaultConfig returns the default configuration.
func DefaultConfig() *Config {
return &Config{
ShowHidden: false,
ShowIcons: true,
PreviewEnabled: true,
PreviewMaxLines: 500,
GitEnabled: true,
Theme: "default",
SortBy: "name",
SortReverse: false,
}
}
// ConfigPath returns the path to the configuration file.
func ConfigPath() string {
dir, err := os.UserConfigDir()
if err != nil {
dir = "."
}
return filepath.Join(dir, "traverse", "config.yaml")
}
// LoadConfig loads the configuration from file.
func LoadConfig() (*Config, error) {
cfg := DefaultConfig()
v := viper.New()
v.SetConfigFile(ConfigPath())
// Set defaults
v.SetDefault("show_hidden", cfg.ShowHidden)
v.SetDefault("show_icons", cfg.ShowIcons)
v.SetDefault("preview_enabled", cfg.PreviewEnabled)
v.SetDefault("preview_max_lines", cfg.PreviewMaxLines)
v.SetDefault("git_enabled", cfg.GitEnabled)
v.SetDefault("theme", cfg.Theme)
v.SetDefault("sort_by", cfg.SortBy)
v.SetDefault("sort_reverse", cfg.SortReverse)
// Try to read config file
if err := v.ReadInConfig(); err != nil {
if _, ok := err.(viper.ConfigFileNotFoundError); !ok {
return nil, fmt.Errorf("error reading config: %w", err)
}
// Config file not found, use defaults - that's OK
}
if err := v.Unmarshal(cfg); err != nil {
return nil, fmt.Errorf("error unmarshaling config: %w", err)
}
return cfg, nil
}
// SaveConfig saves the configuration to file.
func SaveConfig(cfg *Config) error {
configFile := ConfigPath()
dir := filepath.Dir(configFile)
if err := os.MkdirAll(dir, 0755); err != nil {
return fmt.Errorf("error creating config directory: %w", err)
}
v := viper.New()
v.SetConfigFile(configFile)
v.Set("show_hidden", cfg.ShowHidden)
v.Set("show_icons", cfg.ShowIcons)
v.Set("preview_enabled", cfg.PreviewEnabled)
v.Set("preview_max_lines", cfg.PreviewMaxLines)
v.Set("git_enabled", cfg.GitEnabled)
v.Set("theme", cfg.Theme)
v.Set("sort_by", cfg.SortBy)
v.Set("sort_reverse", cfg.SortReverse)
v.Set("bookmarks", cfg.Bookmarks)
return v.WriteConfigAs(configFile)
}
// SortModeFromConfig returns the SortMode from config string.
func SortModeFromConfig(s string) SortMode {
switch s {
case "size":
return SortBySize
case "modified", "time":
return SortByModified
case "type":
return SortByType
default:
return SortByName
}
}