-
-
Notifications
You must be signed in to change notification settings - Fork 22
/
loader.go
58 lines (45 loc) · 1.12 KB
/
loader.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
// Copyright 2020 Clivern. All rights reserved.
// Use of this source code is governed by the MIT
// license that can be found in the LICENSE file.
package pkg
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"github.com/drone/envsubst"
"github.com/spf13/viper"
)
// GetBaseDir returns the project base dir
// Base dir identified if dirName found
// This function for testing purposes only
func GetBaseDir(dirName string) string {
baseDir, _ := os.Getwd()
cacheDir := fmt.Sprintf("%s/%s", baseDir, dirName)
for {
if fi, err := os.Stat(cacheDir); err == nil {
if fi.Mode().IsDir() {
return baseDir
}
}
baseDir = filepath.Dir(baseDir)
cacheDir = fmt.Sprintf("%s/%s", baseDir, dirName)
}
}
// LoadConfigs load configs for testing purposes using viper
func LoadConfigs(path string) error {
data, err := ioutil.ReadFile(path)
if err != nil {
return err
}
data1, err := envsubst.EvalEnv(string(data))
if err != nil {
return err
}
viper.SetConfigType("yaml")
viper.ReadConfig(bytes.NewBufferString(data1))
viper.SetDefault("app.name", "x-x-x-x")
viper.SetDefault("role", "tower")
return nil
}