@@ -58,14 +58,16 @@ func syncRemoteRepo(repo *repo) {
58
58
projectLocalDirectory := path .Join ("./pkg" , repo .LocalAPIDirectory )
59
59
60
60
gitClone (basePath , repo )
61
+ excludeAndRefactor (projectClonedDirectory , projectLocalDirectory , repo )
61
62
62
63
glog .V (100 ).Infof ("Comparing local %s and cloned %s api directories for repo %s" ,
63
64
projectLocalDirectory , projectClonedDirectory , repo .Name )
64
65
65
- if repoSynced (projectClonedDirectory , projectLocalDirectory , repo ) {
66
- gitReset (repo .LocalAPIDirectory )
67
- } else {
68
- syncDirectories (projectClonedDirectory , projectLocalDirectory , repo )
66
+ err := execCmd ("" , "diff" , []string {projectClonedDirectory , projectLocalDirectory })
67
+ if err != nil {
68
+ glog .V (100 ).Infof ("Repos not synced. Copying cloned repo %s to %s" , projectClonedDirectory , projectLocalDirectory )
69
+
70
+ copyClonedToLocal (projectClonedDirectory , projectLocalDirectory )
69
71
}
70
72
71
73
glog .V (100 ).Infof ("Remove cloned directory from filesystem: %s" , path .Join (basePath , repo .Name ))
@@ -81,21 +83,13 @@ func syncRemoteRepo(repo *repo) {
81
83
}
82
84
}
83
85
84
- func repoSynced (clonedDir , localDir string , repo * repo ) bool {
85
- glog .V (100 ).Infof ("Verifying destination directory %s exists" , localDir )
86
-
87
- if _ , err := os .Stat (localDir ); os .IsNotExist (err ) {
88
- glog .V (100 ).Infof ("Destination api directory %s doesn't exist creating directory" , localDir )
89
-
90
- if os .MkdirAll (localDir , 0777 ) != nil {
91
- glog .V (100 ).Infof ("Failed to create api directory. Exit with error code 1" )
92
- os .Exit (1 )
93
- }
94
- }
86
+ // excludeAndRefactor excludes and refactors files in the clonedDir to prepare them for being compared or copied to the
87
+ // localDir.
88
+ func excludeAndRefactor (clonedDir , localDir string , repo * repo ) {
89
+ glog .V (100 ).Infof ("Updating %s to match expected state of %s" , clonedDir , localDir )
95
90
96
91
if len (repo .Excludes ) > 0 {
97
- glog .V (100 ).Infof ("Remove excluded files under %s" ,
98
- path .Base (clonedDir ))
92
+ glog .V (100 ).Infof ("Remove excluded files under %s" , path .Base (clonedDir ))
99
93
100
94
err := excludeFiles (clonedDir , repo .Excludes ... )
101
95
if err != nil {
@@ -108,115 +102,45 @@ func repoSynced(clonedDir, localDir string, repo *repo) bool {
108
102
path .Base (clonedDir ), path .Base (localDir ))
109
103
110
104
err := refactor (
111
- fmt .Sprintf ("package %s" , path .Base (localDir )),
112
105
fmt .Sprintf ("package %s" , path .Base (clonedDir )),
113
- fmt .Sprintf ("./%s" , localDir ), "*.go" )
106
+ fmt .Sprintf ("package %s" , path .Base (localDir )),
107
+ clonedDir , "*.go" )
114
108
115
109
if err != nil {
116
- glog .V (100 ).Infof ("Failed to refactor file before sync due to %w. Exit with error 1" , err )
110
+ glog .V (100 ).Infof ("Failed to replace package names due to %w. Exit with error 1" , err )
117
111
os .Exit (1 )
118
112
}
119
113
120
- glog .V (100 ).Infof ("Replace cloned package imports" )
121
-
122
114
for _ , importMap := range repo .ReplaceImports {
123
- err = refactor (
124
- importMap ["new" ],
125
- importMap ["old" ],
126
- localDir ,
127
- "*.go" )
128
-
115
+ err = refactor (importMap ["old" ], importMap ["new" ], clonedDir , "*.go" )
129
116
if err != nil {
130
- glog .V (100 ).Infof ("Failed to refactor file . Exit with error 1" )
117
+ glog .V (100 ).Infof ("Failed to refactor files due to %w . Exit with error 1" , err )
131
118
os .Exit (1 )
132
119
}
133
120
}
134
-
135
- err = execCmd ("" , "diff" , []string {clonedDir , localDir })
136
-
137
- if err == nil {
138
- glog .V (100 ).Infof ("Repo synced. Revert local files to original state" )
139
-
140
- return true
141
- }
142
-
143
- return false
144
121
}
145
122
146
- func syncDirectories (clonedDir , localDir string , repo * repo ) {
147
- glog .V (100 ).Infof ("Repos are not synced. Cleaning local directory: %s" , localDir )
148
-
149
- if os .RemoveAll (localDir ) != nil {
150
- glog .V (100 ).Infof ("Failed to remove local api directory. Exit with error code 1" )
151
- os .Exit (1 )
152
- }
153
-
154
- glog .V (100 ).Infof ("Create new local directory: %s" , localDir )
123
+ func copyClonedToLocal (clonedDir , localDir string ) {
124
+ glog .V (100 ).Infof ("Create path to new local directory: %s" , localDir )
155
125
126
+ // We use MkdirAll to make sure the path leading up to localDir exists.
156
127
if os .MkdirAll (localDir , 0750 ) != nil {
157
- glog .V (100 ).Infof ("Failed to recreate api directory. Exit with error code 1" )
128
+ glog .V (100 ).Info ("Failed to create local directory. Exit with error code 1" )
158
129
os .Exit (1 )
159
130
}
160
131
161
- if len (repo .Excludes ) > 0 {
162
- glog .V (100 ).Infof ("Remove excluded files under %s" ,
163
- path .Base (clonedDir ))
164
-
165
- err := excludeFiles (clonedDir , repo .Excludes ... )
166
- if err != nil {
167
- glog .V (100 ).Infof ("Failed to remove excluded files due to %w. Exit with error 1" , err )
168
- os .Exit (1 )
169
- }
170
- }
171
-
172
- glog .V (100 ).Infof ("Copy api filed from cloned directory to local api directory" )
173
-
174
- err := execCmd (
175
- "" ,
176
- "cp" ,
177
- []string {"-a" , fmt .Sprintf ("%s/." , clonedDir ), fmt .Sprintf ("%s/" , localDir )})
178
-
132
+ // We use RemoveAll to delete just localDir but not the path leading to it.
133
+ err := os .RemoveAll (localDir )
179
134
if err != nil {
180
- glog .Infof ("Failed to sync directories . Exit with error code 1" )
135
+ glog .V ( 100 ). Infof ("Failed to remove old local directory %s due to %w . Exit with error 1" , localDir , err )
181
136
os .Exit (1 )
182
137
}
183
138
184
- glog .V (100 ).Infof ("Fix packages names" )
185
-
186
- err = refactor (
187
- fmt .Sprintf ("package %s" , path .Base (clonedDir )),
188
- fmt .Sprintf ("package %s" , path .Base (localDir )),
189
- localDir ,
190
- "*.go" )
191
-
139
+ err = execCmd ("" , "cp" , []string {"-a" , clonedDir , localDir })
192
140
if err != nil {
193
- glog .V (100 ).Infof ("Failed to refactor file . Exit with error 1" )
141
+ glog .V (100 ).Infof ("Failed to sync directories due to %w . Exit with error 1" , err )
194
142
os .Exit (1 )
195
143
}
196
-
197
- for _ , importMap := range repo .ReplaceImports {
198
- err = refactor (importMap ["old" ], importMap ["new" ], localDir , "*.go" )
199
-
200
- if err != nil {
201
- glog .V (100 ).Infof ("Failed to refactor file. Exit with error 1" )
202
- os .Exit (1 )
203
- }
204
- }
205
- }
206
-
207
- func gitReset (packageName string ) {
208
- for _ , cmdToRun := range [][]string {
209
- {"reset" , "--" , fmt .Sprintf ("./pkg/%s" , packageName )},
210
- {"checkout" , "--" , fmt .Sprintf ("./pkg/%s" , packageName )},
211
- {"clean" , "-d" , "-f" , fmt .Sprintf ("./pkg/%s" , packageName )},
212
- } {
213
- err := execCmd ("" , "git" , cmdToRun )
214
-
215
- if err != nil {
216
- glog .Infof ("Failed to reset project to it's original state. Exit with error 1" )
217
- os .Exit (1 )
218
- }
219
- }
220
144
}
221
145
222
146
func gitClone (localPath string , repo * repo ) {
0 commit comments