-
Notifications
You must be signed in to change notification settings - Fork 35
/
bootstrap.go
58 lines (47 loc) · 1.46 KB
/
bootstrap.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
package main
import (
"fmt"
"os"
"github.com/elwinar/rambler/log"
"github.com/kelseyhightower/envconfig"
"github.com/urfave/cli"
)
// Bootstrap do the initialization job, and finish by setting the
// `service` global var that will be used by other commands.
func Bootstrap(ctx *cli.Context) error {
return bootstrap(ctx.GlobalString("configuration"), ctx.GlobalString("environment"), ctx.GlobalBool("debug"), ctx.GlobalBool("dry-run"))
}
func bootstrap(configuration, environment string, debug, dryRun bool) (err error) {
logger = log.NewLogger(func(l *log.Logger) {
l.PrintDebug = debug
})
var cfg Configuration
if configuration != DefaultConfiguration || exists(configuration) {
logger.Debug("loading configuration from %s", configuration)
cfg, err = Load(configuration)
if err != nil {
return fmt.Errorf("unable to load configuration from file: %s", err)
}
}
err = envconfig.Process("rambler", &cfg)
if err != nil {
return fmt.Errorf("unable to load configuration from env: %s", err)
}
logger.Debug("loading environment %s", environment)
env, err := cfg.Env(environment)
if err != nil {
return fmt.Errorf("unable to load requested environment: %s", err)
}
logger.Debug("initializing service")
service, err = NewService(env, dryRun)
if err != nil {
return fmt.Errorf("unable to initialize the migration service: %s", err)
}
return nil
}
func exists(file string) bool {
if _, err := os.Stat(file); err == nil {
return true
}
return false
}