From 25a008e2b11bbc63692553ba06ee27fd222224d4 Mon Sep 17 00:00:00 2001 From: Zachary Newman Date: Sat, 23 Apr 2022 18:16:20 -0400 Subject: [PATCH] feat: add "env://" scheme for blob load Signed-off-by: Zachary Newman --- pkg/blob/load.go | 7 +++++++ pkg/blob/load_test.go | 24 +++++++++++++++++++++++- 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/pkg/blob/load.go b/pkg/blob/load.go index 2418f1c6f90..c92d49c4ca3 100644 --- a/pkg/blob/load.go +++ b/pkg/blob/load.go @@ -43,6 +43,13 @@ func LoadFileOrURL(fileRef string) ([]byte, error) { if err != nil { return nil, err } + case "env://": + envVar := parts[1] + value, found := os.LookupEnv(envVar) + if !found { + return nil, fmt.Errorf("loading URL: env var $%s not found", envVar) + } + raw = []byte(value) default: return nil, fmt.Errorf("loading URL: unrecognized scheme: %s", scheme) } diff --git a/pkg/blob/load_test.go b/pkg/blob/load_test.go index e94c5b552f0..c53b0621fd5 100644 --- a/pkg/blob/load_test.go +++ b/pkg/blob/load_test.go @@ -62,8 +62,30 @@ func TestLoadFileOrURL(t *testing.T) { t.Errorf("LoadFileOrURL(HTTP) = '%s'; want '%s'", actual, data) } + os.Setenv("MY_ENV_VAR", string(data)) + actual, err = LoadFileOrURL("env://MY_ENV_VAR") + if err != nil { + t.Errorf("Reading from environment failed: %v", err) + } else if !bytes.Equal(actual, data) { + t.Errorf("LoadFileOrURL(env) = '%s'; want '%s'", actual, data) + } + + os.Setenv("MY_ENV_VAR", "") + actual, err = LoadFileOrURL("env://MY_ENV_VAR") + if err != nil { + t.Errorf("Reading from environment failed: %v", err) + } else if !bytes.Equal(actual, make([]byte, 0)) { + t.Errorf("LoadFileOrURL(env) = '%s'; should be empty", actual) + } + + os.Unsetenv("MY_ENV_VAR") + _, err = LoadFileOrURL("env://MY_ENV_VAR") + if err == nil { + t.Error("LoadFileOrURL(): expected error for unset env var") + } + _, err = LoadFileOrURL("invalid://url") if err == nil { - t.Error("LoadFileOrURL(): expected error") + t.Error("LoadFileOrURL(): expected error for invalid scheme") } }