Fix broken compatibility

Signed-off-by: itaispiegel <itai.spiegel@gmail.com>
pull/10077/head
itaispiegel 3 years ago
parent 0061f66d37
commit b0c802439b

@ -18,7 +18,6 @@ package engine
import ( import (
"encoding/base64" "encoding/base64"
"fmt"
"log" "log"
"path" "path"
"strings" "strings"
@ -105,9 +104,10 @@ func (f files) Glob(pattern string) files {
// //
// data: // data:
// {{ .Files.Glob("config/**").AsConfig() | indent 4 }} // {{ .Files.Glob("config/**").AsConfig() | indent 4 }}
func (f files) AsConfig() (string, error) { func (f files) AsConfig() string {
if f == nil || len(f) == 0 { if len(f) == 0 {
return "", fmt.Errorf("must pass files") log.Printf("must pass files")
return ""
} }
m := make(map[string]string) m := make(map[string]string)
@ -117,7 +117,7 @@ func (f files) AsConfig() (string, error) {
m[path.Base(k)] = string(v) m[path.Base(k)] = string(v)
} }
return toYAML(m), nil return toYAML(m)
} }
// AsSecrets returns the base64-encoded value of a Files object suitable for // AsSecrets returns the base64-encoded value of a Files object suitable for
@ -134,9 +134,10 @@ func (f files) AsConfig() (string, error) {
// //
// data: // data:
// {{ .Files.Glob("secrets/*").AsSecrets() }} // {{ .Files.Glob("secrets/*").AsSecrets() }}
func (f files) AsSecrets() (string, error) { func (f files) AsSecrets() string {
if f == nil || len(f) == 0 { if len(f) == 0 {
return "", fmt.Errorf("must pass files") log.Printf("must pass files")
return ""
} }
m := make(map[string]string) m := make(map[string]string)
@ -145,7 +146,7 @@ func (f files) AsSecrets() (string, error) {
m[path.Base(k)] = base64.StdEncoding.EncodeToString(v) m[path.Base(k)] = base64.StdEncoding.EncodeToString(v)
} }
return toYAML(m), nil return toYAML(m)
} }
// Lines returns each line of a named file (split by "\n") as a slice, so it can // Lines returns each line of a named file (split by "\n") as a slice, so it can
@ -155,10 +156,11 @@ func (f files) AsSecrets() (string, error) {
// //
// {{ range .Files.Lines "foo/bar.html" }} // {{ range .Files.Lines "foo/bar.html" }}
// {{ . }}{{ end }} // {{ . }}{{ end }}
func (f files) Lines(path string) ([]string, error) { func (f files) Lines(path string) []string {
if f == nil || f[path] == nil { if f == nil || f[path] == nil {
return nil, fmt.Errorf("must pass files") log.Printf("must pass files")
return nil
} }
return strings.Split(string(f[path]), "\n"), nil return strings.Split(string(f[path]), "\n")
} }

@ -98,17 +98,22 @@ func TestToConfig(t *testing.T) {
as := assert.New(t) as := assert.New(t)
f := getTestFiles() f := getTestFiles()
out, err := f.Glob("**/captain.txt").AsConfig()
var buf bytes.Buffer
log.SetOutput(&buf)
defer func() {
log.SetOutput(os.Stderr)
}()
out := f.Glob("**/captain.txt").AsConfig()
as.Equal("captain.txt: The Captain", out) as.Equal("captain.txt: The Captain", out)
as.NoError(err)
out, err = f.Glob("ship/**").AsConfig() out = f.Glob("ship/**").AsConfig()
as.Equal("captain.txt: The Captain\nstowaway.txt: Legatt", out) as.Equal("captain.txt: The Captain\nstowaway.txt: Legatt", out)
as.NoError(err)
out, err = f.Glob(NonExistingFileName).AsConfig() out = f.Glob(NonExistingFileName).AsConfig()
as.Empty(out) as.Empty(out)
as.Error(err, "must pass files") as.Contains(buf.String(), "must pass files")
} }
func TestToSecret(t *testing.T) { func TestToSecret(t *testing.T) {
@ -116,13 +121,18 @@ func TestToSecret(t *testing.T) {
f := getTestFiles() f := getTestFiles()
out, err := f.Glob("ship/**").AsSecrets() var buf bytes.Buffer
log.SetOutput(&buf)
defer func() {
log.SetOutput(os.Stderr)
}()
out := f.Glob("ship/**").AsSecrets()
as.Equal("captain.txt: VGhlIENhcHRhaW4=\nstowaway.txt: TGVnYXR0", out) as.Equal("captain.txt: VGhlIENhcHRhaW4=\nstowaway.txt: TGVnYXR0", out)
as.NoError(err)
out, err = f.Glob(NonExistingFileName).AsSecrets() out = f.Glob(NonExistingFileName).AsSecrets()
as.Empty(out) as.Empty(out)
as.Errorf(err, "must pass files") as.Contains(buf.String(), "must pass files")
} }
func TestLines(t *testing.T) { func TestLines(t *testing.T) {
@ -130,12 +140,17 @@ func TestLines(t *testing.T) {
f := getTestFiles() f := getTestFiles()
out, err := f.Lines("multiline/test.txt") var buf bytes.Buffer
log.SetOutput(&buf)
defer func() {
log.SetOutput(os.Stderr)
}()
out := f.Lines("multiline/test.txt")
as.Len(out, 2) as.Len(out, 2)
as.Equal("bar", out[0]) as.Equal("bar", out[0])
as.NoError(err)
out, err = f.Lines(NonExistingFileName) out = f.Lines(NonExistingFileName)
as.Nil(out) as.Nil(out)
as.Error(err, "must pass files") as.Contains(buf.String(), "must pass files")
} }

Loading…
Cancel
Save