From 271c91eecbeb562724612a179fc3f04b81faf6dd Mon Sep 17 00:00:00 2001 From: ston1th Date: Sat, 31 Oct 2020 12:36:57 +0100 Subject: [PATCH] load options from env --- client.go | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/client.go b/client.go index 21e863e..0357e39 100644 --- a/client.go +++ b/client.go @@ -13,6 +13,8 @@ import ( "net" "net/http" "net/http/httputil" + "os" + "strconv" "strings" "sync" "time" @@ -107,6 +109,40 @@ func WithCredentials(username, password string) ClientOption { } } +var ( + ErrMissingAPIEnv = errors.New("missing environment variable PVE_API") + ErrMissingUserEnv = errors.New("missing environment variable PVE_USER") + ErrMissingPasswordEnv = errors.New("missing environment variable PVE_PASSWORD") +) + +func ClientOptionsFromEnv() (options []ClientOption, err error) { + api := os.Getenv("PVE_API") + if api == "" { + err = ErrMissingAPIEnv + //return + } + user := os.Getenv("PVE_USER") + if user == "" { + err = ErrMissingUserEnv + //return + } + + pass := os.Getenv("PVE_PASSWORD") + if pass == "" { + err = ErrMissingPasswordEnv + //return + } + + insecure, _ := strconv.ParseBool(os.Getenv("PVE_INSECURE")) + + options = []ClientOption{ + WithEndpoint(api), + WithCredentials(user, pass), + WithInsecureClient(insecure), + } + return +} + func NewClient(options ...ClientOption) *Client { client := &Client{} /*