/
init.go
73 lines (61 loc) · 1.92 KB
/
init.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
package config
import (
"fmt"
"os"
"github.com/taskcluster/taskcluster/v26/clients/client-shell/client"
)
var (
// Configuration contains the current configuration values.
Configuration map[string]map[string]interface{}
// OptionsDefinitions is a map of all the OptionDefinitions, by command.
OptionsDefinitions = make(map[string]map[string]OptionDefinition)
// RootURL is the root URL for the desired Taskcluster deployment
rootURL string
// Credentials is the client credentials, if present.
Credentials *client.Credentials
)
// Defer erroring out on a missing RootURL until we actually need one..
func RootURL() string {
if rootURL == "" {
fmt.Fprintln(os.Stderr, "No Root URL specified; set TASKCLUSTER_ROOT_URL")
os.Exit(1)
}
return rootURL
}
// set the root URL -- this is used only for testing
func SetRootURL(newRootURL string) {
rootURL = newRootURL
}
// Setup is to be called from main
// this was originally the init() function
// but we want to make sure all other packages have been initialized
// before calling them, which Load() does
func Setup() {
var err error
// load configuration
Configuration, err = Load()
if err != nil {
fmt.Fprintf(os.Stderr, "failed to load configuration file, error: %s\n", err)
os.Exit(1)
}
// load root URL
rootURL = Configuration["config"]["rootUrl"].(string)
// load credentials
clientID, ok1 := Configuration["config"]["clientId"].(string)
accessToken, ok2 := Configuration["config"]["accessToken"].(string)
if ok1 && ok2 {
certificate, _ := Configuration["config"]["certificate"].(string)
authorizedScopes, _ := Configuration["config"]["authorizedScopes"].([]string)
Credentials = &client.Credentials{
ClientID: clientID,
AccessToken: accessToken,
Certificate: certificate,
AuthorizedScopes: authorizedScopes,
}
return
}
if ok1 || ok2 {
fmt.Fprintln(os.Stderr, "Either ClientID or Access Token not set")
os.Exit(1)
}
}