From b06fa293bf2186dfb483687e854625baec753bfb Mon Sep 17 00:00:00 2001 From: Rodrigo Chiossi Date: Fri, 18 May 2018 02:22:39 +0000 Subject: [PATCH] config: Add new functionality to LoadDefaults This patch adds a couple new features for LoadDefaults(). First, it introduces LoadDefaultsForPath(), which allows the default values to be set base on a given path instead of always assuming $PWD. It also removes the restriction that only allowed defaults to be set when --new-config set was declared. Removing this restriction ensures that there are always sane parameters available, even if none exists in the config file. Lastly, it moves the check for local RPMs to LoadDefaults from CreateDefaultConfig(). This prevents GetWd() to be called twice and also allows LOCAL_REPO_DIR and LOCAL_RPM_DIR to be set in the same call. Signed-off-by: Rodrigo Chiossi --- builder/builder.go | 2 +- config/config.go | 45 +++++++++++++++++++++------------------------ 2 files changed, 22 insertions(+), 25 deletions(-) diff --git a/builder/builder.go b/builder/builder.go index 0297a98..66aae0c 100644 --- a/builder/builder.go +++ b/builder/builder.go @@ -100,7 +100,7 @@ func New() *Builder { // NewFromConfig creates a new Builder with the given Configuration. func NewFromConfig(conf string) (*Builder, error) { b := New() - if err := b.Config.LoadDefaults(); err != nil { + if err := b.Config.LoadDefaults(false); err != nil { return nil, err } if err := b.LoadBuilderConf(conf); err != nil { diff --git a/config/config.go b/config/config.go index ba4ef87..e23163a 100644 --- a/config/config.go +++ b/config/config.go @@ -68,21 +68,24 @@ type mixerConf struct { } // LoadDefaults sets sane values for the config properties -func (config *MixConfig) LoadDefaults() error { - if !UseNewConfig { - return nil - } - +func (config *MixConfig) LoadDefaults(localrpms bool) error { pwd, err := os.Getwd() if err != nil { return err } + config.LoadDefaultsForPath(localrpms, pwd) + return nil +} + +// LoadDefaultsForPath sets sane values for config properties using `path` as base directory +func (config *MixConfig) LoadDefaultsForPath(localrpms bool, path string) { + // [Builder] - config.Builder.Cert = filepath.Join(pwd, "Swupd_Root.pem") - config.Builder.ServerStateDir = filepath.Join(pwd, "update") - config.Builder.VersionPath = pwd - config.Builder.DNFConf = filepath.Join(pwd, ".yum-mix.conf") + config.Builder.Cert = filepath.Join(path, "Swupd_Root.pem") + config.Builder.ServerStateDir = filepath.Join(path, "update") + config.Builder.VersionPath = path + config.Builder.DNFConf = filepath.Join(path, ".yum-mix.conf") // [Swupd] config.Swupd.Bundle = "os-core-update" @@ -96,11 +99,15 @@ func (config *MixConfig) LoadDefaults() error { config.Server.DebugInfoSrc = "/usr/src/debug" // [Mixer] - config.Mixer.LocalBundleDir = filepath.Join(pwd, "local-bundles") - config.Mixer.LocalRPMDir = "" - config.Mixer.LocalRepoDir = "" + config.Mixer.LocalBundleDir = filepath.Join(path, "local-bundles") - return nil + if localrpms { + config.Mixer.LocalRPMDir = filepath.Join(path, "local-rpms") + config.Mixer.LocalRepoDir = filepath.Join(path, "local-yum") + } else { + config.Mixer.LocalRPMDir = "" + config.Mixer.LocalRepoDir = "" + } } // CreateDefaultConfig creates a default builder.conf using the active @@ -110,20 +117,10 @@ func (config *MixConfig) CreateDefaultConfig(localrpms bool) error { return config.createLegacyConfig(localrpms) } - if err := config.LoadDefaults(); err != nil { + if err := config.LoadDefaults(localrpms); err != nil { return err } - if localrpms { - pwd, err := os.Getwd() - if err != nil { - return err - } - - config.Mixer.LocalRPMDir = filepath.Join(pwd, "local-rpms") - config.Mixer.LocalRepoDir = filepath.Join(pwd, "local-yum") - } - filename, err := GetConfigPath("") if err != nil { return err