load options from env

This commit is contained in:
ston1th 2020-10-31 12:36:57 +01:00
commit 271c91eecb

View file

@ -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{}
/*