-
Notifications
You must be signed in to change notification settings - Fork 0
/
tools.go
108 lines (95 loc) · 2.07 KB
/
tools.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
package tfsyntax
import (
"context"
"log"
"os"
"path/filepath"
"regexp"
"github.com/google/go-github/v39/github"
"golang.org/x/oauth2"
)
// get all files filterd with ext.
func CheckExt(ext string) []string {
pathS, err := os.Getwd()
if err != nil {
panic(err)
}
var files []string
filepath.WalkDir(pathS, func(path string, d os.DirEntry, _ error) error {
if d.IsDir() && d.Name() == ".terraform" {
return filepath.SkipDir
} else {
r, err := regexp.MatchString(ext, d.Name())
if err == nil && r {
files = append(files, d.Name())
}
}
return nil
})
return files
}
// return bool if list contains a string
func Contains(s []string, str string) bool {
for _, v := range s {
if v == str {
return true
}
}
return false
}
// Create list of keys from a map
func CreateKeyList(except []string, m ...map[string]interface{}) []string {
var listOfKeys []string
for _, v := range m {
for k, _ := range v {
if !Contains(except, k) {
listOfKeys = append(listOfKeys, k)
}
}
}
return listOfKeys
}
// Convert list of interfaces to list of strings.
func ConvertLItoLS(i interface{}) []string {
var output []string
if i != nil {
for _, v := range i.([]interface{}) {
output = append(output, v.(string))
}
}
return output
}
func Tags(pat, owner, repo string) []string {
var tags []string
ctx := context.Background()
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: pat},
)
tc := oauth2.NewClient(ctx, ts)
client := github.NewClient(tc)
// list all repositories for the authenticated user
repoTags, _, err := client.Repositories.ListTags(ctx, owner, repo, nil)
if err != nil {
log.Fatal(err)
}
for _, tag := range repoTags {
tags = append(tags, tag.GetName())
}
return tags
}
func PopElements(slice, subslice []string) []string {
for i := 0; i < len(slice); i++ {
url := slice[i]
for _, rem := range subslice {
if url == rem {
slice = append(slice[:i], slice[i+1:]...)
i--
break
}
}
}
return slice
}
func SupportedVersions(tags, excluded []string) []string {
return PopElements(tags, excluded)
}