-
Notifications
You must be signed in to change notification settings - Fork 427
/
Copy pathconfig.go
106 lines (95 loc) · 2.47 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
package sdk
import (
"log"
"os"
"path/filepath"
"github.com/pelletier/go-toml/v2"
"github.com/snowflakedb/gosnowflake"
)
func DefaultConfig() *gosnowflake.Config {
config, err := ProfileConfig("default")
if err != nil || config == nil {
log.Printf("[DEBUG] No Snowflake config file found, returning empty config: %v\n", err)
config = &gosnowflake.Config{}
}
return config
}
func ProfileConfig(profile string) (*gosnowflake.Config, error) {
configs, err := loadConfigFile()
if err != nil {
return nil, err
}
if profile == "" {
profile = "default"
}
var config *gosnowflake.Config
if cfg, ok := configs[profile]; ok {
log.Printf("[DEBUG] loading config for profile: \"%s\"", profile)
config = cfg
}
if config == nil {
log.Printf("[DEBUG] no config found for profile: \"%s\"", profile)
return nil, nil
}
// us-west-2 is Snowflake's default region, but if you actually specify that it won't trigger the default code
// https://github.com/snowflakedb/gosnowflake/blob/52137ce8c32eaf93b0bd22fc5c7297beff339812/dsn.go#L61
if config.Region == "us-west-2" {
config.Region = ""
}
return config, nil
}
func MergeConfig(baseConfig *gosnowflake.Config, mergeConfig *gosnowflake.Config) *gosnowflake.Config {
if baseConfig == nil {
return mergeConfig
}
if baseConfig.Account == "" {
baseConfig.Account = mergeConfig.Account
}
if baseConfig.User == "" {
baseConfig.User = mergeConfig.User
}
if baseConfig.Password == "" {
baseConfig.Password = mergeConfig.Password
}
if baseConfig.Role == "" {
baseConfig.Role = mergeConfig.Role
}
if baseConfig.Region == "" {
baseConfig.Region = mergeConfig.Region
}
if baseConfig.Host == "" {
baseConfig.Host = mergeConfig.Host
}
return baseConfig
}
func configFile() (string, error) {
// has the user overwridden the default config path?
if configPath, ok := os.LookupEnv("SNOWFLAKE_CONFIG_PATH"); ok {
if configPath != "" {
return configPath, nil
}
}
dir, err := os.UserHomeDir()
if err != nil {
return "", err
}
// default config path is ~/.snowflake/config.
return filepath.Join(dir, ".snowflake", "config"), nil
}
func loadConfigFile() (map[string]*gosnowflake.Config, error) {
path, err := configFile()
if err != nil {
return nil, err
}
dat, err := os.ReadFile(path)
if err != nil {
return nil, err
}
var s map[string]*gosnowflake.Config
err = toml.Unmarshal(dat, &s)
if err != nil {
log.Printf("[DEBUG] error unmarshalling config file: %v\n", err)
return nil, nil
}
return s, nil
}