Skip to content

Commit

Permalink
Add base64 encoding support to file provider
Browse files Browse the repository at this point in the history
Signed-off-by: Stepan Kokhanovskiy <[email protected]>
  • Loading branch information
Stepan Kokhanovskiy committed Sep 16, 2023
1 parent 193b8a2 commit 70b0cfe
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -576,6 +576,7 @@ Examples:
- `ref+file://foo/bar` loads the file at `foo/bar`
- `ref+file:///home/foo/bar` loads the file at `/home/foo/bar`
- `ref+file://foo/bar?encode=base64` loads the file at `foo/bar` and encodes its content to a base64 string
- `ref+file://some.yaml#/foo/bar` loads the YAML file at `some.yaml` and reads the value for the path `$.foo.bar`.
Let's say `some.yaml` contains `{"foo":{"bar":"BAR"}}`, `key1: ref+file://some.yaml#/foo/bar` results in `key1: BAR`.
Expand Down
18 changes: 17 additions & 1 deletion pkg/providers/file/file.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package file

import (
"encoding/base64"
"fmt"
"os"
"strings"

Expand All @@ -10,20 +12,34 @@ import (
)

type provider struct {
Encode string
}

func New(cfg api.StaticConfig) *provider {
p := &provider{}
p.Encode = cfg.String("encode")
if p.Encode == "" {
p.Encode = "raw"
}
return p
}

func (p *provider) GetString(key string) (string, error) {
res := ""
key = strings.TrimSuffix(key, "/")
bs, err := os.ReadFile(key)
if err != nil {
return "", err
}
return string(bs), nil
switch p.Encode {
case "raw":
res = string(bs)
case "base64":
res = base64.StdEncoding.EncodeToString(bs)
default:
return "", fmt.Errorf("Unsupported encode parameter: '%s'.", p.Encode)
}
return res, nil
}

func (p *provider) GetStringMap(key string) (map[string]interface{}, error) {
Expand Down

0 comments on commit 70b0cfe

Please sign in to comment.