-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.go
More file actions
155 lines (137 loc) · 3.72 KB
/
config.go
File metadata and controls
155 lines (137 loc) · 3.72 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
package main
import (
"encoding/json"
"os"
"path/filepath"
)
type prefix struct {
T string `json:"title"`
D string `json:"description"`
}
type config struct {
Prefixes []prefix `json:"prefixes"`
SignOffCommits bool `json:"signOffCommits"`
ScopeInputCharLimit int `json:"scopeInputCharLimit"`
CommitInputCharLimit int `json:"commitInputCharLimit"`
TotalInputCharLimit int `json:"totalInputCharLimit"`
OverflowCharLimit bool `json:"overflowCharLimit"`
ScopeCompletionOrder string `json:"scopeCompletionOrder"`
FindAllCommitMessages bool `json:"findAllCommitMessages"`
StoreRuntime bool `json:"storeRuntime"`
ShowRuntime bool `json:"showRuntime"`
ShowStats bool `json:"showStats"`
ShowStatsFormat string `json:"showStatsFormat"`
SessionStatAsSeconds bool `json:"sessionStatAsSeconds"`
ColorScheme string `json:"colorScheme,omitempty"`
}
func (i prefix) Title() string { return i.T }
func (i prefix) Description() string { return i.D }
func (i prefix) FilterValue() string { return i.T }
var defaultPrefixes = []prefix{
{
T: "feat",
D: "Introduces a new feature",
},
{
T: "fix",
D: "Patches a bug",
},
{
T: "docs",
D: "Documentation changes only",
},
{
T: "test",
D: "Adding missing tests or correcting existing tests",
},
{
T: "build",
D: "Changes that affect the build system",
},
{
T: "ci",
D: "Changes to CI configuration files and scripts",
},
{
T: "perf",
D: "A code change that improves performance",
},
{
T: "refactor",
D: "A code change that neither fixes a bug nor adds a feature",
},
{
T: "revert",
D: "Reverts a previous change",
},
{
T: "style",
D: "Changes that do not affect the meaning of the code (white-space, formatting, missing semi-colons, etc)",
},
{
T: "chore",
D: "A minor change which does not fit into any other category",
},
}
const applicationName = "cometary"
func loadConfig() (*config, string) {
nonXdgConfigFile := ".comet.json"
// Check for configuration file local to current directory
if _, err := os.Stat(nonXdgConfigFile); err == nil {
return loadConfigFile(nonXdgConfigFile)
}
// Check for configuration file local to user's home directory
if home, err := os.UserHomeDir(); err == nil {
path := filepath.Join(home, nonXdgConfigFile)
if _, err := os.Stat(path); err == nil {
return loadConfigFile(path)
}
}
// Check for configuration file according to XDG Base Directory Specification
if cfgDir, err := GetConfigDir(); err == nil {
path := filepath.Join(cfgDir, "config.json")
if _, err := os.Stat(path); err == nil {
return loadConfigFile(path)
}
}
return newConfig(), ""
}
func newConfig() *config {
return &config{
Prefixes: defaultPrefixes,
SignOffCommits: false,
ScopeInputCharLimit: 16,
CommitInputCharLimit: 100,
TotalInputCharLimit: 0,
OverflowCharLimit: false,
ScopeCompletionOrder: "descending",
FindAllCommitMessages: false,
StoreRuntime: false,
ShowRuntime: false,
ShowStats: false,
ShowStatsFormat: "seconds",
SessionStatAsSeconds: true,
}
}
func GetConfigDir() (string, error) {
configDir := os.Getenv("XDG_CONFIG_HOME")
if configDir == "" || configDir[0:1] != "/" {
homeDir, err := os.UserHomeDir()
if err != nil {
return "", err
}
return filepath.Join(homeDir, ".config", applicationName), nil
}
return filepath.Join(configDir, applicationName), nil
}
func loadConfigFile(path string) (*config, string) {
var c config
data, err := os.ReadFile(path)
if err != nil {
return &c, path
}
if err := json.Unmarshal(data, &c); err != nil {
return &c, path
}
return &c, path
}