-
Notifications
You must be signed in to change notification settings - Fork 69
/
config.go
181 lines (154 loc) · 4.11 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
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
package edgegrid
import (
"fmt"
"os"
"strconv"
"strings"
"github.com/go-ini/ini"
"gopkg.in/mattes/go-expand-tilde.v1"
)
// Config struct provides all the necessary fields to
// create authorization header, debug is optional
type Config struct {
Host string `ini:"host"`
ClientToken string `ini:"client_token"`
ClientSecret string `ini:"client_secret"`
AccessToken string `ini:"access_token"`
HeaderToSign []string `ini:"headers_to_sign"`
MaxBody int `ini:"max_body"`
Debug bool `ini:"debug"`
}
// Init initializes by first attempting to use ENV vars, with .edgerc as a fallback
//
// See: InitEnv()
// See: InitEdgeRc()
func Init(filepath string, section string) (Config, error) {
if section == "" {
section = defaultSection
} else {
section = strings.ToUpper(section)
}
_, exists := os.LookupEnv("AKAMAI_" + section + "_HOST")
if !exists && section == defaultSection {
_, exists := os.LookupEnv("AKAMAI_HOST")
if exists {
return InitEnv("")
}
}
if exists {
return InitEnv(section)
}
c, err := InitEdgeRc(filepath, strings.ToLower(section))
if err == nil {
return c, nil
}
if section != defaultSection {
_, ok := os.LookupEnv("AKAMAI_HOST")
if ok {
return InitEnv("")
}
}
return c, fmt.Errorf("Unable to create instance using environment or .edgerc file")
}
// InitEdgeRc initializes using a configuration file in standard INI format
//
// By default, it uses the .edgerc found in the users home directory, and the
// "default" section.
func InitEdgeRc(filepath string, section string) (Config, error) {
var (
c Config
requiredOptions = []string{"host", "client_token", "client_secret", "access_token"}
missing []string
)
// Check if filepath is empty
if filepath == "" {
filepath = "~/.edgerc"
}
// Check if section is empty
if section == "" {
section = "default"
}
// Tilde seems to be not working when passing ~/.edgerc as file
// Takes current user and use home dir instead
path, err := tilde.Expand(filepath)
if err != nil {
panic(fmt.Errorf(errorMap[ErrHomeDirNotFound], err))
}
edgerc, err := ini.Load(path)
if err != nil {
panic(fmt.Errorf(errorMap[ErrConfigFile], err))
}
err = edgerc.Section(section).MapTo(&c)
if err != nil {
panic(fmt.Errorf(errorMap[ErrConfigFileSection], err))
}
for _, opt := range requiredOptions {
if !(edgerc.Section(section).HasKey(opt)) {
missing = append(missing, opt)
}
}
if len(missing) > 0 {
panic(fmt.Errorf(errorMap[ErrConfigMissingOptions], missing))
}
if c.MaxBody == 0 {
c.MaxBody = 131072
}
return c, nil
}
// InitEnv initializes using the Environment (ENV)
//
// By default, it uses AKAMAI_HOST, AKAMAI_CLIENT_TOKEN, AKAMAI_CLIENT_SECRET,
// AKAMAI_ACCESS_TOKEN, and AKAMAI_MAX_BODY variables.
//
// You can define multiple configurations by prefixing with the section name specified, e.g.
// passing "ccu" will cause it to look for AKAMAI_CCU_HOST, etc.
//
// If AKAMAI_{SECTION} does not exist, it will fall back to just AKAMAI_.
func InitEnv(section string) (Config, error) {
var (
c Config
requiredOptions = []string{"HOST", "CLIENT_TOKEN", "CLIENT_SECRET", "ACCESS_TOKEN"}
missing []string
prefix string
)
// Check if section is empty
if section == "" {
section = defaultSection
} else {
section = strings.ToUpper(section)
}
prefix = "AKAMAI_"
_, ok := os.LookupEnv("AKAMAI_" + section + "_HOST")
if ok {
prefix = "AKAMAI_" + section + "_"
}
for _, opt := range requiredOptions {
val, ok := os.LookupEnv(prefix + opt)
if !ok {
missing = append(missing, prefix+opt)
} else {
switch {
case opt == "HOST":
c.Host = val
case opt == "CLIENT_TOKEN":
c.ClientToken = val
case opt == "CLIENT_SECRET":
c.ClientSecret = val
case opt == "ACCESS_TOKEN":
c.AccessToken = val
}
}
}
if len(missing) > 0 {
return c, fmt.Errorf(errorMap[ErrMissingEnvVariables], missing)
}
c.MaxBody = 0
val, ok := os.LookupEnv(prefix + "MAX_BODY")
if i, err := strconv.Atoi(val); err == nil {
c.MaxBody = i
}
if !ok || c.MaxBody == 0 {
c.MaxBody = 131072
}
return c, nil
}