-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpost-processing.go
More file actions
194 lines (164 loc) · 4.26 KB
/
post-processing.go
File metadata and controls
194 lines (164 loc) · 4.26 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
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
//go:build ignore
// +build ignore
package main
import (
"bytes"
"encoding/json"
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"os/exec"
"path/filepath"
"regexp"
"strings"
)
const (
platformRepo = "github.com/percona/platform"
saasRepo = "github.com/percona/saas"
saasRoot = "../saas"
)
var generatedImportRe = regexp.MustCompile(`(?mi)[\n]^.*github_com_mwitkow.*$`)
func saasFilePatch(content []byte) []byte {
return bytes.Replace(content, []byte(platformRepo), []byte(saasRepo), -1)
}
// copyAndPatchFile copies a file src to dst, applying a specified patch function
func copyAndPatchFile(src, dst string, patchFunc func([]byte) []byte) error {
b, err := ioutil.ReadFile(src) //nolint:gosec
if err != nil {
return err
}
b = patchFunc(b)
if err = os.MkdirAll(filepath.Dir(dst), 0o755); err != nil { //nolint:gosec
return err
}
return ioutil.WriteFile(dst, b, 0o644)
}
// runInDir runs command name with args in dir and returns stdout.
func runInDir(dir, name string, args ...string) ([]byte, error) {
cmd := exec.Command(name, args...) //nolint:gosec
cmd.Dir = dir
cmd.Stderr = os.Stderr
log.Print(strings.Join(cmd.Args, " "))
return cmd.Output()
}
func removeDirs(root string, directories ...string) {
for _, d := range directories {
path := filepath.Join(root, d)
log.Printf("Removing %s ...", path)
if err := os.RemoveAll(path); err != nil {
log.Fatal(err)
}
}
}
// makeProcessDirsFunc returns a function that applies patch function to included files and copies them to the root directory.
func makeProcessDirsFunc(root string, patchFunc func([]byte) []byte, includeFiles []string, excludeFiles []string) func(string, os.FileInfo, error) error {
return func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
var copy bool
for _, s := range includeFiles {
if strings.Contains(path, "internal") {
// TODO: Improve internal packages handling
panic("internal packages should not be copied to saas repo")
}
if strings.HasSuffix(path, s) {
copy = true
}
}
for _, s := range excludeFiles {
if strings.HasSuffix(path, s) {
copy = false
}
}
if !copy {
return nil
}
dst := filepath.Join(root, path)
log.Printf(" %s -> %s", path, dst)
return copyAndPatchFile(path, dst, patchFunc)
}
}
func walk(processDirsFunc func(string, os.FileInfo, error) error, directories ...string) {
log.Print("Copying and patching files:")
for _, src := range directories {
err := filepath.Walk(src, processDirsFunc)
if err != nil {
log.Fatal(err)
}
}
}
func processSaas() {
if _, err := os.Stat(saasRoot); err != nil {
log.Fatal(err)
}
removeDirs(saasRoot, "api", "gen", "pkg")
processDirsFunc := makeProcessDirsFunc(saasRoot, saasFilePatch, []string{".go", ".proto"}, []string{"_test.go", "_fuzz.go"})
walk(
processDirsFunc,
"api/telemetry",
"gen/telemetry", "gen/utils/",
"pkg/logger",
)
// install and tidy to check if we have anything
_, err := runInDir(saasRoot, "go", "mod", "tidy")
if err != nil {
log.Fatal(err)
}
_, err = runInDir(saasRoot, "go", "install", "-v", "./...")
if err != nil {
log.Fatal(err)
}
// check dependencies
b, err := runInDir(saasRoot, "go", "list", "-json", "./...")
if err != nil {
log.Fatal(err)
}
type packageInfo struct {
Dir string
Deps []string
}
d := json.NewDecoder(bytes.NewReader(b))
for {
var info packageInfo
err = d.Decode(&info)
if err == io.EOF {
return
}
if err != nil {
log.Fatal(err)
}
for _, dep := range info.Deps {
if strings.Contains(dep, platformRepo) {
log.Fatalf("%s depends on platform module:\n%s", info.Dir, strings.Join(info.Deps, "\n"))
}
}
}
}
func main() {
const saasProject = "saas"
availableProjects := []string{
saasProject,
}
availableProjectsStr := strings.Join(availableProjects, " | ")
project := flag.String("project", "", fmt.Sprintf("project to run post-processing for (%s)", availableProjectsStr))
flag.Parse()
if flag.NFlag() > 1 {
flag.PrintDefaults()
log.Fatal("Too many arguments, use only one")
}
if flag.NFlag() == 0 {
flag.PrintDefaults()
log.Fatal("You have to provide one argument")
}
switch *project {
case saasProject:
processSaas()
default:
flag.PrintDefaults()
log.Fatal("Provide the target project name")
}
}