diff options
| author | Mistivia <i@mistivia.com> | 2025-11-02 15:27:18 +0800 |
|---|---|---|
| committer | Mistivia <i@mistivia.com> | 2025-11-02 15:27:18 +0800 |
| commit | e9c24f4af7ed56760f6db7941827d09f6db9020b (patch) | |
| tree | 62128c43b883ce5e3148113350978755779bb5de /teleirc/matterbridge/vendor/github.com/google/gops/internal/internal.go | |
| parent | 58d5e7cfda4781d8a57ec52aefd02983835c301a (diff) | |
add matterbridge
Diffstat (limited to 'teleirc/matterbridge/vendor/github.com/google/gops/internal/internal.go')
| -rw-r--r-- | teleirc/matterbridge/vendor/github.com/google/gops/internal/internal.go | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/teleirc/matterbridge/vendor/github.com/google/gops/internal/internal.go b/teleirc/matterbridge/vendor/github.com/google/gops/internal/internal.go new file mode 100644 index 0000000..7fc162a --- /dev/null +++ b/teleirc/matterbridge/vendor/github.com/google/gops/internal/internal.go @@ -0,0 +1,71 @@ +// 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" + "io/ioutil" + "os" + "os/user" + "path/filepath" + "runtime" + "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 + } + + 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 + } + + 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 := ioutil.ReadFile(portfile) + if err != nil { + return "", err + } + port := strings.TrimSpace(string(b)) + return port, nil +} |
