Support _FILE postfixes for environment variables (#1142)

* Support _FILE postfixes for environment variables to be loaded from files

* fix gofmt error
This commit is contained in:
james h
2018-07-30 20:13:06 +02:00
committed by Reed Allman
parent e7188d0e30
commit 61d42e7621
2 changed files with 25 additions and 0 deletions

View File

@@ -2,10 +2,12 @@ package server
import (
"context"
"io/ioutil"
"os"
"os/signal"
"runtime"
"strconv"
"strings"
"syscall"
"github.com/fnproject/fn/api/common"
@@ -21,6 +23,11 @@ func init() {
func getEnv(key, fallback string) string {
if value, ok := os.LookupEnv(key); ok {
return value
} else if value, ok := os.LookupEnv(key + "_FILE"); ok {
dat, err := ioutil.ReadFile(value)
if err == nil {
return string(dat)
}
}
return fallback
}
@@ -34,6 +41,16 @@ func getEnvInt(key string, fallback int) int {
panic(err) // not sure how to handle this
}
return i
} else if value, ok := os.LookupEnv(key + "_FILE"); ok {
dat, err := ioutil.ReadFile(value)
if err == nil {
var err error
var i int
if i, err = strconv.Atoi(strings.TrimSpace(string(dat))); err != nil {
panic(err) // not sure how to handle this
}
return i
}
}
return fallback
}