Skip to content
This repository was archived by the owner on Sep 9, 2020. It is now read-only.

Commit f0ff2f5

Browse files
committed
internal/gps: update stripVendor functions
Signed-off-by: Ibrahim AshShohail <[email protected]>
1 parent 5918de8 commit f0ff2f5

File tree

3 files changed

+49
-59
lines changed

3 files changed

+49
-59
lines changed

internal/gps/prune.go

+2-10
Original file line numberDiff line numberDiff line change
@@ -218,11 +218,7 @@ func pruneNonGoFiles(baseDir string, logger *log.Logger) error {
218218
return errors.Wrap(err, "could not prune non-Go files")
219219
}
220220

221-
if err := deleteFiles(files); err != nil {
222-
return err
223-
}
224-
225-
return nil
221+
return deleteFiles(files)
226222
}
227223

228224
// calculateNonGoFiles returns a list of all non-Go files within baseDir.
@@ -283,11 +279,7 @@ func pruneGoTestFiles(baseDir string, logger *log.Logger) error {
283279
return errors.Wrap(err, "could not prune Go test files")
284280
}
285281

286-
if err := deleteFiles(files); err != nil {
287-
return err
288-
}
289-
290-
return nil
282+
return deleteFiles(files)
291283
}
292284

293285
// calculateGoTestFiles walks over baseDir and returns a list of all

internal/gps/strip_vendor.go

+16-18
Original file line numberDiff line numberDiff line change
@@ -16,29 +16,27 @@ func stripVendor(path string, info os.FileInfo, err error) error {
1616
return err
1717
}
1818

19-
if info.Name() == "vendor" {
20-
if _, err := os.Lstat(path); err != nil {
19+
// Skip anything not named vendor
20+
if info.Name() != "vendor" {
21+
return nil
22+
}
23+
24+
// If the file is a symlink to a directory, delete the symlink.
25+
if (info.Mode() & os.ModeSymlink) != 0 {
26+
realInfo, err := os.Stat(path)
27+
if err != nil {
2128
return err
2229
}
23-
24-
if (info.Mode() & os.ModeSymlink) != 0 {
25-
realInfo, err := os.Stat(path)
26-
if err != nil {
27-
return err
28-
}
29-
if realInfo.IsDir() {
30-
return os.Remove(path)
31-
}
30+
if realInfo.IsDir() {
31+
return os.Remove(path)
3232
}
33+
}
3334

34-
if info.IsDir() {
35-
if err := removeAll(path); err != nil {
36-
return err
37-
}
38-
return filepath.SkipDir
35+
if info.IsDir() {
36+
if err := removeAll(path); err != nil {
37+
return err
3938
}
40-
41-
return nil
39+
return filepath.SkipDir
4240
}
4341

4442
return nil

internal/gps/strip_vendor_windows.go

+31-31
Original file line numberDiff line numberDiff line change
@@ -14,38 +14,38 @@ func stripVendor(path string, info os.FileInfo, err error) error {
1414
return err
1515
}
1616

17-
if info.Name() == "vendor" {
18-
if _, err := os.Lstat(path); err == nil {
19-
symlink := (info.Mode() & os.ModeSymlink) != 0
20-
dir := info.IsDir()
21-
22-
switch {
23-
case symlink && dir:
24-
// This could be a windows junction directory. Support for these in the
25-
// standard library is spotty, and we could easily delete an important
26-
// folder if we called os.Remove or os.RemoveAll. Just skip these.
27-
//
28-
// TODO: If we could distinguish between junctions and Windows symlinks,
29-
// we might be able to safely delete symlinks, even though junctions are
30-
// dangerous.
31-
return filepath.SkipDir
32-
33-
case symlink:
34-
realInfo, err := os.Stat(path)
35-
if err != nil {
36-
return err
37-
}
38-
if realInfo.IsDir() {
39-
return os.Remove(path)
40-
}
41-
42-
case dir:
43-
if err := removeAll(path); err != nil {
44-
return err
45-
}
46-
return filepath.SkipDir
47-
}
17+
if info.Name() != "vendor" {
18+
return nil
19+
}
20+
21+
symlink := (info.Mode() & os.ModeSymlink) != 0
22+
dir := info.IsDir()
23+
24+
switch {
25+
case symlink && dir:
26+
// This could be a windows junction directory. Support for these in the
27+
// standard library is spotty, and we could easily delete an important
28+
// folder if we called os.Remove or os.RemoveAll. Just skip these.
29+
//
30+
// TODO: If we could distinguish between junctions and Windows symlinks,
31+
// we might be able to safely delete symlinks, even though junctions are
32+
// dangerous.
33+
return filepath.SkipDir
34+
35+
case symlink:
36+
realInfo, err := os.Stat(path)
37+
if err != nil {
38+
return err
39+
}
40+
if realInfo.IsDir() {
41+
return os.Remove(path)
42+
}
43+
44+
case dir:
45+
if err := removeAll(path); err != nil {
46+
return err
4847
}
48+
return filepath.SkipDir
4949
}
5050

5151
return nil

0 commit comments

Comments
 (0)