From cec1d9d6b3d4016e4acdc8c21d90da52e18cdffa Mon Sep 17 00:00:00 2001 From: Abubakr-Sadik Nii Nai Davis Date: Tue, 24 Oct 2017 12:01:02 +0000 Subject: [PATCH] Combine config reading functions into single function. --- cmd/common.go | 6 ++--- cmd/util.go | 72 +++++---------------------------------------------- 2 files changed, 9 insertions(+), 69 deletions(-) diff --git a/cmd/common.go b/cmd/common.go index 2c22c9d..4a9c8fa 100644 --- a/cmd/common.go +++ b/cmd/common.go @@ -66,9 +66,9 @@ func runChecks(t check.NodeType) { // Get the set of exectuables and config files we care about on this type of node. This also // checks that the executables we need for the node type are running. binmap := getBinaries(typeConf) - confmap := getConfigFiles(typeConf) - podspecmap := getPodSpecFiles(typeConf) - unitfilemap := getUnitFiles(typeConf) + confmap := getConfigFiles(typeConf, "conf") + podspecmap := getConfigFiles(typeConf, "podspec") + unitfilemap := getConfigFiles(typeConf, "unitfile") switch t { case check.MASTER: diff --git a/cmd/util.go b/cmd/util.go index 1de375c..ec666ef 100644 --- a/cmd/util.go +++ b/cmd/util.go @@ -117,7 +117,9 @@ func getBinaries(v *viper.Viper) map[string]string { } // getConfigFiles finds which of the set of candidate config files exist -func getConfigFiles(v *viper.Viper) map[string]string { +// accepts a string 't' which indicates the type of config file, conf, +// podspec or untifile. +func getConfigFiles(v *viper.Viper, t string) map[string]string { confmap := make(map[string]string) for _, component := range v.GetStringSlice("components") { @@ -127,10 +129,10 @@ func getConfigFiles(v *viper.Viper) map[string]string { } // See if any of the candidate config files exist - conf := findConfigFile(s.GetStringSlice("confs")) + conf := findConfigFile(s.GetStringSlice(t)) if conf == "" { - if s.IsSet("defaultconf") { - conf = s.GetString("defaultconf") + if s.IsSet("default" + t) { + conf = s.GetString("default" + t) glog.V(2).Info(fmt.Sprintf("Using default config file name '%s' for component %s", conf, component)) } else { // Default the config file name that we'll substitute to the name of the component @@ -147,68 +149,6 @@ func getConfigFiles(v *viper.Viper) map[string]string { return confmap } -// getPodSpecFiles finds which of the set of candidate podspec files exist -func getPodSpecFiles(v *viper.Viper) map[string]string { - podspecmap := make(map[string]string) - - for _, component := range v.GetStringSlice("components") { - s := v.Sub(component) - if s == nil { - continue - } - - // See if any of the candidate podspec files exist - podspec := findConfigFile(s.GetStringSlice("podspecs")) - if podspec == "" { - if s.IsSet("defaultpodspec") { - podspec = s.GetString("defaultpodspec") - glog.V(2).Info(fmt.Sprintf("Using default podspec file name '%s' for component %s", podspec, component)) - } else { - // Default the config file name that we'll substitute to the name of the component - printlnWarn(fmt.Sprintf("Missing podspec file for %s", component)) - podspec = component - } - } else { - glog.V(2).Info(fmt.Sprintf("Component %s uses podspec file '%s'", component, podspec)) - } - - podspecmap[component] = podspec - } - - return podspecmap -} - -// getUnitFiles finds which of the set of candidate unit files exist -func getUnitFiles(v *viper.Viper) map[string]string { - unitfilemap := make(map[string]string) - - for _, component := range v.GetStringSlice("components") { - s := v.Sub(component) - if s == nil { - continue - } - - // See if any of the candidate podspec files exist - unitfile := findConfigFile(s.GetStringSlice("unitfiles")) - if unitfile == "" { - if s.IsSet("defaultunitfile") { - unitfile = s.GetString("defaultunitfile") - glog.V(2).Info(fmt.Sprintf("Using default unit file name '%s' for component %s", unitfile, component)) - } else { - // Default the config file name that we'll substitute to the name of the component - printlnWarn(fmt.Sprintf("Missing unit file for %s", component)) - unitfile = component - } - } else { - glog.V(2).Info(fmt.Sprintf("Component %s uses unit file '%s'", component, unitfile)) - } - - unitfilemap[component] = unitfile - } - - return unitfilemap -} - // verifyBin checks that the binary specified is running func verifyBin(bin string) bool {