diff --git a/cmd/harvest/harvest.go b/cmd/harvest/harvest.go index 32945dfa7..055d0a4c4 100644 --- a/cmd/harvest/harvest.go +++ b/cmd/harvest/harvest.go @@ -88,8 +88,8 @@ func doManageCmd(cmd *cobra.Command, args []string) { err error ) opts.command = cmd.Name() - HarvestHomePath = conf.GetHarvestHomePath() - HarvestConfigPath = conf.GetDefaultHarvestConfigPath() + HarvestHomePath = conf.Path() + HarvestConfigPath = conf.Path(conf.HarvestYML) if opts.verbose { _ = cmd.Flags().Set("loglevel", "1") diff --git a/cmd/poller/options/options.go b/cmd/poller/options/options.go index 24bb03721..d70f01ac4 100644 --- a/cmd/poller/options/options.go +++ b/cmd/poller/options/options.go @@ -68,7 +68,6 @@ func SetPathsAndHostname(args *Options) { args.Hostname = hostname } - args.HomePath = conf.GetHarvestHomePath() - + args.HomePath = conf.Path() args.LogPath = conf.GetHarvestLogPath() } diff --git a/cmd/poller/poller.go b/cmd/poller/poller.go index 7c7eb490e..4f223221e 100644 --- a/cmd/poller/poller.go +++ b/cmd/poller/poller.go @@ -778,7 +778,7 @@ func Union2(hNode *node.Node, poller *conf.Poller) { newNode := node.NewS(yNode.Value) // this is the value that goes along with the key from yNode valNode := rootContent.Content[index+1] - //fmt.Printf("node type=%s val=%s %s\n", yNode.Value, valNode.Tag, valNode.Value) + // fmt.Printf("node type=%s val=%s %s\n", yNode.Value, valNode.Tag, valNode.Value) switch valNode.Tag { case "!!str", "!!bool": newNode.Content = []byte(valNode.Value) @@ -1163,7 +1163,7 @@ func (p *Poller) doZAPIsExist() error { } func startPoller(_ *cobra.Command, _ []string) { - //cmd.DebugFlags() // uncomment to print flags + // cmd.DebugFlags() // uncomment to print flags poller := &Poller{} poller.options = &args if poller.Init() != nil { @@ -1180,7 +1180,7 @@ var args = options.Options{ } func init() { - configPath := conf.GetDefaultHarvestConfigPath() + configPath := conf.Path(conf.HarvestYML) var flags = pollerCmd.Flags() flags.StringVarP(&args.Poller, "poller", "p", "", "Poller name as defined in config") diff --git a/cmd/tools/doctor/doctor.go b/cmd/tools/doctor/doctor.go index 5ad102164..a1842d958 100644 --- a/cmd/tools/doctor/doctor.go +++ b/cmd/tools/doctor/doctor.go @@ -181,7 +181,7 @@ func checkCollectorName(config conf.HarvestConfig) validation { func checkCustomYaml(confParent string) validation { valid := validation{isValid: true} - confDir := path.Join(conf.GetHarvestHomePath(), "conf") + confDir := conf.Path("conf") if confParent != "" { confDir = path.Join(confParent, "conf") } diff --git a/cmd/tools/grafana/grafana.go b/cmd/tools/grafana/grafana.go index fe5175649..2440b009a 100644 --- a/cmd/tools/grafana/grafana.go +++ b/cmd/tools/grafana/grafana.go @@ -70,7 +70,7 @@ type Folder struct { func adjustOptions() { opts.config = conf.ConfigPath(opts.config) - homePath = conf.GetHarvestHomePath() + homePath = conf.Path() opts.dirGrafanaFolderMap = make(map[string]*Folder) // When opt.addr starts with https don't change it @@ -171,7 +171,7 @@ func exportFiles(dir string, folder *Folder) error { fmt.Printf("error marshall dashboard [%s]: %v\n\n", uid, err) return err } - //#nosec G306 -- creating dashboards with group and other permissions of read are OK + // #nosec G306 -- creating dashboards with group and other permissions of read are OK if err = os.WriteFile(fp, data, 0644); err != nil { fmt.Printf("error write to [%s]: %v\n", fp, err) return err @@ -970,7 +970,7 @@ func doRequest(opts *options, method, url string, query map[string]interface{}) return nil, status, code, err } - //fmt.Printf("(debug) send request [%s]\n", request.URL.String()) + // fmt.Printf("(debug) send request [%s]\n", request.URL.String()) request.Header = opts.headers diff --git a/cmd/tools/rest/rest.go b/cmd/tools/rest/rest.go index 34c7da99d..383735c4e 100644 --- a/cmd/tools/rest/rest.go +++ b/cmd/tools/rest/rest.go @@ -579,7 +579,7 @@ func stderr(format string, a ...any) { } func init() { - configPath := conf.GetDefaultHarvestConfigPath() + configPath := conf.Path(conf.HarvestYML) Cmd.AddCommand(showCmd) flags := Cmd.PersistentFlags() diff --git a/cmd/tools/zapi/export.go b/cmd/tools/zapi/export.go index e01a93562..74af3b3d7 100644 --- a/cmd/tools/zapi/export.go +++ b/cmd/tools/zapi/export.go @@ -89,7 +89,7 @@ func exportCounters(item *node.Node, c *client.Client, args *Args) error { */ var fp []string - harvestHomePath = conf.GetHarvestHomePath() + harvestHomePath = conf.Path() fp = append(fp, harvestHomePath) fp = append(fp, "conf/") fp = append(fp, "zapiperf/") @@ -100,7 +100,7 @@ func exportCounters(item *node.Node, c *client.Client, args *Args) error { fp = append(fp, "7mode") } - //fp = append(fp, fmt.Sprintf("%d.%d.%d", c.System.Version[0], c.System.Version[1], c.System.Version[2])) + // fp = append(fp, fmt.Sprintf("%d.%d.%d", c.System.Version[0], c.System.Version[1], c.System.Version[2])) fp = append(fp, "9.8.0") fp = append(fp, strings.ReplaceAll(args.Object, ":", "_")+".yaml") diff --git a/cmd/tools/zapi/zapi.go b/cmd/tools/zapi/zapi.go index 0997bff32..3b583519b 100644 --- a/cmd/tools/zapi/zapi.go +++ b/cmd/tools/zapi/zapi.go @@ -331,7 +331,7 @@ func getData(c *client.Client, args *Args) (*node.Node, error) { var args = &Args{} func init() { - configPath := conf.GetDefaultHarvestConfigPath() + configPath := conf.Path(conf.HarvestYML) Cmd.AddCommand(showCmd, exportCmd) flags := Cmd.PersistentFlags() flags.StringVarP(&args.Poller, "poller", "p", "", "name of poller (cluster), as defined in your harvest config") diff --git a/pkg/conf/conf.go b/pkg/conf/conf.go index 116d655f3..ce16a2207 100644 --- a/pkg/conf/conf.go +++ b/pkg/conf/conf.go @@ -13,11 +13,9 @@ import ( "gopkg.in/yaml.v3" "log" "os" - "path" "path/filepath" "regexp" "strconv" - "strings" ) var Config = HarvestConfig{} @@ -170,25 +168,13 @@ func PollerNamed(name string) (*Poller, error) { return poller, nil } -// GetDefaultHarvestConfigPath returns the absolute path of the default harvest config file. -func GetDefaultHarvestConfigPath() string { - configPath := os.Getenv("HARVEST_CONF") - if configPath == "" { - return "./" + HarvestYML - } - return path.Join(configPath, HarvestYML) -} - -// GetHarvestHomePath returns the value of the env var HARVEST_CONF or ./ -func GetHarvestHomePath() string { - harvestConf := os.Getenv("HARVEST_CONF") - if harvestConf == "" { - return "./" - } - if strings.HasSuffix(harvestConf, "/") { - return harvestConf - } - return harvestConf + "/" +// Path joins a set of path elems into a single path. +// The final path will be relative to the HARVEST_CONF environment variable +// or ./ when the environment variable is not set +func Path(elem ...string) string { + home := os.Getenv("HARVEST_CONF") + paths := append([]string{home}, elem...) + return filepath.Join(paths...) } func GetHarvestLogPath() string {