wisski-cloud-distillery/internal/config/validators/url.go
Tom Wiesing 945329a080
Move to yaml-based configuration
This commit updates the configuration to be yaml-based and updates the
configuration to read in a yaml file.
2023-02-25 09:14:56 +01:00

46 lines
848 B
Go

package validators
import (
"net/url"
"github.com/pkg/errors"
)
// URL represents a url.URL that is marshaled as a string representing the url.
type URL url.URL
func (u *URL) MarshalText() (text []byte, err error) {
return []byte(u.String()), nil
}
func (u *URL) String() string {
if u == nil {
return ""
}
return (*url.URL)(u).String()
}
func (u *URL) UnmarshalText(text []byte) error {
if len(text) == 0 {
return nil
}
pu, err := url.Parse(string(text))
if err != nil {
return err
}
*u = URL(*pu)
return nil
}
func ValidateHTTPSURL(url **URL, dflt string) error {
if (*url).String() == "" {
*url = new(URL)
if err := (*url).UnmarshalText([]byte(dflt)); err != nil {
return err
}
}
if (*url).Scheme != "https" {
return errors.Errorf("%q is not a valid https URL (%q)", *url, (*url).Scheme)
}
return nil
}