Skip to content

Commit 6dded82

Browse files
author
Otávio Fernandes
committed
Renaming method and more logging.
1 parent 87db222 commit 6dded82

File tree

6 files changed

+23
-24
lines changed

6 files changed

+23
-24
lines changed

pkg/vault-handler/dot_env.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ type DotEnv struct {
2121

2222
// Prepare by checking if dot-env (".env") file already exists, read it's contents.
2323
func (d *DotEnv) Prepare() error {
24-
if !fileExists(d.fullPath) {
24+
if !FileExists(d.fullPath) {
2525
d.logger.Info("Dot-env file is not found.")
2626
return nil
2727
}
@@ -43,7 +43,7 @@ func (d *DotEnv) Write() error {
4343
defer f.Close()
4444

4545
for k, v := range d.data {
46-
if _, err = f.WriteString(fmt.Sprintf("%s=\"%s\"\n", k, shellescape.Quote(v))); err != nil {
46+
if _, err = f.WriteString(fmt.Sprintf("%s=%s\n", k, shellescape.Quote(v))); err != nil {
4747
return err
4848
}
4949
}
@@ -73,11 +73,13 @@ func (d *DotEnv) envVarName(file *File) string {
7373
// loadFiles loop over array of Files, load contents
7474
func (d *DotEnv) loadFiles() {
7575
for _, file := range d.files {
76-
key := d.envVarName(file)
77-
if _, found := d.data[key]; found {
78-
d.logger.Warnf("Variable '%s' is already preset on '%s'!", key, d.fullPath)
76+
k := d.envVarName(file)
77+
if _, found := d.data[k]; found {
78+
d.logger.Warnf("Variable '%s' is already preset on '%s'!", k, d.fullPath)
7979
}
80-
d.data[key] = string(file.Payload)
80+
v := string(file.Payload)
81+
d.logger.Tracef("Adding entry on dot-env: '%s'='%s'", k, v)
82+
d.data[k] = v
8183
}
8284
}
8385

pkg/vault-handler/file.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ func (f *File) Read(baseDir string) error {
7474
var err error
7575

7676
fullPath := f.FilePath(baseDir)
77-
if !fileExists(fullPath) {
77+
if !FileExists(fullPath) {
7878
return fmt.Errorf("can't find file '%s'", fullPath)
7979
}
8080
if f.Payload, err = ioutil.ReadFile(fullPath); err != nil {

pkg/vault-handler/kubernetes.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ func (k *Kubernetes) localConfig() (*rest.Config, error) {
113113
}
114114
k.logger.Infof("Using kubernetes configuration: '%s'", k.kubeConfig)
115115

116-
if !fileExists(k.kubeConfig) {
116+
if !FileExists(k.kubeConfig) {
117117
return nil, fmt.Errorf("can't find kube-config file at: '%s'", k.kubeConfig)
118118
}
119119

pkg/vault-handler/utils.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import (
77
log "github.com/sirupsen/logrus"
88
)
99

10-
// fileExists Check if path exists, boolean return.
11-
func fileExists(path string) bool {
10+
// FileExists Check if path exists, boolean return.
11+
func FileExists(path string) bool {
1212
if _, err := os.Stat(path); err != nil {
1313
return false
1414
}
@@ -23,7 +23,7 @@ func readFile(path string) []byte {
2323
logger := log.WithField("path", path)
2424
logger.Infof("Reading file bytes")
2525

26-
if !fileExists(path) {
26+
if !FileExists(path) {
2727
logger.Fatal("Can't find file")
2828
}
2929
if fileBytes, err = ioutil.ReadFile(path); err != nil {

pkg/vault-handler/utils_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ import (
77
)
88

99
func TestUtilsFileExists(t *testing.T) {
10-
found := fileExists("../../test/manifest.yaml")
10+
found := FileExists("../../test/manifest.yaml")
1111
assert.True(t, found)
1212

13-
found = fileExists("../../should/not/exist.yaml")
13+
found = FileExists("../../should/not/exist.yaml")
1414
assert.False(t, found)
1515
}
1616

test/e2e/vault_handler_test.go

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"io/ioutil"
66
"os"
7+
"path"
78
"testing"
89

910
log "github.com/sirupsen/logrus"
@@ -18,6 +19,7 @@ var config = &vh.Config{
1819
VaultAddr: "http://127.0.0.1:8200",
1920
InputDir: "../mock/input-dir",
2021
OutputDir: "/tmp",
22+
DotEnv: true,
2123
VaultRoleID: os.Getenv("VAULT_HANDLER_VAULT_ROLE_ID"),
2224
VaultSecretID: os.Getenv("VAULT_HANDLER_VAULT_SECRET_ID"),
2325
KubeConfig: os.Getenv("KUBECONFIG"),
@@ -69,23 +71,18 @@ func readFile(t *testing.T, path string) []byte {
6971
return fileBytes
7072
}
7173

72-
func fileExists(path string) bool {
73-
if _, err := os.Stat(path); err != nil {
74-
return false
75-
}
76-
return true
77-
}
78-
7974
func cleanUp(t *testing.T) {
75+
_ = os.Remove(path.Join(config.OutputDir, ".env"))
76+
8077
loopOverManifests(t, func(t *testing.T, manifest *vh.Manifest) {
8178
loopOverGroupSecrets(t, manifest, func(t *testing.T, group string, data *vh.SecretData) {
8279
file := vh.NewFile(group, "", data, nil)
83-
path := file.FilePath(config.OutputDir)
80+
fullPath := file.FilePath(config.OutputDir)
8481

85-
t.Logf("Excluding file: '%s'", path)
82+
t.Logf("Excluding file: '%s'", fullPath)
8683

87-
_ = os.Remove(path)
88-
assert.False(t, fileExists(path))
84+
_ = os.Remove(fullPath)
85+
assert.False(t, vh.FileExists(fullPath))
8986
})
9087
})
9188
}

0 commit comments

Comments
 (0)