-
Notifications
You must be signed in to change notification settings - Fork 122
/
Copy pathconfig.go
117 lines (95 loc) · 2.54 KB
/
config.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
package config
import (
"fmt"
"io/ioutil"
"strings"
log "github.com/sirupsen/logrus"
"os"
cfg "github.com/infinityworks/go-common/config"
)
// Config struct holds all of the runtime confgiguration for the application
type Config struct {
*cfg.BaseConfig
APIURL string
Repositories string
Organisations string
Users string
APITokenEnv string
APITokenFile string
APIToken string
TargetURLs []string
}
// Init populates the Config struct based on environmental runtime configuration
func Init() Config {
ac := cfg.Init()
url := cfg.GetEnv("API_URL", "https://api.github.com")
repos := os.Getenv("REPOS")
orgs := os.Getenv("ORGS")
users := os.Getenv("USERS")
tokenEnv := os.Getenv("GITHUB_TOKEN")
tokenFile := os.Getenv("GITHUB_TOKEN_FILE")
token, err := getAuth(tokenEnv, tokenFile)
scraped, err := getScrapeURLs(url, repos, orgs, users)
if err != nil {
log.Errorf("Error initialising Configuration, Error: %v", err)
}
appConfig := Config{
&ac,
url,
repos,
orgs,
users,
tokenEnv,
tokenFile,
token,
scraped,
}
return appConfig
}
// Init populates the Config struct based on environmental runtime configuration
// All URL's are added to the TargetURL's string array
func getScrapeURLs(apiURL, repos, orgs, users string) ([]string, error) {
urls := []string{}
opts := "?&per_page=100" // Used to set the Github API to return 100 results per page (max)
// User input validation, check that either repositories or organisations have been passed in
if len(repos) == 0 && len(orgs) == 0 && len(users) == 0 {
return urls, fmt.Errorf("No targets specified")
}
// Append repositories to the array
if repos != "" {
rs := strings.Split(repos, ", ")
for _, x := range rs {
y := fmt.Sprintf("%s/repos/%s%s", apiURL, x, opts)
urls = append(urls, y)
}
}
// Append github orginisations to the array
if orgs != "" {
o := strings.Split(orgs, ", ")
for _, x := range o {
y := fmt.Sprintf("%s/orgs/%s/repos%s", apiURL, x, opts)
urls = append(urls, y)
}
}
if users != "" {
us := strings.Split(users, ", ")
for _, x := range us {
y := fmt.Sprintf("%s/users/%s/repos%s", apiURL, x, opts)
urls = append(urls, y)
}
}
return urls, nil
}
// getAuth returns oauth2 token as string for usage in http.request
func getAuth(token string, tokenFile string) (string, error) {
if token != "" {
return token, nil
} else if tokenFile != "" {
b, err := ioutil.ReadFile(tokenFile)
if err != nil {
return "", err
}
return strings.TrimSpace(string(b)), err
}
return "", nil
}