5
0
mirror of https://github.com/cwinfo/matterbridge.git synced 2024-11-23 07:01:41 +00:00
matterbridge/vendor/github.com/google/gops/internal/internal.go

72 lines
1.5 KiB
Go
Raw Normal View History

// Copyright 2017 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
2017-03-23 22:28:55 +00:00
package internal
import (
"errors"
"io/ioutil"
"os"
"os/user"
"path/filepath"
"runtime"
"strconv"
2017-03-23 22:28:55 +00:00
"strings"
)
const gopsConfigDirEnvKey = "GOPS_CONFIG_DIR"
2017-03-23 22:28:55 +00:00
func ConfigDir() (string, error) {
if configDir := os.Getenv(gopsConfigDirEnvKey); configDir != "" {
return configDir, nil
}
if osUserConfigDir := getOSUserConfigDir(); osUserConfigDir != "" {
return filepath.Join(osUserConfigDir, "gops"), nil
}
2017-03-23 22:28:55 +00:00
if runtime.GOOS == "windows" {
return filepath.Join(os.Getenv("APPDATA"), "gops"), nil
}
if xdgConfigDir := os.Getenv("XDG_CONFIG_HOME"); xdgConfigDir != "" {
return filepath.Join(xdgConfigDir, "gops"), nil
}
2017-03-23 22:28:55 +00:00
homeDir := guessUnixHomeDir()
if homeDir == "" {
return "", errors.New("unable to get current user home directory: os/user lookup failed; $HOME is empty")
}
return filepath.Join(homeDir, ".config", "gops"), nil
}
func guessUnixHomeDir() string {
usr, err := user.Current()
if err == nil {
return usr.HomeDir
}
return os.Getenv("HOME")
}
func PIDFile(pid int) (string, error) {
gopsdir, err := ConfigDir()
if err != nil {
return "", err
}
return filepath.Join(gopsdir, strconv.Itoa(pid)), nil
2017-03-23 22:28:55 +00:00
}
func GetPort(pid int) (string, error) {
portfile, err := PIDFile(pid)
if err != nil {
return "", err
}
b, err := ioutil.ReadFile(portfile)
if err != nil {
return "", err
}
port := strings.TrimSpace(string(b))
return port, nil
}