mirror of
https://github.com/42wim/matterbridge.git
synced 2024-11-30 06:32:01 -08:00
08779c2909
* Update dependencies
62 lines
1.3 KiB
Go
62 lines
1.3 KiB
Go
// 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.
|
|
|
|
package internal
|
|
|
|
import (
|
|
"errors"
|
|
"os"
|
|
"os/user"
|
|
"path/filepath"
|
|
"strconv"
|
|
"strings"
|
|
)
|
|
|
|
const gopsConfigDirEnvKey = "GOPS_CONFIG_DIR"
|
|
|
|
func ConfigDir() (string, error) {
|
|
if configDir := os.Getenv(gopsConfigDirEnvKey); configDir != "" {
|
|
return configDir, nil
|
|
}
|
|
|
|
if userConfigDir, err := os.UserConfigDir(); err == nil {
|
|
return filepath.Join(userConfigDir, "gops"), nil
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
func GetPort(pid int) (string, error) {
|
|
portfile, err := PIDFile(pid)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
b, err := os.ReadFile(portfile)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
port := strings.TrimSpace(string(b))
|
|
return port, nil
|
|
}
|