diff --git a/config/config.go b/config/config.go index 30719d83..d8a7351f 100644 --- a/config/config.go +++ b/config/config.go @@ -16,6 +16,8 @@ package config +import "path/filepath" + // Secret special type for storing secrets. type Secret string @@ -32,3 +34,20 @@ func (s *Secret) UnmarshalYAML(unmarshal func(interface{}) error) error { type plain Secret return unmarshal((*plain)(s)) } + +// DirectorySetter is a config type that contains file paths that may +// be relative to the file containing the config. +type DirectorySetter interface { + // SetDirectory joins any relative file paths with dir. + // Any paths that are empty or absolute remain unchanged. + SetDirectory(dir string) +} + +// JoinDir joins dir and path if path is relative. +// If path is empty or absolute, it is returned unchanged. +func JoinDir(dir, path string) string { + if path == "" || filepath.IsAbs(path) { + return path + } + return filepath.Join(dir, path) +} diff --git a/config/http_config.go b/config/http_config.go index 3a49aa7c..4dd88758 100644 --- a/config/http_config.go +++ b/config/http_config.go @@ -44,6 +44,14 @@ type BasicAuth struct { PasswordFile string `yaml:"password_file,omitempty"` } +// SetDirectory joins any relative file paths with dir. +func (a *BasicAuth) SetDirectory(dir string) { + if a == nil { + return + } + a.PasswordFile = JoinDir(dir, a.PasswordFile) +} + // URL is a custom URL type that allows validation at configuration load time. type URL struct { *url.URL @@ -86,6 +94,16 @@ type HTTPClientConfig struct { TLSConfig TLSConfig `yaml:"tls_config,omitempty"` } +// SetDirectory joins any relative file paths with dir. +func (c *HTTPClientConfig) SetDirectory(dir string) { + if c == nil { + return + } + c.TLSConfig.SetDirectory(dir) + c.BasicAuth.SetDirectory(dir) + c.BearerTokenFile = JoinDir(dir, c.BearerTokenFile) +} + // Validate validates the HTTPClientConfig to check only one of BearerToken, // BasicAuth and BearerTokenFile is configured. func (c *HTTPClientConfig) Validate() error { @@ -352,6 +370,16 @@ type TLSConfig struct { InsecureSkipVerify bool `yaml:"insecure_skip_verify"` } +// SetDirectory joins any relative file paths with dir. +func (c *TLSConfig) SetDirectory(dir string) { + if c == nil { + return + } + c.CAFile = JoinDir(dir, c.CAFile) + c.CertFile = JoinDir(dir, c.CertFile) + c.KeyFile = JoinDir(dir, c.KeyFile) +} + // UnmarshalYAML implements the yaml.Unmarshaler interface. func (c *TLSConfig) UnmarshalYAML(unmarshal func(interface{}) error) error { type plain TLSConfig