-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
147 lines (113 loc) · 2.69 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
package main
import (
"bufio"
"fmt"
"io/ioutil"
"log"
"os"
"regexp"
"strings"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing/transport/http"
"gopkg.in/yaml.v3"
)
func readCredentials(file string) map[interface{}]interface{} {
yfile, err := ioutil.ReadFile(file)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Reading credentials from %s\n", file)
data := make(map[interface{}]interface{})
err2 := yaml.Unmarshal(yfile, &data)
if err2 != nil {
log.Fatal(err2)
}
return data
}
func findImages(dir string, repoImages map[string]void) {
file, err := os.Open(dir + "/.drone.yml")
var stepS390xFlag bool = false
reImage := regexp.MustCompile(`image:[a-zA-Z \/:0-9.-]*`)
reStep := regexp.MustCompile(`^name: `)
reStepS390x := regexp.MustCompile(`^name: [a-zA-Z \/:0-9.-]*s390x`)
if err != nil {
fmt.Println(err)
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
text := scanner.Text()
step := reStep.FindAll([]byte(text), -1)
if len(step) > 0 {
stepS390x := reStepS390x.FindAll([]byte(text), -1)
if len(stepS390x) > 0 {
stepS390xFlag = true
} else {
stepS390xFlag = false
}
}
if stepS390xFlag {
found := reImage.FindAll([]byte(text), -1)
if len(found) > 0 {
fmt.Printf("\t%q\n", found[0])
repoImages[string(found[0])] = member
}
}
}
if err := scanner.Err(); err != nil {
fmt.Println(err)
}
}
func clone(repo string, credentials map[interface{}]interface{}) string {
dirname := strings.Split(repo, "/")
dir, err := ioutil.TempDir("./temp/", dirname[len(dirname)-1])
if err != nil {
log.Fatal(err)
}
fmt.Printf("Repo %s\n", repo)
_, err = git.PlainClone(dir, false, &git.CloneOptions{
URL: repo,
Auth: &http.BasicAuth{
Username: credentials["user"].(string),
Password: credentials["pwd"].(string),
},
Depth: 1,
})
if err != nil {
log.Fatal(err)
}
return dir
}
func getRepos(reposFile string) []string {
file, err := os.Open(reposFile)
repoList := []string{}
fmt.Println("Fetching the repos to analyze")
if err != nil {
fmt.Println(err)
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
repoList = append(repoList, scanner.Text())
}
if err := scanner.Err(); err != nil {
fmt.Println(err)
}
return repoList
}
type void struct{}
var member void
func main() {
repoImages := make(map[string]void)
repoList := getRepos("repositories.txt")
credentials := readCredentials("credentials.yaml")
for _, repo := range repoList {
dir := clone(repo, credentials)
findImages(dir, repoImages)
defer os.RemoveAll(dir)
}
fmt.Println("\nUnique images used in all the repos:")
for k := range repoImages {
fmt.Println(k)
}
}