1
0
mirror of https://github.com/google/gops.git synced 2024-11-24 08:22:25 +02:00

make ConfigDir configurable (#61)

make ConfigDir configurable

* internal: retrieve ConfigDir from GOPS_CONFIG_DIR environment variable

* make code graceful

* code optimize
This commit is contained in:
Rambone 2017-11-13 15:52:04 +08:00 committed by Emmanuel T Odeke
parent ce0552a598
commit dcc978d6e8
5 changed files with 58 additions and 4 deletions

View File

@ -45,6 +45,9 @@ func main() {
}
```
Otherwise, you could set `GOPS_CONFIG_DIR` environment variables to assign your config dir.
Default, gops will use the current user's home directory(AppData on windows).
### Manual
It is possible to use gops tool both in local and remote mode.

View File

@ -44,6 +44,11 @@ type Options struct {
// Optional.
Addr string
// ConfigDir is the directory to store the configuration file,
// PID of the gops process, filename, port as well as content.
// Optional.
ConfigDir string
// ShutdownCleanup automatically cleans up resources if the
// running process receives an interrupt. Otherwise, users
// can call Close before shutting down.
@ -68,11 +73,17 @@ func Listen(opts Options) error {
return fmt.Errorf("gops: agent already listening at: %v", listener.Addr())
}
gopsdir, err := internal.ConfigDir()
if err != nil {
return err
// new
gopsdir := opts.ConfigDir
if gopsdir == "" {
cfgDir, err := internal.ConfigDir()
if err != nil {
return err
}
gopsdir = cfgDir
}
err = os.MkdirAll(gopsdir, os.ModePerm)
err := os.MkdirAll(gopsdir, os.ModePerm)
if err != nil {
return err
}

View File

@ -32,6 +32,17 @@ func TestAgentClose(t *testing.T) {
}
}
func TestUseCustomConfigDir(t *testing.T) {
err := Listen(Options{
ConfigDir: os.TempDir(),
ShutdownCleanup: true,
})
if err != nil {
t.Fatal(err)
}
Close()
}
func TestAgentListenMultipleClose(t *testing.T) {
err := Listen(Options{})
if err != nil {

View File

@ -15,7 +15,13 @@ import (
"strings"
)
const gopsConfigDirEnvKey = "GOPS_CONFIG_DIR"
func ConfigDir() (string, error) {
if configDir := os.Getenv(gopsConfigDirEnvKey); configDir != "" {
return configDir, nil
}
if runtime.GOOS == "windows" {
return filepath.Join(os.Getenv("APPDATA"), "gops"), nil
}

23
internal/internal_test.go Normal file
View File

@ -0,0 +1,23 @@
package internal
import (
"os"
"testing"
)
func TestConfigDir(t *testing.T) {
key := gopsConfigDirEnvKey
oldDir := os.Getenv(key)
defer os.Setenv(key, oldDir)
newDir := "foo-bar"
os.Setenv(key, newDir)
configDir, err := ConfigDir()
if err != nil {
t.Fatal(err)
}
if g, w := configDir, newDir; g != w {
t.Errorf("ConfigDir: got=%v want=%v", g, w)
}
}