-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathinit.go
89 lines (77 loc) · 2.11 KB
/
init.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
package main
import (
"fmt"
"os"
"strings"
"github.com/hashicorp/nomad/api"
"github.com/knadh/koanf"
"github.com/knadh/koanf/parsers/toml"
"github.com/knadh/koanf/providers/env"
"github.com/knadh/koanf/providers/file"
flag "github.com/spf13/pflag"
"github.com/zerodha/logf"
)
// initLogger initializes logger instance.
func initLogger(ko *koanf.Koanf) logf.Logger {
opts := logf.Opts{EnableCaller: true}
if ko.String("app.log_level") == "debug" {
opts.Level = logf.DebugLevel
}
if ko.String("app.env") == "dev" {
opts.EnableColor = true
}
return logf.New(opts)
}
// initConfig loads config to `ko` object.
func initConfig(cfgDefault string, envPrefix string) (*koanf.Koanf, error) {
var (
ko = koanf.New(".")
f = flag.NewFlagSet("front", flag.ContinueOnError)
)
// Configure Flags.
f.Usage = func() {
fmt.Println(f.FlagUsages())
os.Exit(0)
}
// Register `--config` flag.
cfgPath := f.String("config", cfgDefault, "Path to a config file to load.")
// Parse and Load Flags.
err := f.Parse(os.Args[1:])
if err != nil {
return nil, err
}
// Load the config files from the path provided.
err = ko.Load(file.Provider(*cfgPath), toml.Parser())
if err != nil {
return nil, err
}
// Load environment variables if the key is given
// and merge into the loaded config.
if envPrefix != "" {
err = ko.Load(env.Provider(envPrefix, ".", func(s string) string {
return strings.Replace(strings.ToLower(
strings.TrimPrefix(s, envPrefix)), "__", ".", -1)
}), nil)
if err != nil {
return nil, err
}
}
return ko, nil
}
// initNomadClient initialises a Nomad API client.
func initNomadClient() (*api.Client, error) {
client, err := api.NewClient(api.DefaultConfig())
if err != nil {
return nil, err
}
return client, nil
}
func initOpts(ko *koanf.Koanf) Opts {
return Opts{
refreshInterval: ko.MustDuration("app.refresh_interval"),
removeAllocInterval: ko.MustDuration("app.remove_alloc_interval"),
nomadDataDir: ko.MustString("app.nomad_data_dir"),
vectorConfigDir: ko.MustString("app.vector_config_dir"),
extraTemplatesDir: ko.String("app.extra_templates_dir"),
}
}