added some client options
This commit is contained in:
parent
5a69cc1f27
commit
99bd412291
1 changed files with 52 additions and 3 deletions
|
|
@ -12,6 +12,7 @@ import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httputil"
|
"net/http/httputil"
|
||||||
"net/url"
|
"net/url"
|
||||||
|
neturl "net/url"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
@ -75,9 +76,11 @@ func WithHTTPClient(httpClient *http.Client) ClientOption {
|
||||||
//}
|
//}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
ErrMissingEndpointEnv = errors.New("missing environment variable KEYCTL_ENDPOINT")
|
ErrMissingEndpointEnv = errors.New("missing environment variable KEYCTL_ENDPOINT")
|
||||||
|
ErrMissingEndpointInURL = errors.New("missing host in URL")
|
||||||
ErrMissingEndpoint = errors.New("missing Endpoint in config")
|
ErrMissingIDInURL = errors.New("missing ID in URL")
|
||||||
|
ErrUnsupportedScheme = errors.New("unsupported scheme")
|
||||||
|
ErrMissingEndpoint = errors.New("missing Endpoint in config")
|
||||||
)
|
)
|
||||||
|
|
||||||
type Config struct {
|
type Config struct {
|
||||||
|
|
@ -112,6 +115,52 @@ func ClientOptionsFromEnv() (options []ClientOption, err error) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ClientOptionsFromURL(url string) (options []ClientOption, err error) {
|
||||||
|
u, err := neturl.Parse(url)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if u.Scheme != "keyctl" {
|
||||||
|
return nil, fmt.Errorf("%w: %s", ErrUnsupportedScheme, u.Scheme)
|
||||||
|
}
|
||||||
|
if u.Host == "" {
|
||||||
|
err = ErrMissingEndpointInURL
|
||||||
|
return
|
||||||
|
}
|
||||||
|
//insecure, _ := strconv.ParseBool(os.Getenv("HAPROXY_LB_INSECURE"))
|
||||||
|
var endpoint string
|
||||||
|
if u.Query().Get("tls") == "true" {
|
||||||
|
endpoint = "https://" + u.Host
|
||||||
|
} else {
|
||||||
|
endpoint = "http://" + u.Host
|
||||||
|
}
|
||||||
|
|
||||||
|
options = []ClientOption{
|
||||||
|
WithEndpoint(endpoint),
|
||||||
|
//WithInsecureClient(insecure),
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func KeyFromURL(url string) (id, share string, err error) {
|
||||||
|
u, err := neturl.Parse(url)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if u.Scheme != "keyctl" {
|
||||||
|
err = fmt.Errorf("%w: %s", ErrUnsupportedScheme, u.Scheme)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
q := u.Query()
|
||||||
|
id = q.Get("id")
|
||||||
|
if id == "" {
|
||||||
|
err = ErrMissingIDInURL
|
||||||
|
return
|
||||||
|
}
|
||||||
|
share = q.Get("share")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
func NewClient(options ...ClientOption) *Client {
|
func NewClient(options ...ClientOption) *Client {
|
||||||
client := &Client{}
|
client := &Client{}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue