Skip to content

Commit

Permalink
feat: add "env://" scheme for blob load
Browse files Browse the repository at this point in the history
Signed-off-by: Zachary Newman <z@znewman.net>
  • Loading branch information
znewman01 committed Apr 24, 2022
1 parent 89dd9a9 commit 25a008e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
7 changes: 7 additions & 0 deletions pkg/blob/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
24 changes: 23 additions & 1 deletion pkg/blob/load_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}

0 comments on commit 25a008e

Please sign in to comment.