@@ -15,8 +15,22 @@ import (
15
15
"github.com/pkg/errors"
16
16
)
17
17
18
- // FindBuildDirectory locates the target build directory.
19
- func FindBuildDirectory () (string , bool , error ) {
18
+ // BuildDirectory function locates the target build directory. If the directory doesn't exist, it will create it.
19
+ func BuildDirectory () (string , error ) {
20
+ buildDir , found , err := findBuildDirectory ()
21
+ if err != nil {
22
+ return "" , errors .Wrap (err , "locating build directory failed" )
23
+ }
24
+ if ! found {
25
+ buildDir , err = createBuildDirectory ()
26
+ if err != nil {
27
+ return "" , errors .Wrap (err , "creating new build directory failed" )
28
+ }
29
+ }
30
+ return buildDir , nil
31
+ }
32
+
33
+ func findBuildDirectory () (string , bool , error ) {
20
34
workDir , err := os .Getwd ()
21
35
if err != nil {
22
36
return "" , false , errors .Wrap (err , "locating working directory failed" )
@@ -38,15 +52,15 @@ func FindBuildDirectory() (string, bool, error) {
38
52
return "" , false , nil
39
53
}
40
54
41
- // MustFindBuildPackagesDirectory function locates the target build directory for packages.
55
+ // BuildPackagesDirectory function locates the target build directory for packages.
42
56
// If the directories path doesn't exist, it will create it.
43
- func MustFindBuildPackagesDirectory (packageRoot string ) (string , error ) {
57
+ func BuildPackagesDirectory (packageRoot string ) (string , error ) {
44
58
buildDir , found , err := FindBuildPackagesDirectory ()
45
59
if err != nil {
46
60
return "" , errors .Wrap (err , "locating build directory failed" )
47
61
}
48
62
if ! found {
49
- buildDir , err = createBuildPackagesDirectory ()
63
+ buildDir , err = createBuildDirectory ( "integrations" ) // TODO add support for other package types
50
64
if err != nil {
51
65
return "" , errors .Wrap (err , "creating new build directory failed" )
52
66
}
@@ -61,7 +75,7 @@ func MustFindBuildPackagesDirectory(packageRoot string) (string, error) {
61
75
62
76
// FindBuildPackagesDirectory function locates the target build directory for packages.
63
77
func FindBuildPackagesDirectory () (string , bool , error ) {
64
- buildDir , found , err := FindBuildDirectory ()
78
+ buildDir , found , err := findBuildDirectory ()
65
79
if err != nil {
66
80
return "" , false , err
67
81
}
@@ -86,7 +100,7 @@ func FindBuildPackagesDirectory() (string, bool, error) {
86
100
87
101
// BuildPackage function builds the package.
88
102
func BuildPackage (packageRoot string ) (string , error ) {
89
- destinationDir , err := MustFindBuildPackagesDirectory (packageRoot )
103
+ destinationDir , err := BuildPackagesDirectory (packageRoot )
90
104
if err != nil {
91
105
return "" , errors .Wrap (err , "locating build directory for package failed" )
92
106
}
@@ -118,7 +132,7 @@ func BuildPackage(packageRoot string) (string, error) {
118
132
return destinationDir , nil
119
133
}
120
134
121
- func createBuildPackagesDirectory ( ) (string , error ) {
135
+ func createBuildDirectory ( dirs ... string ) (string , error ) {
122
136
workDir , err := os .Getwd ()
123
137
if err != nil {
124
138
return "" , errors .Wrap (err , "locating working directory failed" )
@@ -129,7 +143,11 @@ func createBuildPackagesDirectory() (string, error) {
129
143
path := filepath .Join (dir , ".git" )
130
144
fileInfo , err := os .Stat (path )
131
145
if err == nil && fileInfo .IsDir () {
132
- buildDir := filepath .Join (dir , "build" , "integrations" ) // TODO add support for other package types
146
+ p := []string {dir , "build" }
147
+ if len (dirs ) > 0 {
148
+ p = append (p , dirs ... )
149
+ }
150
+ buildDir := filepath .Join (p ... )
133
151
err = os .MkdirAll (buildDir , 0755 )
134
152
if err != nil {
135
153
return "" , errors .Wrapf (err , "mkdir failed (path: %s)" , buildDir )
0 commit comments