-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathconfig.go
116 lines (102 loc) · 2.4 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
package main
import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"github.com/charmbracelet/bubbles/list"
)
type prefix struct {
T string `json:"title"`
D string `json:"description"`
}
type config struct {
Prefixes []prefix `json:"prefixes"`
SignOffCommits bool `json:"signOffCommits"`
}
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 = []list.Item{
prefix{
T: "feat",
D: "Introduces a new feature",
},
prefix{
T: "fix",
D: "Patches a bug",
},
prefix{
T: "docs",
D: "Documentation changes only",
},
prefix{
T: "test",
D: "Adding missing tests or correcting existing tests",
},
prefix{
T: "build",
D: "Changes that affect the build system",
},
prefix{
T: "ci",
D: "Changes to CI configuration files and scripts",
},
prefix{
T: "perf",
D: "A code change that improves performance",
},
prefix{
T: "refactor",
D: "A code change that neither fixes a bug nor adds a feature",
},
prefix{
T: "revert",
D: "Reverts a previous change",
},
prefix{
T: "style",
D: "Changes that do not affect the meaning of the code (white-space, formatting, missing semi-colons, etc)",
},
prefix{
T: "chore",
D: "A minor change which does not fit into any other category",
},
}
const configFile = ".comet.json"
func loadConfig() ([]list.Item, bool, error) {
if _, err := os.Stat(configFile); err == nil {
return loadConfigFile(configFile)
}
if home, err := os.UserHomeDir(); err == nil {
path := filepath.Join(home, configFile)
if _, err := os.Stat(path); err == nil {
return loadConfigFile(path)
}
}
if _, err := os.Stat(configFile); err == nil {
return loadConfigFile(configFile)
}
return defaultPrefixes, false, nil
}
func loadConfigFile(path string) ([]list.Item, bool, error) {
data, err := os.ReadFile(path)
if err != nil {
return nil, false, fmt.Errorf("failed to read config file: %w", err)
}
var c config
if err := json.Unmarshal(data, &c); err != nil {
return nil, false, fmt.Errorf("invalid json in config file '%s': %w", path, err)
}
return convertPrefixes(c.Prefixes), c.SignOffCommits, nil
}
func convertPrefixes(prefixes []prefix) []list.Item {
var output []list.Item
for _, prefix := range prefixes {
output = append(output, prefix)
}
if len(output) == 0 {
return defaultPrefixes
}
return output
}