-
Notifications
You must be signed in to change notification settings - Fork 0
/
aws_config.go
101 lines (86 loc) · 2.89 KB
/
aws_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
// Package config create AWS session and distribute it (alongside other config value) to any package interested.
package config
import (
"context"
"fmt"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"github.com/spf13/viper"
"github.com/thomasduchatelle/dphoto/internal/printer"
"github.com/thomasduchatelle/dphoto/pkg/awssupport/awsfactory"
"github.com/thomasduchatelle/dphoto/pkg/pkgfactory"
"os"
"path"
)
// Listener function is called once config is loaded
type Listener func(Config)
var (
ForcedConfigFile string // ForcedConfigFile is the path to the file of the config to use (instead of defaulting to ./dphoto.yml, $HOME/.dphoto/dphoto.yml, ...)
Environment string // Environment is used as suffix for the config file name.
listeners []Listener
config *viperConfig
)
// Listen registers a Listener that will be invoked when configuration will be provided.
func Listen(listener Listener) {
listeners = append(listeners, listener)
if config != nil {
listener(config)
}
}
// Connect must be called by main function, it dispatches the config to all components requiring it. Set ignite to TRUE to connect to AWS (required for most commands)
func Connect(ignite, createConfigIfNotExist bool) error {
defaultConfigFile := ForcedConfigFile
if ForcedConfigFile == "" {
configFileName := "dphoto"
if Environment != "" {
configFileName = fmt.Sprintf("%s-%s", configFileName, Environment)
}
viper.SetConfigName(configFileName)
viper.AddConfigPath(".")
viper.AddConfigPath("$HOME/.dphoto")
viper.AddConfigPath("/etc/dphoto/")
defaultConfigFile = os.ExpandEnv("$HOME/.dphoto/") + configFileName + ".yaml"
} else {
viper.SetConfigFile(ForcedConfigFile)
}
err := viper.ReadInConfig()
if err != nil {
if _, isFileNotFound := err.(viper.ConfigFileNotFoundError); isFileNotFound && createConfigIfNotExist {
printer.Info("Creating default configuration file: %s", defaultConfigFile)
err = os.MkdirAll(path.Dir(defaultConfigFile), 0600)
if err != nil {
return errors.Wrapf(err, "can't create directory for the config file %s", defaultConfigFile)
}
_, err = os.Create(defaultConfigFile)
if err != nil {
return err
}
// read config is re-run to find the now-created file
return viper.ReadInConfig()
} else {
return err
}
}
pkgfactory.AWSNames = new(ViperAWSName)
if ignite {
ctx := context.TODO()
factory, err := awsfactory.NewAWSFactory(ctx, &awsfactory.StaticConfig{
Region: viper.GetString(AwsRegion),
AccessKeyID: viper.GetString(AwsKey),
SecretAccessKey: viper.GetString(AwsSecret),
Endpoint: viper.GetString(AwsEndpoint),
})
if err != nil {
return err
}
config = &viperConfig{
Viper: viper.GetViper(),
AWSFactory: factory,
}
for _, listener := range listeners {
listener(config)
}
log.Debugf("Config > %d adapters connected", len(listeners))
}
return nil
}