forked from sosedoff/gitkit
-
Notifications
You must be signed in to change notification settings - Fork 4
/
config.go
101 lines (83 loc) · 2.13 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
package gitkit
import (
"io/ioutil"
"os"
"path/filepath"
)
type Config struct {
KeyDir string // Directory for server ssh keys. Only used in SSH strategy.
Dir string // Directory that contains repositories
GitPath string // Path to git binary
GitUser string // User for ssh connections
AutoCreate bool // Automatically create repostories
AutoHooks bool // Automatically setup git hooks
Hooks *HookScripts // Scripts for hooks/* directory
Auth bool // Require authentication
}
// HookScripts represents all repository server-size git hooks
type HookScripts struct {
PreReceive string
Update string
PostReceive string
}
// Configure hook scripts in the repo base directory
func (c *HookScripts) setupInDir(path string) error {
basePath := filepath.Join(path, "hooks")
scripts := map[string]string{
"pre-receive": c.PreReceive,
"update": c.Update,
"post-receive": c.PostReceive,
}
// Cleanup any existing hooks first
hookFiles, err := ioutil.ReadDir(basePath)
if err == nil {
for _, file := range hookFiles {
if err := os.Remove(filepath.Join(basePath, file.Name())); err != nil {
return err
}
}
}
// Write new hook files
for name, script := range scripts {
fullPath := filepath.Join(basePath, name)
// Dont create hook if there's no script content
if script == "" {
continue
}
if err := ioutil.WriteFile(fullPath, []byte(script), 0755); err != nil {
logError("hook-update", err)
return err
}
}
return nil
}
func (c *Config) KeyPath() string {
return filepath.Join(c.KeyDir, "gitkit.rsa")
}
func (c *Config) Setup() error {
if _, err := os.Stat(c.Dir); err != nil {
if err = os.Mkdir(c.Dir, 0755); err != nil {
return err
}
}
if c.AutoHooks == true {
return c.setupHooks()
}
return nil
}
func (c *Config) setupHooks() error {
files, err := ioutil.ReadDir(c.Dir)
if err != nil {
return err
}
for _, file := range files {
if !file.IsDir() {
continue
}
path := filepath.Join(c.Dir, file.Name())
if err := c.Hooks.setupInDir(path); err != nil {
return err
}
}
return nil
}