/
finder.go
130 lines (117 loc) · 3.03 KB
/
finder.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// Copyright 2020 Fairwinds
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License
package finder
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"k8s.io/klog"
"github.com/fairwindsops/pluto/v3/pkg/api"
)
// Dir is the finder dirlication
type Dir struct {
RootPath string
FileList []string
Instance *api.Instance
}
// NewFinder returns a new struct with config portions complete.
func NewFinder(path string, instance *api.Instance) *Dir {
cfg := &Dir{
Instance: instance,
}
if path == "" {
cwd, err := os.Getwd()
if err != nil {
klog.Fatal(err)
}
cfg.RootPath = cwd
} else {
cfg.RootPath = path
}
return cfg
}
// FindVersions runs the finder. This will populate the
// dir struct with any files that might be versioned.
func (dir *Dir) FindVersions() error {
err := dir.listFiles()
if err != nil {
return err
}
err = dir.scanFiles()
if err != nil {
return err
}
if dir.Instance.Outputs != nil {
for _, file := range dir.Instance.Outputs {
klog.V(6).Infof("%s - %s - %s", file.APIVersion.DeprecatedIn, file.APIVersion.Name, file.Name)
}
}
return nil
}
// listFiles gets a list of all the files in the directory.
func (dir *Dir) listFiles() error {
var files []string
if _, err := os.Stat(dir.RootPath); os.IsNotExist(err) {
return fmt.Errorf("specified path does not exist")
}
err := filepath.Walk(dir.RootPath, func(path string, info os.FileInfo, err error) error {
if !info.IsDir() {
files = append(files, path)
}
return nil
})
if err != nil {
return err
}
dir.FileList = files
return nil
}
// scanFiles loops through the file list and finds versioned files
// to add to the dir struct
func (dir *Dir) scanFiles() error {
for _, file := range dir.FileList {
klog.V(8).Infof("processing file: %s", file)
apiFile, err := dir.CheckForAPIVersion(file)
if err != nil {
klog.V(2).Infof("error scanning file %s: %s", file, err.Error())
}
if apiFile != nil {
dir.Instance.Outputs = append(dir.Instance.Outputs, apiFile...)
}
}
return nil
}
// CheckForAPIVersion checks a filename to see if
// it is an api-versioned Kubernetes object.
// Returns the File object if it is.
func (dir *Dir) CheckForAPIVersion(file string) ([]*api.Output, error) {
data, err := ioutil.ReadFile(file)
if err != nil {
return nil, err
}
outputs, err := dir.Instance.IsVersioned(data)
if err != nil {
return nil, err
}
cwd, err := os.Getwd()
if err != nil {
return nil, err
}
filePath := fmt.Sprintf("%s/%s", cwd, file)
for _, output := range outputs {
output.FilePath = filePath
}
return outputs, nil
}