-
Notifications
You must be signed in to change notification settings - Fork 0
/
git.go
136 lines (118 loc) · 3.05 KB
/
git.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
package gitadapter
import (
"encoding/json"
"errors"
"fmt"
"net/url"
"os"
"path"
"github.com/caddyserver/caddy/v2"
"github.com/caddyserver/caddy/v2/caddyconfig"
caddycmd "github.com/caddyserver/caddy/v2/cmd"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"
"go.uber.org/zap"
"sigs.k8s.io/yaml"
)
func init() {
caddyconfig.RegisterAdapter("git", Adapter{})
}
type GitAdapterConfig struct {
Url string `json:"url"`
Ref plumbing.ReferenceName `json:"ref"`
ClonePath string `json:"clone_path"`
Caddyfile string `json:"caddyfile"`
}
type Adapter struct{}
var tmpDir = os.TempDir()
func (a Adapter) Adapt(body []byte, options map[string]interface{}) (
[]byte, []caddyconfig.Warning, error) {
adapterConfig := GitAdapterConfig{}
err := yaml.Unmarshal(body, &adapterConfig)
if err != nil {
return nil, nil, err
}
if adapterConfig.Url == "" {
caddy.Log().Named("adapters.git.config").Error(fmt.Sprintf("url Not Found"))
panic("CaddyGitAdapter url Not Found")
}
if adapterConfig.Ref == "" {
adapterConfig.Ref = plumbing.Master
}
if adapterConfig.ClonePath == "" {
adapterConfig.ClonePath = tmpDir
}
if adapterConfig.Caddyfile == "" {
adapterConfig.Caddyfile = "Caddyfile"
}
p, err := url.Parse(adapterConfig.Url)
if err != nil {
return nil, nil, err
}
repoClonePath := path.Join(adapterConfig.ClonePath, p.Host, p.Path)
os.MkdirAll(repoClonePath, 0o744)
caddy.Log().Named("adapters.git.config").Info("cloning to", zap.String("dir", repoClonePath))
r, err := git.PlainClone(repoClonePath, false, &git.CloneOptions{
URL: adapterConfig.Url,
ReferenceName: adapterConfig.Ref,
})
if errors.Is(err, git.ErrRepositoryAlreadyExists) {
r, err = git.PlainOpen(repoClonePath)
if err != nil {
return nil, nil, errors.New("directory already exists and is not git repository")
}
} else if err != nil {
return nil, nil, err
}
err = r.Fetch(&git.FetchOptions{
Force: true,
})
if err != nil {
if !errors.Is(err, git.NoErrAlreadyUpToDate) {
return nil, nil, err
}
}
workTree, err := r.Worktree()
if err != nil {
return nil, nil, err
}
err = workTree.Reset(&git.ResetOptions{
Mode: git.HardReset,
})
if err != nil {
return nil, nil, err
}
err = workTree.Clean(&git.CleanOptions{
Dir: true,
})
if err != nil {
return nil, nil, err
}
err = workTree.Checkout(&git.CheckoutOptions{
Branch: adapterConfig.Ref,
})
if err != nil {
return nil, nil, err
}
err = workTree.Pull(&git.PullOptions{
Force: true,
ReferenceName: adapterConfig.Ref,
})
if err != nil {
if !errors.Is(err, git.NoErrAlreadyUpToDate) {
return nil, nil, err
}
}
config, _, err := caddycmd.LoadConfig(path.Join(repoClonePath, adapterConfig.Caddyfile), "caddyfile")
if err != nil {
return nil, nil, err
}
mapConfig := map[string]any{}
err = json.Unmarshal(config, &mapConfig)
if err != nil {
return nil, nil, err
}
sortedConfig, err := json.Marshal(mapConfig)
return sortedConfig, nil, err
}
var _ caddyconfig.Adapter = (*Adapter)(nil)