Skip to content

Commit 11bcb5b

Browse files
author
Julien Pivotto
authored
Merge pull request #415 from FUSAKLA/fus-http-config-from-file
feat: make LoadHTTPConfigFile set directory and move from tests file
2 parents 87b669d + d9cd6f2 commit 11bcb5b

File tree

2 files changed

+25
-23
lines changed

2 files changed

+25
-23
lines changed

config/http_config.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"net/http"
2626
"net/url"
2727
"os"
28+
"path/filepath"
2829
"strings"
2930
"sync"
3031
"time"
@@ -248,6 +249,30 @@ func (a *OAuth2) SetDirectory(dir string) {
248249
a.TLSConfig.SetDirectory(dir)
249250
}
250251

252+
// LoadHTTPConfig parses the YAML input s into a HTTPClientConfig.
253+
func LoadHTTPConfig(s string) (*HTTPClientConfig, error) {
254+
cfg := &HTTPClientConfig{}
255+
err := yaml.UnmarshalStrict([]byte(s), cfg)
256+
if err != nil {
257+
return nil, err
258+
}
259+
return cfg, nil
260+
}
261+
262+
// LoadHTTPConfigFile parses the given YAML file into a HTTPClientConfig.
263+
func LoadHTTPConfigFile(filename string) (*HTTPClientConfig, []byte, error) {
264+
content, err := os.ReadFile(filename)
265+
if err != nil {
266+
return nil, nil, err
267+
}
268+
cfg, err := LoadHTTPConfig(string(content))
269+
if err != nil {
270+
return nil, nil, err
271+
}
272+
cfg.SetDirectory(filepath.Dir(filepath.Dir(filename)))
273+
return cfg, content, nil
274+
}
275+
251276
// HTTPClientConfig configures an HTTP client.
252277
type HTTPClientConfig struct {
253278
// The HTTP basic authentication credentials for the targets.

config/http_config_test.go

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1125,29 +1125,6 @@ func TestInvalidHTTPConfigs(t *testing.T) {
11251125
}
11261126
}
11271127

1128-
// LoadHTTPConfig parses the YAML input s into a HTTPClientConfig.
1129-
func LoadHTTPConfig(s string) (*HTTPClientConfig, error) {
1130-
cfg := &HTTPClientConfig{}
1131-
err := yaml.UnmarshalStrict([]byte(s), cfg)
1132-
if err != nil {
1133-
return nil, err
1134-
}
1135-
return cfg, nil
1136-
}
1137-
1138-
// LoadHTTPConfigFile parses the given YAML file into a HTTPClientConfig.
1139-
func LoadHTTPConfigFile(filename string) (*HTTPClientConfig, []byte, error) {
1140-
content, err := os.ReadFile(filename)
1141-
if err != nil {
1142-
return nil, nil, err
1143-
}
1144-
cfg, err := LoadHTTPConfig(string(content))
1145-
if err != nil {
1146-
return nil, nil, err
1147-
}
1148-
return cfg, content, nil
1149-
}
1150-
11511128
type roundTrip struct {
11521129
theResponse *http.Response
11531130
theError error

0 commit comments

Comments
 (0)