-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
271 lines (188 loc) · 6 KB
/
main.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
package main
import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"strings"
)
type hooksConfig struct {
PreCommit string `json:"pre-commit"`
PostCommit string `json:"post-commit"`
CommitMsg string `json:"commit-msg"`
PreRebase string `json:"pre-rebase"`
PostRebase string `json:"post-rebase"`
PostCheckout string `json:"post-checkout"`
PostMerge string `json:"post-merge"`
PostRewrite string `json:"post-rewrite"`
PrePush string `json:"pre-push"`
}
func loadHooksConfig(filename string) (*hooksConfig, error) {
data, err := os.ReadFile(filename)
if err != nil {
return nil, fmt.Errorf("error reading file %s: %v", filename, err)
}
var config hooksConfig
err = json.Unmarshal(data, &config)
if err != nil {
return nil, fmt.Errorf("error parsing JSON: %v", err)
}
return &config, nil
}
func updateHookInConfig(configFile, scriptFile string, hookName string) error {
scriptContent, err := os.ReadFile(scriptFile)
if err != nil {
return fmt.Errorf("error reading file %s: %v", scriptFile, err)
}
data, err := os.ReadFile(configFile)
if err != nil {
return fmt.Errorf("error reading configuration file %s: %v", configFile, err)
}
var hooks hooksConfig
err = json.Unmarshal(data, &hooks)
if err != nil {
return fmt.Errorf("error parsing configuration JSON: %v", err)
}
getHookField := map[string]func() *string{
"pre-commit": func() *string { return &hooks.PreCommit },
"post-commit": func() *string { return &hooks.PostCommit },
"commit-msg": func() *string { return &hooks.CommitMsg },
"pre-rebase": func() *string { return &hooks.PreRebase },
"post-rebase": func() *string { return &hooks.PostRebase },
"post-checkout": func() *string { return &hooks.PostCheckout },
"post-merge": func() *string { return &hooks.PostMerge },
"post-rewrite": func() *string { return &hooks.PostRewrite },
"pre-push": func() *string { return &hooks.PrePush },
}
if getField, exists := getHookField[hookName]; exists {
updateField := getField()
*updateField = string(scriptContent)
} else {
return fmt.Errorf("unrecognized filename for hook: %s", scriptFile)
}
updatedData, err := json.MarshalIndent(hooks, "", " ")
if err != nil {
return fmt.Errorf("error serializing updated JSON: %v", err)
}
err = os.WriteFile(configFile, updatedData, 0644)
if err != nil {
return fmt.Errorf("error writing configuration file %s: %v", configFile, err)
}
return nil
}
func createGitHook(hookName string, hookContent string) error {
hookPath := fmt.Sprintf(".git/hooks/%s", hookName)
hookFile, err := os.OpenFile(hookPath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0755)
if err != nil {
return fmt.Errorf("error creating the hook %s: %v", hookName, err)
}
defer hookFile.Close()
_, err = hookFile.WriteString(hookContent)
if err != nil {
return fmt.Errorf("error writing to hook %s: %v", hookName, err)
}
err = os.Chmod(hookPath, 0755)
if err != nil {
return fmt.Errorf("error when changing hook permissions %s: %v", hookName, err)
}
return nil
}
func checkAndUpdateHooks(configFile string, hookDir string) error {
hookFiles := []string{
"pre-commit.sh",
"post-commit.sh",
"commit-msg.sh",
"pre-rebase.sh",
"post-rebase.sh",
"post-checkout.sh",
"post-merge.sh",
"post-rewrite.sh",
"pre-push.sh",
}
for _, hookFile := range hookFiles {
hookPath := filepath.Join(hookDir, hookFile)
if _, err := os.Stat(hookPath); err == nil {
fmt.Printf("Apply %s \n", hookPath)
hookName := strings.TrimSuffix(hookFile, ".sh")
err := updateHookInConfig(configFile, hookPath, hookName)
if err != nil {
return fmt.Errorf("error updating configuration for %s: %v", hookPath, err)
}
} else if os.IsNotExist(err) {
data := fmt.Sprintf("#!/bin/bash\necho \"🔄 Running %s hook...\"", hookFile)
hookFile, err := os.OpenFile(hookPath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0755)
if err != nil {
return fmt.Errorf("error creating the hook %v: %v", hookFile, err)
}
defer hookFile.Close()
_, err = hookFile.WriteString(data)
if err != nil {
return fmt.Errorf("error writing to hook %v: %v", hookFile, err)
}
} else {
return fmt.Errorf("error when verifying the existence of %s: %v", hookPath, err)
}
}
return nil
}
func initializeConfigFile(configFile string) error {
if _, err := os.Stat(configFile); err == nil {
return nil
} else if os.IsNotExist(err) {
config := hooksConfig{}
data, err := json.MarshalIndent(config, "", " ")
if err != nil {
return fmt.Errorf("error serializing configuration file: %v", err)
}
err = os.WriteFile(configFile, data, 0644)
if err != nil {
return fmt.Errorf("error writing configuration file %s: %v", configFile, err)
}
fmt.Println("Configuration file created successfully:", configFile)
return nil
} else {
return fmt.Errorf("error verifying file existence %s: %v", configFile, err)
}
}
func main() {
if len(os.Args) < 2 {
fmt.Println("A directory must be provided for the hook files.")
return
}
hookDir := os.Args[1]
configFile := "hooks_config.json"
err := initializeConfigFile(configFile)
if err != nil {
fmt.Println("Error initializing configuration file:", err)
return
}
err = checkAndUpdateHooks(configFile, hookDir)
if err != nil {
fmt.Printf("Error verifying and updating hooks: %v\n", err)
return
}
config, err := loadHooksConfig(configFile)
if err != nil {
fmt.Printf("Error loading configuration: %v\n", err)
return
}
hooks := map[string]string{
"pre-commit": config.PreCommit,
"post-commit": config.PostCommit,
"commit-msg": config.CommitMsg,
"pre-rebase": config.PreRebase,
"post-rebase": config.PostRebase,
"post-checkout": config.PostCheckout,
"post-merge": config.PostMerge,
"post-rewrite": config.PostRewrite,
"pre-push": config.PrePush,
}
for hookName, hookContent := range hooks {
err := createGitHook(hookName, hookContent)
if err != nil {
fmt.Printf("Error creating the hook %s: %v\n", hookName, err)
return
}
}
fmt.Println("Hooks configured correctly.")
}