From a25b8eefded6231b247d3eebe98c84363996674f Mon Sep 17 00:00:00 2001 From: Dmitry Filimonov Date: Mon, 18 Jan 2021 14:04:43 -0800 Subject: [PATCH] improves homebrew support --- pkg/cli/cli.go | 34 ++++++++++++++++++++++++++++++++++ pkg/config/config.go | 8 ++++---- 2 files changed, 38 insertions(+), 4 deletions(-) diff --git a/pkg/cli/cli.go b/pkg/cli/cli.go index e0bb91b78e..95a5eceb40 100644 --- a/pkg/cli/cli.go +++ b/pkg/cli/cli.go @@ -5,7 +5,9 @@ import ( "flag" "fmt" "os" + "path/filepath" "reflect" + "runtime" "strconv" "strings" "time" @@ -47,6 +49,8 @@ func populateFlagSet(obj interface{}, flagSet *flag.FlagSet) { t := reflect.TypeOf(v.Interface()) num := t.NumField() + installPrefix := getInstallPrefix() + for i := 0; i < num; i++ { field := t.Field(i) fieldV := v.Field(i) @@ -68,6 +72,7 @@ func populateFlagSet(obj interface{}, flagSet *flag.FlagSet) { flagSet.Var(val2, nameVal, descVal) case reflect.TypeOf(""): val := fieldV.Addr().Interface().(*string) + defaultValStr := strings.ReplaceAll(defaultValStr, "", installPrefix) flagSet.StringVar(val, nameVal, defaultValStr, descVal) case reflect.TypeOf(true): val := fieldV.Addr().Interface().(*bool) @@ -104,6 +109,35 @@ func populateFlagSet(obj interface{}, flagSet *flag.FlagSet) { } } +// on mac pyroscope is usually installed via homebrew. homebrew installs under a prefix +// this is logic to figure out what prefix it is +func getInstallPrefix() string { + if runtime.GOOS != "darwin" { + return "" + } + + executablePath, err := os.Executable() + if err != nil { + // TODO: figure out what kind of errors might happen, handle it + return "" + } + cellarPath := filepath.Clean(filepath.Join(resolvePath(executablePath), "../../../..")) + + if !strings.HasSuffix(cellarPath, "Cellar") { + // looks like it's not installed via homebrew + return "" + } + + return filepath.Clean(filepath.Join(cellarPath, "../")) +} + +func resolvePath(path string) string { + if res, err := filepath.EvalSymlinks(path); err == nil { + return res + } + return path +} + func printUsage(c *ffcli.Command) string { return gradientBanner() + "\n" + DefaultUsageFunc(c) } diff --git a/pkg/config/config.go b/pkg/config/config.go index b24c4fcf4b..cd803fe0f8 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -14,7 +14,7 @@ type Config struct { } type Agent struct { - Config string `def:"/etc/pyroscope/agent.yml" desc:"location of config file"` + Config string `def:"/etc/pyroscope/agent.yml" desc:"location of config file"` LogLevel string `def:"info", desc:"debug|info|warn|error"` // AgentCMD []string @@ -23,16 +23,16 @@ type Agent struct { ServerAddress string `def:"http://localhost:4040" desc:"address of the pyroscope server"` UpstreamThreads int `def:"4"` UpstreamRequestTimeout time.Duration `def:"10s"` - UNIXSocketPath string `def:"/var/run/pyroscope-agent.sock" desc:"path to a UNIX socket file"` + UNIXSocketPath string `def:"/var/run/pyroscope-agent.sock" desc:"path to a UNIX socket file"` } type Server struct { - Config string `def:"/etc/pyroscope/server.yml" desc:"location of config file"` + Config string `def:"/etc/pyroscope/server.yml" desc:"location of config file"` LogLevel string `def:"info", desc:"debug|info|warn|error"` // TODO: fix, doesn't see to work BadgerLogLevel string `def:"error", desc:"debug|info|warn|error"` - StoragePath string `def:"/var/lib/pyroscope"` + StoragePath string `def:"/var/lib/pyroscope"` ApiBindAddr string `def:":4040"` CacheDimensionSize int `def:"1000"`