-
Notifications
You must be signed in to change notification settings - Fork 38
/
config.go
147 lines (124 loc) · 3.17 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
package external
import (
"fmt"
"time"
"github.com/ucloud/ucloud-sdk-go/ucloud"
"github.com/ucloud/ucloud-sdk-go/ucloud/auth"
)
// CredentialProvider is the provider to store and provide credential instance
type CredentialProvider interface {
Credential() *auth.Credential
}
// ClientConfigProvider is the provider to store and provide config instance
type ClientConfigProvider interface {
Config() *ucloud.Config
}
// ConfigProvider is the provider to store and provide config/credential instance
type ConfigProvider interface {
CredentialProvider
ClientConfigProvider
}
// config will read configuration
type config struct {
// Named profile
Profile string
SharedConfigFile string
SharedCredentialFile string
// Credential configuration
PublicKey string
PrivateKey string
SecurityToken string
CanExpire bool
Expires time.Time
// Client configuration
ProjectId string
Zone string
Region string
BaseUrl string
Timeout time.Duration
}
func newConfig() *config {
return &config{}
}
func (c *config) loadEnv() error {
cfg, err := loadEnvConfig()
if err != nil {
return err
}
err = c.merge(cfg)
if err != nil {
return err
}
return nil
}
func (c *config) loadFileIfExist() error {
cfg, err := loadSharedConfigFile(
c.SharedConfigFile,
c.SharedCredentialFile,
c.Profile,
)
if err != nil {
return err
}
err = c.merge(cfg)
if err != nil {
return err
}
return nil
}
func (c *config) merge(other *config) error {
if other == nil {
return nil
}
setStringify(&c.Profile, other.Profile)
setStringify(&c.SharedConfigFile, other.SharedConfigFile)
setStringify(&c.SharedCredentialFile, other.SharedCredentialFile)
setStringify(&c.PublicKey, other.PublicKey)
setStringify(&c.PrivateKey, other.PrivateKey)
setStringify(&c.ProjectId, other.ProjectId)
setStringify(&c.Zone, other.Zone)
setStringify(&c.Region, other.Region)
setStringify(&c.BaseUrl, other.BaseUrl)
if other.Timeout != time.Duration(0) {
c.Timeout = other.Timeout
}
return nil
}
// Config is the configuration of ucloud client
func (c *config) Config() *ucloud.Config {
cfg := ucloud.NewConfig()
setStringify(&cfg.ProjectId, c.ProjectId)
setStringify(&cfg.Zone, c.Zone)
setStringify(&cfg.Region, c.Region)
setStringify(&cfg.BaseUrl, c.BaseUrl)
if c.Timeout != time.Duration(0) {
cfg.Timeout = c.Timeout
}
return &cfg
}
// Credential is the configuration of ucloud authorization information
func (c *config) Credential() *auth.Credential {
cred := auth.NewCredential()
setStringify(&cred.PublicKey, c.PublicKey)
setStringify(&cred.PrivateKey, c.PrivateKey)
setStringify(&cred.SecurityToken, c.SecurityToken)
cred.CanExpire = c.CanExpire
cred.Expires = c.Expires
return &cred
}
// LoadDefaultUCloudConfig is the default loader to load config
func LoadDefaultUCloudConfig() (ConfigProvider, error) {
cfg := newConfig()
if err := cfg.loadEnv(); err != nil {
return nil, fmt.Errorf("error on loading env, %s", err)
}
if err := cfg.loadFileIfExist(); err != nil {
return nil, fmt.Errorf("error on loading shared config file, %s", err)
}
return cfg, nil
}
func setStringify(p *string, s string) {
if p != nil && len(s) != 0 {
*p = s
}
}