-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathconfig.go
More file actions
138 lines (120 loc) · 4.56 KB
/
Copy pathconfig.go
File metadata and controls
138 lines (120 loc) · 4.56 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
package main
import (
"fmt"
"os"
"github.com/charmbracelet/huh"
"github.com/charmbracelet/log"
"github.com/spf13/afero"
"github.com/stefanlogue/meteor/pkg/config"
)
const (
defaultCommitTitleCharLimit = 48
defaultCommitBodyCharLimit = 0
defaultCommitBodyLineLength = 0
minimumCommitBodyLineLength = 20
defaultMessageTemplate = "{{.Type}}{{if .Scope}}({{.Scope}}){{end}}{{if .IsBreakingChange}}!{{end}}: {{.Message}}"
defaultMessageWithTicketTemplate = "{{.TicketNumber}}{{if .Scope}}({{.Scope}}){{end}}{{if .IsBreakingChange}}!{{end}}: <{{.Type}}> {{.Message}}"
)
type LoadConfigReturn struct {
MessageTemplate string
MessageWithTicketTemplate string
Prefixes []huh.Option[string]
Coauthors []huh.Option[string]
Boards []huh.Option[string]
Scopes []huh.Option[string]
CommitTitleCharLimit int
CommitBodyCharLimit int
CommitBodyLineLength int
ShowIntro bool
ReadContributorsFromGit bool
AddAll bool
}
// loadConfig loads the config file from the current directory or any parent
func loadConfig(fs afero.Fs) (LoadConfigReturn, error) {
filePath, err := config.FindConfigFile(fs, os.Getwd, os.UserHomeDir)
if err != nil {
log.Debug("Error finding config file", "error", err)
return LoadConfigReturn{
MessageTemplate: defaultMessageTemplate,
MessageWithTicketTemplate: defaultMessageWithTicketTemplate,
Prefixes: config.DefaultPrefixes,
CommitTitleCharLimit: defaultCommitTitleCharLimit,
CommitBodyCharLimit: defaultCommitBodyCharLimit,
CommitBodyLineLength: defaultCommitBodyLineLength,
ShowIntro: true,
ReadContributorsFromGit: false,
AddAll: false,
}, nil
}
log.Debug("found config file", "path", filePath)
c := config.New()
err = c.LoadFile(filePath)
if err != nil {
return LoadConfigReturn{
MessageTemplate: defaultMessageTemplate,
MessageWithTicketTemplate: defaultMessageWithTicketTemplate,
CommitTitleCharLimit: defaultCommitTitleCharLimit,
CommitBodyCharLimit: defaultCommitBodyCharLimit,
CommitBodyLineLength: defaultCommitBodyLineLength,
ShowIntro: true,
ReadContributorsFromGit: false,
AddAll: false,
}, fmt.Errorf("error parsing config file: %w", err)
}
if c.AddAll == nil {
all := false
c.AddAll = &all
}
if c.ShowIntro == nil {
showIntro := true
c.ShowIntro = &showIntro
}
if c.CommitTitleCharLimit == nil || *c.CommitTitleCharLimit < defaultCommitTitleCharLimit {
commitTitleCharLimit := defaultCommitTitleCharLimit
c.CommitTitleCharLimit = &commitTitleCharLimit
}
if c.CommitBodyCharLimit == nil || *c.CommitBodyCharLimit < defaultCommitBodyCharLimit {
commitBodyCharLimit := defaultCommitBodyCharLimit
c.CommitBodyCharLimit = &commitBodyCharLimit
}
if c.CommitBodyLineLength == nil || *c.CommitBodyLineLength < minimumCommitBodyLineLength {
commitBodyLineLength := defaultCommitBodyLineLength
c.CommitBodyLineLength = &commitBodyLineLength
}
if c.ReadContributorsFromGit == nil {
read := false
c.ReadContributorsFromGit = &read
}
messageTemplate := defaultMessageTemplate
if c.MessageTemplate != nil {
messageTemplate, err = config.ConvertTemplate(*c.MessageTemplate)
if err != nil {
log.Error("Error converting message template", "error", err)
messageTemplate = defaultMessageTemplate
}
}
c.MessageTemplate = &messageTemplate
messageWithTicketTemplate := defaultMessageWithTicketTemplate
if c.MessageWithTicketTemplate != nil {
messageWithTicketTemplate, err = config.ConvertTemplate(*c.MessageWithTicketTemplate)
if err != nil {
log.Error("Error converting message with ticket template", "error", err)
messageWithTicketTemplate = defaultMessageWithTicketTemplate
}
}
c.MessageWithTicketTemplate = &messageWithTicketTemplate
return LoadConfigReturn{
MessageTemplate: messageTemplate,
MessageWithTicketTemplate: messageWithTicketTemplate,
Prefixes: c.Prefixes.Options(),
Coauthors: c.Coauthors.Options(),
Boards: c.Boards.Options(),
Scopes: c.Scopes.Options(),
CommitTitleCharLimit: *c.CommitTitleCharLimit,
CommitBodyCharLimit: *c.CommitBodyCharLimit,
CommitBodyLineLength: *c.CommitBodyLineLength,
ShowIntro: *c.ShowIntro,
ReadContributorsFromGit: *c.ReadContributorsFromGit,
AddAll: *c.AddAll,
}, nil
}