-
Notifications
You must be signed in to change notification settings - Fork 0
/
handleprivaterepo.go
197 lines (168 loc) · 5.76 KB
/
handleprivaterepo.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
package main
import (
"crypto/sha1"
"encoding/hex"
"fmt"
"log"
"os"
"os/exec"
"path"
"strings"
"github.com/jrschumacher/doctl-serverless-go/pkg/projectconfig"
"golang.org/x/mod/modfile"
)
func cleanPrivateRepo(_ *projectconfig.ProjectSpec) goPackageFunc {
log.Print("cleaning private repos...")
return func(pkgDirName, actDirName string) error {
prefix := pkgPrefix(pkgDirName, actDirName)
// remove private repo dir
log.Print(prefix("removing private repo dir... "))
privateRepoDir := path.Join(actDirName, PrivateRepoDir)
if _, err := os.Stat(privateRepoDir); os.IsNotExist(err) {
log.Print(prefix("skip: private repo dir does not exist"))
return nil
}
if err := os.RemoveAll(privateRepoDir); err != nil {
return fmt.Errorf(prefix("error: failed removing private repo dir: %w"), prefix, err)
}
// restore go.mod to original state
log.Print(prefix("restoring go.mod to original state... "))
goMod := path.Join(actDirName, "go.mod")
// find all go.mod.*.bak files
var goModBak string
files, err := os.ReadDir(actDirName)
if err != nil {
return fmt.Errorf(prefix("error: failed reading dir: %w"), err)
}
for _, f := range files {
if strings.HasPrefix(f.Name(), "go.mod.") && strings.HasSuffix(f.Name(), ".bak") {
goModBak = path.Join(actDirName, f.Name())
break
}
}
// test checksum
if goModBak == "" {
return fmt.Errorf(prefix("error: failed finding go.mod backup"))
}
hash := strings.TrimSuffix(strings.TrimPrefix(path.Base(goModBak), "go.mod."), ".bak")
modFile, err := os.ReadFile(goMod)
if err != nil {
return fmt.Errorf(prefix("error: failed reading go.mod: %w"), err)
}
h := sha1.New()
if _, err := h.Write(modFile); err != nil {
return fmt.Errorf(prefix("error: failed hashing go.mod: %w"), err)
}
hexsum := hex.EncodeToString(h.Sum(nil))
if hash != hexsum {
log.Printf(prefix("checksum: %s"), hexsum)
log.Printf(prefix("hash: %s"), hash)
return fmt.Errorf(prefix("error: go.mod checksum mismatch backup: %s hash: %s"), goModBak)
}
if f, err := os.Stat(goModBak); err == nil && !f.IsDir() {
if err := os.Remove(goMod); err != nil {
return fmt.Errorf(prefix("error: failed removing go.mod: %w"), err)
}
if err := os.Rename(goModBak, goMod); err != nil {
return fmt.Errorf(prefix("error: failed restoring go.mod: %w"), err)
}
}
return nil
}
}
func clonePrivateRepo(projectCfg *projectconfig.ProjectSpec) goPackageFunc {
log.Print("checking for private repos...")
// check if GOPRIVATE is set in projectCfg
var privateRepos []string
for k, v := range projectCfg.Environment {
if k == "GOPRIVATE" {
privateRepos = strings.Split(v, ",")
break
}
}
// if no private repos defined, skip
if len(privateRepos) == 0 {
log.Print("skipping: no private repos defined in GOPRIVATE environment")
return nil
}
log.Print("cloning private repos for each go project...")
return func(pkgDirName, actDirName string) error {
prefix := pkgPrefix(pkgDirName, actDirName)
goMod := path.Join(actDirName, "go.mod")
modFile, err := os.ReadFile(goMod)
if err != nil {
return fmt.Errorf(prefix("error: failed reading go.mod: %w"), err)
}
mod, err := modfile.Parse(goMod, modFile, nil)
if err != nil {
return fmt.Errorf(prefix("error: failed parsing go.mod: %w"), err)
}
// make private repo dir
log.Print(prefix("creating private repo dir... "))
privateRepoDir := path.Join(actDirName, PrivateRepoDir)
if _, err := os.Stat(privateRepoDir); os.IsNotExist(err) {
os.Mkdir(privateRepoDir, 0755)
} else {
return fmt.Errorf(prefix("error: private repo dir already exists"))
}
// for each require check if it is a private repo
goModChange := false
for _, r := range mod.Require {
for _, p := range privateRepos {
if match, err := path.Match(p, r.Mod.Path); err != nil {
return fmt.Errorf(prefix("error: failed matching private repo: %w"), err)
} else if match {
clonePath := path.Join(privateRepoDir, r.Mod.Path)
newModPath := "." + string(os.PathSeparator) + path.Join(PrivateRepoDir, r.Mod.Path)
log.Printf(prefix("found private repo: %s"), r.Mod.Path)
// clone private repo
log.Print(prefix("cloning private repo... "))
// recursively create private repo dir
dir := privateRepoDir
for _, d := range strings.Split(path.Dir(r.Mod.Path), "/") {
dir = path.Join(dir, d)
log.Printf(prefix("creating dir: %s"), dir)
if _, err := os.Stat(privateRepoDir); os.IsNotExist(err) {
os.Mkdir(privateRepoDir, 0755)
}
}
cmd := exec.Command("git", "clone", "https://"+r.Mod.Path, clonePath)
o, err := cmd.CombinedOutput()
if err != nil {
return fmt.Errorf(prefix("error: failed cloning private repo: %s"), o)
}
// modify go.mod to use private repo
log.Print(prefix("modifying go.mod to use private repo... "))
if err := mod.AddReplace(r.Mod.Path, r.Mod.Version, newModPath, ""); err != nil {
return fmt.Errorf(prefix("error: failed adding replace to go.mod: %w"), err)
}
goModChange = true
}
}
}
if !goModChange {
log.Print(prefix("no private repos found"))
return nil
}
// write go.mod
b, err := mod.Format()
if err != nil {
return fmt.Errorf(prefix("error: failed formatting go.mod: %w"), err)
}
// sha1 hash of bytes
h := sha1.New()
if _, err := h.Write(b); err != nil {
return fmt.Errorf(prefix("error: failed hashing go.mod: %w"), err)
}
hexsum := hex.EncodeToString(h.Sum(nil))
// backup go.mod
if err := os.Rename(goMod, goMod+"."+hexsum+".bak"); err != nil {
return fmt.Errorf(prefix("error: failed renaming go.mod: %w"), err)
}
// write go.mod
if err := os.WriteFile(goMod, b, 0644); err != nil {
return fmt.Errorf(prefix("error: failed writing go.mod: %w"), err)
}
return nil
}
}