Skip to content

Commit

Permalink
config: add SetDirectory (#250)
Browse files Browse the repository at this point in the history
Signed-off-by: Andy Bursavich <abursavich@gmail.com>
  • Loading branch information
abursavich authored Aug 19, 2020
1 parent 0bd4490 commit f39dfa2
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
19 changes: 19 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package config

import "path/filepath"

// Secret special type for storing secrets.
type Secret string

Expand All @@ -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)
}
28 changes: 28 additions & 0 deletions config/http_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit f39dfa2

Please sign in to comment.