@@ -17,14 +17,11 @@ limitations under the License.
17
17
package initializer
18
18
19
19
import (
20
- "fmt"
21
20
"path/filepath"
22
21
"sort"
23
22
24
23
"github.com/karrick/godirwalk"
25
- "github.com/sirupsen/logrus"
26
24
27
- "github.com/GoogleContainerTools/skaffold/pkg/skaffold/initializer/kubectl"
28
25
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/util"
29
26
)
30
27
@@ -34,86 +31,9 @@ type analysis struct {
34
31
builderAnalyzer * builderAnalyzer
35
32
}
36
33
37
- // analyzer is a generic Visitor that is called on every file in the directory
38
- // It can manage state and react to walking events assuming a bread first search
39
- type analyzer interface {
40
- enterDir (dir string )
41
- analyzeFile (file string ) error
42
- exitDir (dir string )
43
- }
44
-
45
- type directoryAnalyzer struct {
46
- currentDir string
47
- }
48
-
49
- func (a * directoryAnalyzer ) analyzeFile (filePath string ) error {
50
- return nil
51
- }
52
-
53
- func (a * directoryAnalyzer ) enterDir (dir string ) {
54
- a .currentDir = dir
55
- }
56
-
57
- func (a * directoryAnalyzer ) exitDir (dir string ) {
58
- //pass
59
- }
60
-
61
- type kubectlAnalyzer struct {
62
- directoryAnalyzer
63
- kubernetesManifests []string
64
- }
65
-
66
- func (a * kubectlAnalyzer ) analyzeFile (filePath string ) error {
67
- if kubectl .IsKubernetesManifest (filePath ) && ! IsSkaffoldConfig (filePath ) {
68
- a .kubernetesManifests = append (a .kubernetesManifests , filePath )
69
- }
70
- return nil
71
- }
72
-
73
- type skaffoldConfigAnalyzer struct {
74
- directoryAnalyzer
75
- force bool
76
- }
77
-
78
- func (a * skaffoldConfigAnalyzer ) analyzeFile (filePath string ) error {
79
- if ! IsSkaffoldConfig (filePath ) {
80
- return nil
81
- }
82
- if ! a .force {
83
- return fmt .Errorf ("pre-existing %s found (you may continue with --force)" , filePath )
84
- }
85
- logrus .Debugf ("%s is a valid skaffold configuration: continuing since --force=true" , filePath )
86
- return nil
87
- }
88
-
89
- type builderAnalyzer struct {
90
- directoryAnalyzer
91
- enableJibInit bool
92
- enableBuildpackInit bool
93
- findBuilders bool
94
- foundBuilders []InitBuilder
95
-
96
- parentDirToStopFindBuilders string
97
- }
98
-
99
- func (a * builderAnalyzer ) analyzeFile (filePath string ) error {
100
- if a .findBuilders && (a .parentDirToStopFindBuilders == "" || a .parentDirToStopFindBuilders == a .currentDir ) {
101
- builderConfigs , continueSearchingBuilders := detectBuilders (a .enableJibInit , a .enableBuildpackInit , filePath )
102
- a .foundBuilders = append (a .foundBuilders , builderConfigs ... )
103
- if ! continueSearchingBuilders {
104
- a .parentDirToStopFindBuilders = a .currentDir
105
- }
106
- }
107
- return nil
108
- }
109
-
110
- func (a * builderAnalyzer ) exitDir (dir string ) {
111
- if a .parentDirToStopFindBuilders == dir {
112
- a .parentDirToStopFindBuilders = ""
113
- }
114
- }
115
-
116
- // analyze recursively walks a directory and returns the k8s configs and builder configs that it finds
34
+ // analyze recursively walks a directory and notifies the analyzers of files and enterDir and exitDir events
35
+ // at the end of the analyze function the analysis struct's analyzers should contain the state that we can
36
+ // use to do further computation.
117
37
func (a * analysis ) analyze (dir string ) error {
118
38
for _ , analyzer := range a .analyzers () {
119
39
analyzer .enterDir (dir )
@@ -168,3 +88,31 @@ func (a *analysis) analyzers() []analyzer {
168
88
a .builderAnalyzer ,
169
89
}
170
90
}
91
+
92
+ // analyzer is following the visitor pattern. It is called on every file
93
+ // as the analysis.analyze function walks the directory structure recursively.
94
+ // It can manage state and react to walking events assuming a breadth first search.
95
+ type analyzer interface {
96
+ enterDir (dir string )
97
+ analyzeFile (file string ) error
98
+ exitDir (dir string )
99
+ }
100
+
101
+ // directoryAnalyzer is a base analyzer that can be included in every analyzer as a convenience
102
+ // it saves the current directory on enterDir events. Benefits to include this into other analyzers is that
103
+ // they can rely on the current directory var, but also they don't have to implement enterDir and exitDir.
104
+ type directoryAnalyzer struct {
105
+ currentDir string
106
+ }
107
+
108
+ func (a * directoryAnalyzer ) analyzeFile (filePath string ) error {
109
+ return nil
110
+ }
111
+
112
+ func (a * directoryAnalyzer ) enterDir (dir string ) {
113
+ a .currentDir = dir
114
+ }
115
+
116
+ func (a * directoryAnalyzer ) exitDir (dir string ) {
117
+ //pass
118
+ }
0 commit comments