Skip to content

Commit f39dfa2

Browse files
authored
config: add SetDirectory (#250)
Signed-off-by: Andy Bursavich <[email protected]>
1 parent 0bd4490 commit f39dfa2

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

config/config.go

+19
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package config
1818

19+
import "path/filepath"
20+
1921
// Secret special type for storing secrets.
2022
type Secret string
2123

@@ -32,3 +34,20 @@ func (s *Secret) UnmarshalYAML(unmarshal func(interface{}) error) error {
3234
type plain Secret
3335
return unmarshal((*plain)(s))
3436
}
37+
38+
// DirectorySetter is a config type that contains file paths that may
39+
// be relative to the file containing the config.
40+
type DirectorySetter interface {
41+
// SetDirectory joins any relative file paths with dir.
42+
// Any paths that are empty or absolute remain unchanged.
43+
SetDirectory(dir string)
44+
}
45+
46+
// JoinDir joins dir and path if path is relative.
47+
// If path is empty or absolute, it is returned unchanged.
48+
func JoinDir(dir, path string) string {
49+
if path == "" || filepath.IsAbs(path) {
50+
return path
51+
}
52+
return filepath.Join(dir, path)
53+
}

config/http_config.go

+28
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,14 @@ type BasicAuth struct {
4444
PasswordFile string `yaml:"password_file,omitempty"`
4545
}
4646

47+
// SetDirectory joins any relative file paths with dir.
48+
func (a *BasicAuth) SetDirectory(dir string) {
49+
if a == nil {
50+
return
51+
}
52+
a.PasswordFile = JoinDir(dir, a.PasswordFile)
53+
}
54+
4755
// URL is a custom URL type that allows validation at configuration load time.
4856
type URL struct {
4957
*url.URL
@@ -86,6 +94,16 @@ type HTTPClientConfig struct {
8694
TLSConfig TLSConfig `yaml:"tls_config,omitempty"`
8795
}
8896

97+
// SetDirectory joins any relative file paths with dir.
98+
func (c *HTTPClientConfig) SetDirectory(dir string) {
99+
if c == nil {
100+
return
101+
}
102+
c.TLSConfig.SetDirectory(dir)
103+
c.BasicAuth.SetDirectory(dir)
104+
c.BearerTokenFile = JoinDir(dir, c.BearerTokenFile)
105+
}
106+
89107
// Validate validates the HTTPClientConfig to check only one of BearerToken,
90108
// BasicAuth and BearerTokenFile is configured.
91109
func (c *HTTPClientConfig) Validate() error {
@@ -352,6 +370,16 @@ type TLSConfig struct {
352370
InsecureSkipVerify bool `yaml:"insecure_skip_verify"`
353371
}
354372

373+
// SetDirectory joins any relative file paths with dir.
374+
func (c *TLSConfig) SetDirectory(dir string) {
375+
if c == nil {
376+
return
377+
}
378+
c.CAFile = JoinDir(dir, c.CAFile)
379+
c.CertFile = JoinDir(dir, c.CertFile)
380+
c.KeyFile = JoinDir(dir, c.KeyFile)
381+
}
382+
355383
// UnmarshalYAML implements the yaml.Unmarshaler interface.
356384
func (c *TLSConfig) UnmarshalYAML(unmarshal func(interface{}) error) error {
357385
type plain TLSConfig

0 commit comments

Comments
 (0)