/
configuration.go
97 lines (83 loc) · 2.13 KB
/
configuration.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
package ncloud
import (
"bufio"
"github.com/NaverCloudPlatform/ncloud-sdk-go-v2/ncloud/credentials"
"log"
"net/http"
"os"
"os/user"
"path/filepath"
"strings"
)
type APIKey struct {
AccessKey string
SecretKey string
}
type Configuration struct {
BasePath string `json:"basePath,omitempty"`
Host string `json:"host,omitempty"`
Scheme string `json:"scheme,omitempty"`
DefaultHeader map[string]string `json:"defaultHeader,omitempty"`
UserAgent string `json:"userAgent,omitempty"`
HTTPClient *http.Client
APIKey *APIKey
Credentials *credentials.Credentials
}
// Keys This is for backward compatibility. Will be deprecated soon.
func Keys() *APIKey {
apiKey := &APIKey{
AccessKey: "",
SecretKey: "",
}
usr, err := user.Current()
if err != nil {
log.Fatal(err)
return nil
}
if usr.HomeDir == "" {
log.Fatal("use.HomeDir is nil")
return nil
}
configureFile := filepath.Join(usr.HomeDir, ".ncloud", "configure")
file, err := os.Open(configureFile)
if err != nil {
log.Fatal(err)
return nil
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
s := strings.Split(line, "=")
switch strings.TrimSpace(s[0]) {
case "ncloud_access_key_id":
apiKey.AccessKey = strings.TrimSpace(s[1])
case "ncloud_secret_access_key":
apiKey.SecretKey = strings.TrimSpace(s[1])
}
}
if err := scanner.Err(); err != nil {
log.Fatal(err)
return nil
}
return apiKey
}
func (c *Configuration) AddDefaultHeader(key string, value string) {
c.DefaultHeader[key] = value
}
func (c *Configuration) GetCredentials() *credentials.Credentials {
if c.ValidCredentials() {
return c.Credentials.Retrieve()
}
return nil
}
func (c *Configuration) ValidCredentials() bool {
return c.Credentials != nil && c.Credentials.Valid()
}
func (c *Configuration) InitCredentials() {
if c.APIKey != nil {
c.Credentials = credentials.NewValueProviderCreds(c.APIKey.AccessKey, c.APIKey.SecretKey)
} else if !c.ValidCredentials() {
c.Credentials = credentials.LoadCredentials(credentials.DefaultCredentialsChain())
}
}