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