Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: simplify conf path management #2325

Merged
merged 2 commits into from
Aug 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions cmd/harvest/harvest.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
3 changes: 1 addition & 2 deletions cmd/poller/options/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ func SetPathsAndHostname(args *Options) {
args.Hostname = hostname
}

args.HomePath = conf.GetHarvestHomePath()

args.HomePath = conf.Path()
args.LogPath = conf.GetHarvestLogPath()
}
6 changes: 3 additions & 3 deletions cmd/poller/poller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand All @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion cmd/tools/doctor/doctor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down
6 changes: 3 additions & 3 deletions cmd/tools/grafana/grafana.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion cmd/tools/rest/rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions cmd/tools/zapi/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -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/")
Expand All @@ -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")

Expand Down
2 changes: 1 addition & 1 deletion cmd/tools/zapi/zapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
28 changes: 7 additions & 21 deletions pkg/conf/conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,9 @@ import (
"gopkg.in/yaml.v3"
"log"
"os"
"path"
"path/filepath"
"regexp"
"strconv"
"strings"
)

var Config = HarvestConfig{}
Expand Down Expand Up @@ -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 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See if below makes sense. This will avoid HasSuffix check and provide a clean output path if elem has a relative path.

func Path(elem ...string) string {
	home := os.Getenv("HARVEST_CONF")
	if home == "" {
		return filepath.Join(elem...)
	}
	allPaths := append([]string{home}, elem...)
	return filepath.Join(allPaths...)
}

home := os.Getenv("HARVEST_CONF")
paths := append([]string{home}, elem...)
return filepath.Join(paths...)
}

func GetHarvestLogPath() string {
Expand Down
Loading