load options from env
This commit is contained in:
parent
93d99e29cc
commit
271c91eecb
1 changed files with 36 additions and 0 deletions
36
client.go
36
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{}
|
||||
/*
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue