Skip to content

Commit

Permalink
Fixed problem where the config was not correctly picked up
Browse files Browse the repository at this point in the history
  • Loading branch information
blaubaer committed Feb 10, 2019
1 parent 3054bc9 commit e79be7d
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 16 deletions.
4 changes: 1 addition & 3 deletions app/appFactory.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,7 @@ func newAppFor(config *ConfigWrapper, platform string, executableType Executable
PlaceHolder(config.ListenAddress().String()).
SetValue(config.ListenAddress())

if executableType == Daemon {
config.forDaemon = true
} else {
if executableType != Daemon {
app.Flag("pem", "Location of PEM file which contains the private public key pair for access to the daemon.").
Short('p').
PlaceHolder(config.PemFile().String()).
Expand Down
18 changes: 12 additions & 6 deletions app/configWrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"github.com/echocat/caretakerd/defaults"
"github.com/echocat/caretakerd/errors"
"github.com/echocat/caretakerd/values"
"runtime"
"sync"
)

Expand All @@ -13,7 +14,6 @@ import (
type ConfigWrapper struct {
config *caretakerd.Config
explicitSet bool
forDaemon bool
listenAddress *FlagWrapper
pemFile *FlagWrapper
platform string
Expand All @@ -33,6 +33,7 @@ func NewConfigWrapperFor(platform string) *ConfigWrapper {
listenAddress: NewFlagWrapper(&defaultListenAddress),
pemFile: NewFlagWrapper(&defaultPemFile),
platform: platform,
mutex: new(sync.Mutex),
}
}

Expand All @@ -46,6 +47,7 @@ func (instance *ConfigWrapper) Set(value string) error {
if config, err := instance.loadConfigFrom(values.String(value)); err != nil {
return err
} else {
instance.explicitSet = true
instance.config = config
return nil
}
Expand All @@ -63,14 +65,14 @@ func (instance ConfigWrapper) loadConfigFrom(fileName values.String) (*caretaker
return &conf, nil
}

func (instance *ConfigWrapper) populateAndValidate(conf *caretakerd.Config) error {
func (instance *ConfigWrapper) populateAndValidate(forDaemon bool, conf *caretakerd.Config) error {
instance.listenAddress.AssignIfExplicitSet(&conf.RPC.Listen)
instance.pemFile.AssignIfExplicitSet(&conf.Control.Access.PemFile)
err := conf.Validate()
if err != nil {
return err
}
if instance.forDaemon {
if forDaemon {
return conf.ValidateMaster()
}
return nil
Expand All @@ -92,7 +94,7 @@ func (instance ConfigWrapper) PemFile() *FlagWrapper {
}

// ProvideConfig will either return the already loaded configuration or will load it
func (instance *ConfigWrapper) ProvideConfig() (*caretakerd.Config, error) {
func (instance *ConfigWrapper) ProvideConfig(forDaemon bool) (*caretakerd.Config, error) {
instance.mutex.Lock()
defer instance.mutex.Unlock()
if instance.loaded {
Expand All @@ -105,9 +107,13 @@ func (instance *ConfigWrapper) ProvideConfig() (*caretakerd.Config, error) {
} else {
filename := defaults.ConfigFilenameFor(instance.platform)
if lConfig, err := instance.loadConfigFrom(filename); caretakerd.IsConfigNotExists(err) {
if instance.forDaemon {
if forDaemon {
return nil, errors.New("There is neither the --config flag set nor does a configuration file under default position (%v) exist.", filename)
}
if lConfig == nil {
nConfig := caretakerd.NewConfigFor(runtime.GOOS)
lConfig = &nConfig
}
config = lConfig
} else if err != nil {
return nil, err
Expand All @@ -116,7 +122,7 @@ func (instance *ConfigWrapper) ProvideConfig() (*caretakerd.Config, error) {
}
}

if err := instance.populateAndValidate(config); err != nil {
if err := instance.populateAndValidate(forDaemon, config); err != nil {
return nil, err
}
instance.config = config
Expand Down
10 changes: 7 additions & 3 deletions app/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ func attachArgsToMasterIfPossible(args []string, to *caretakerd.Config) {
}
}

func runDaemon(conf caretakerd.Config, args []string) {
attachArgsToMasterIfPossible(args, &conf)
func runDaemon(conf *caretakerd.Config, args []string) {
attachArgsToMasterIfPossible(args, conf)
instance, err := caretakerd.NewCaretakerd(conf, sync.NewGroup())
if err != nil {
stack.Print(err, os.Stderr, 0)
Expand Down Expand Up @@ -58,7 +58,11 @@ func registerDaemonCommandsAt(config *ConfigWrapper, executableType ExecutableTy
Strings()

cmd.Action(func(*kingpin.ParseContext) error {
runDaemon(*config.config, *arguments)
config, err := config.ProvideConfig(true)
if err != nil {
return err
}
runDaemon(config, *arguments)
return nil
})
}
4 changes: 2 additions & 2 deletions caretakerd.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (

// Caretakerd instance structure
type Caretakerd struct {
config Config
config *Config
logger *logger.Logger
control *control.Control
services *service.Services
Expand All @@ -36,7 +36,7 @@ func finalize(what *Caretakerd) {
}

// NewCaretakerd creates a new Caretakerd instance from the given config
func NewCaretakerd(conf Config, syncGroup *usync.Group) (*Caretakerd, error) {
func NewCaretakerd(conf *Config, syncGroup *usync.Group) (*Caretakerd, error) {
err := conf.Validate()
if err != nil {
return nil, err
Expand Down
4 changes: 2 additions & 2 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
)

type ConfigProvider interface {
ProvideConfig() (*caretakerd.Config, error)
ProvideConfig(forDaemon bool) (*caretakerd.Config, error)
}

// AccessDeniedError represents an error that occurs if someone tries to access a
Expand Down Expand Up @@ -63,7 +63,7 @@ func NewFactory(configProvider ConfigProvider) *Factory {

// NewClient creates a new Client.
func (instance *Factory) NewClient() (*Client, error) {
config, err := instance.configProvider.ProvideConfig()
config, err := instance.configProvider.ProvideConfig(false)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit e79be7d

Please sign in to comment.