-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
311 lines (271 loc) · 6.55 KB
/
main.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
package main
import (
"encoding/json"
"flag"
"fmt"
"github.com/hashicorp/hcl"
"github.com/mbndr/logo"
"github.com/olekukonko/tablewriter"
. "gopkg.in/src-d/go-git.v4/plumbing/transport"
"io/ioutil"
"os"
"path/filepath"
"reflect"
"regexp"
"sync"
"time"
)
var log = logo.NewSimpleLogger(os.Stderr, logo.INFO, "versionGetter ", true)
var StatsInfo = NewStats()
var files []string
var config Config
var wg sync.WaitGroup
type arrayFlags []string
func (i *arrayFlags) String() string {
return "my string representation"
}
func (i *arrayFlags) Set(value string) error {
*i = append(*i, value)
return nil
}
type ListFileData struct {
Data []FileData
}
type Module struct {
Name string
Type string
Source string
Version string
Path string
gitInfo GitInfo
}
type FileData struct {
Name string
Path string
Modules []Module
}
func (s *Stats) addFile() {
s.Files++
}
func (s *Stats) addModule() {
s.Modules++
}
func NewStats() *Stats {
return &Stats{StartTime: time.Now()}
}
func (s *Stats) Report() string {
return fmt.Sprintf("Number of Files: %v. Number of Modules %v", s.Files, s.Modules)
}
type FileContents struct {
Item []map[string][]struct {
Source string `hcl:"source" json:"source"`
Version string `hcl:"version" json:"version"`
} `hcl:"module" json:"module"`
}
func parsePayload(paylaod []byte) (FileContents, error) {
var elem = FileContents{}
err := hcl.Unmarshal(paylaod, &elem)
if err != nil {
return FileContents{}, err
}
return elem, nil
}
type Config struct {
SrcPath string
Verbose bool
Stats bool
ExtraSkip arrayFlags
Output string
}
func parseSrc(source string) *Endpoint {
endpoint, err := NewEndpoint(source)
if err != nil {
panic(err)
}
return endpoint
}
type GitInfo struct {
Owner string
Repository string
Ref string
SubPath string
}
func SplitGitUrl(str string) GitInfo {
regStr := `(?P<owner>[\w\d]+)\/(?P<reponame>[\w\d]+.git)\??(ref=(?P<ref>((\d+\.)?(\d+\.)?(\*|\d+))|[\w\d]+))?(\/\/(?P<path>[\w\d]+))?`
regOperator, err := regexp.Compile(regStr)
if err != nil {
panic(err)
}
match := regOperator.FindStringSubmatch(str)
result := make(map[string]string)
for i, name := range regOperator.SubexpNames() {
if i != 0 && name != "" {
result[name] = match[i]
}
}
gitInfo := GitInfo{Owner: result["owner"],
Repository: result["reponame"],
Ref: result["ref"],
SubPath: result["path"]}
log.Debug(gitInfo)
return gitInfo
}
func processFile(path string) (FileData, error) {
fileContents, _ := ioutil.ReadFile(path)
fileData := FileData{Name: filepath.Base(path),
Path: path}
log.Debugf(fmt.Sprintf("Processing file %s", path))
StatsInfo.addFile()
element, err := parsePayload(fileContents)
if err != nil {
return FileData{}, err
}
for _, item := range element.Item {
if len(item) >= 1 {
for k, v := range item {
StatsInfo.addModule()
thisMod := Module{Name: k, Source: v[0].Source, Version: v[0].Version}
endpoint := parseSrc(thisMod.Source)
thisMod.Type = endpoint.Protocol
switch endpoint.Protocol {
case "ssh":
thisMod.gitInfo = SplitGitUrl(endpoint.Path)
thisMod.Version = thisMod.gitInfo.Ref
thisMod.Path = thisMod.gitInfo.SubPath
thisMod.Source = thisMod.gitInfo.Repository
case "file":
if thisMod.Version != "" {
thisMod.Type = "registry"
}
thisMod.Path = endpoint.Path
}
fileData.Modules = append(fileData.Modules, thisMod)
}
}
}
return fileData, nil
}
type Stats struct {
StartTime time.Time
EndTime time.Time
Files int
Modules int
Duration time.Duration
}
func (s *Stats) Stop() {
s.EndTime = time.Now()
s.Duration = s.EndTime.Sub(s.StartTime)
}
func listFiles(path string, f os.FileInfo, err error) error {
isToSkip, _ := in_array(f.Name(), []string{".git", ".terraform"})
log.Debugf("Processing %s", f.Name())
if f.IsDir() && isToSkip {
log.Debugf("Skipping dir %s", f.Name())
return filepath.SkipDir
}
if filepath.Ext(path) == ".tf" {
files = append(files, path)
}
return nil
}
func in_array(val interface{}, array interface{}) (exists bool, index int) {
exists = false
index = -1
switch reflect.TypeOf(array).Kind() {
case reflect.Slice:
s := reflect.ValueOf(array)
for i := 0; i < s.Len(); i++ {
if reflect.DeepEqual(val, s.Index(i).Interface()) == true {
index = i
exists = true
return
}
}
}
return
}
func main() {
config.Output = "text"
// Add basic folders to exlude
config.ExtraSkip.Set(".git")
config.ExtraSkip.Set(".terraform")
flag.StringVar(&config.SrcPath, "source-path", "", "The path to the source directory to scan")
flag.BoolVar(&config.Verbose, "verbose", false, "be verbose")
flag.BoolVar(&config.Stats, "stats", false, "Show stats")
flag.Var(&config.ExtraSkip, "extra-skip", "Extra skip dirs")
flag.StringVar(&config.Output, "output", "text", "output format (text|json)")
flag.Parse()
if config.Verbose {
log.SetLevel(logo.DEBUG)
}
var Data ListFileData
err := filepath.Walk(config.SrcPath, listFiles)
//c := make(chan error)
//go func() { c <- filepath.Walk(config.SrcPath, listFiles) }()
//err := <-c // Walk done, check the error
if err != nil {
panic(err)
}
if config.SrcPath == "" {
flag.Usage()
os.Exit(1)
} else {
search := fmt.Sprintf("%s", config.SrcPath)
log.Infof("search in %s", search)
//paths, err := filepath.Glob(search)
if err != nil {
panic(err)
}
for _, file := range files {
fileData, err := processFile(file)
if err != nil {
panic(err)
}
if fileData.Modules != nil {
Data.Data = append(Data.Data, fileData)
}
}
}
StatsInfo.Stop()
// Cleanup
switch config.Output {
case "text":
Data.TableWriter()
case "json":
DisplayElement(Data)
}
//DisplayElement(Data)
if config.Stats {
log.Infof(StatsInfo.Report())
log.Infof("Took %s", StatsInfo.Duration)
}
}
func (ld *ListFileData) TableWriter() {
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader(ld.Header())
for _, v := range ld.PrepareTabulare() {
table.Append(v)
}
table.Render() // Send output
}
func (ld *ListFileData) Header() []string {
return []string{"File", "Module", "Type", "Source", "Version"}
}
func (ld *ListFileData) PrepareTabulare() [][]string {
var output [][]string
for _, item := range ld.Data {
for _, v := range item.Modules {
add := []string{item.Path, v.Name, v.Type, v.Source, v.Version}
output = append(output, add)
}
}
return output
}
func DisplayElement(data ListFileData) {
// Process Elems
jsonPayload, err := json.MarshalIndent(data.Data, "", " ")
if err != nil {
panic(err)
}
fmt.Println(string(jsonPayload))
}