diff --git a/config/http_config.go b/config/http_config.go index 92699b58..3ba7f99c 100644 --- a/config/http_config.go +++ b/config/http_config.go @@ -25,6 +25,7 @@ import ( "net/http" "net/url" "os" + "path/filepath" "strings" "sync" "time" @@ -248,6 +249,30 @@ func (a *OAuth2) SetDirectory(dir string) { a.TLSConfig.SetDirectory(dir) } +// LoadHTTPConfig parses the YAML input s into a HTTPClientConfig. +func LoadHTTPConfig(s string) (*HTTPClientConfig, error) { + cfg := &HTTPClientConfig{} + err := yaml.UnmarshalStrict([]byte(s), cfg) + if err != nil { + return nil, err + } + return cfg, nil +} + +// LoadHTTPConfigFile parses the given YAML file into a HTTPClientConfig. +func LoadHTTPConfigFile(filename string) (*HTTPClientConfig, []byte, error) { + content, err := os.ReadFile(filename) + if err != nil { + return nil, nil, err + } + cfg, err := LoadHTTPConfig(string(content)) + if err != nil { + return nil, nil, err + } + cfg.SetDirectory(filepath.Dir(filepath.Dir(filename))) + return cfg, content, nil +} + // HTTPClientConfig configures an HTTP client. type HTTPClientConfig struct { // The HTTP basic authentication credentials for the targets. diff --git a/config/http_config_test.go b/config/http_config_test.go index 89594cd6..f608447c 100644 --- a/config/http_config_test.go +++ b/config/http_config_test.go @@ -1125,29 +1125,6 @@ func TestInvalidHTTPConfigs(t *testing.T) { } } -// LoadHTTPConfig parses the YAML input s into a HTTPClientConfig. -func LoadHTTPConfig(s string) (*HTTPClientConfig, error) { - cfg := &HTTPClientConfig{} - err := yaml.UnmarshalStrict([]byte(s), cfg) - if err != nil { - return nil, err - } - return cfg, nil -} - -// LoadHTTPConfigFile parses the given YAML file into a HTTPClientConfig. -func LoadHTTPConfigFile(filename string) (*HTTPClientConfig, []byte, error) { - content, err := os.ReadFile(filename) - if err != nil { - return nil, nil, err - } - cfg, err := LoadHTTPConfig(string(content)) - if err != nil { - return nil, nil, err - } - return cfg, content, nil -} - type roundTrip struct { theResponse *http.Response theError error