-
Notifications
You must be signed in to change notification settings - Fork 4
/
dotnet_source_remover.go
51 lines (43 loc) · 1.17 KB
/
dotnet_source_remover.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
package dotnetpublish
import (
"fmt"
"os"
"path/filepath"
"github.com/paketo-buildpacks/packit/v2/fs"
)
type DotnetSourceRemover struct{}
func NewDotnetSourceRemover() DotnetSourceRemover {
return DotnetSourceRemover{}
}
func (m DotnetSourceRemover) Remove(workingDir, publishOutputDir string, excludedFiles ...string) error {
workingDirFiles, err := filepath.Glob(filepath.Join(workingDir, "*"))
if err != nil {
return fmt.Errorf("could not glob %s: %w", filepath.Join(workingDir, "*"), err)
}
for _, file := range workingDirFiles {
protectFile := false
for _, excludedFileName := range excludedFiles {
if filepath.Base(file) != excludedFileName {
continue
}
protectFile = true
}
if !protectFile {
err = os.RemoveAll(file)
if err != nil {
return fmt.Errorf("could not remove %s: %w", file, err)
}
}
}
outputFiles, err := filepath.Glob(filepath.Join(publishOutputDir, "*"))
if err != nil {
return fmt.Errorf("could not glob %s: %w", filepath.Join(publishOutputDir, "*"), err)
}
for _, file := range outputFiles {
err = fs.Move(file, filepath.Join(workingDir, filepath.Base(file)))
if err != nil {
return err
}
}
return nil
}