2021-09-09 11:12:25 -07:00
|
|
|
//go:build !windows
|
2019-03-13 04:14:30 -07:00
|
|
|
|
2022-01-26 01:23:18 -08:00
|
|
|
package environment
|
2019-03-13 04:14:30 -07:00
|
|
|
|
|
|
|
import (
|
2020-11-04 23:56:12 -08:00
|
|
|
"errors"
|
2019-03-13 04:14:30 -07:00
|
|
|
"os"
|
2021-12-22 08:28:15 -08:00
|
|
|
"strings"
|
2021-08-01 06:25:15 -07:00
|
|
|
"time"
|
2021-05-22 07:50:34 -07:00
|
|
|
|
2021-11-13 14:46:06 -08:00
|
|
|
"github.com/shirou/gopsutil/v3/host"
|
2021-05-22 07:50:34 -07:00
|
|
|
terminal "github.com/wayneashleyberry/terminal-dimensions"
|
2019-03-13 04:14:30 -07:00
|
|
|
)
|
|
|
|
|
2022-01-26 01:23:18 -08:00
|
|
|
func (env *ShellEnvironment) Root() bool {
|
2022-01-23 12:37:51 -08:00
|
|
|
defer env.trace(time.Now(), "Root")
|
2019-03-13 04:14:30 -07:00
|
|
|
return os.Geteuid() == 0
|
|
|
|
}
|
2020-10-12 07:01:08 -07:00
|
|
|
|
2022-01-26 01:23:18 -08:00
|
|
|
func (env *ShellEnvironment) Home() string {
|
2020-10-12 07:01:08 -07:00
|
|
|
return os.Getenv("HOME")
|
|
|
|
}
|
2020-11-04 23:56:12 -08:00
|
|
|
|
2022-03-05 23:41:02 -08:00
|
|
|
func (env *ShellEnvironment) QueryWindowTitles(processName, windowTitleRegex string) (string, error) {
|
2020-11-04 23:56:12 -08:00
|
|
|
return "", errors.New("not implemented")
|
|
|
|
}
|
2021-02-14 23:26:52 -08:00
|
|
|
|
2022-01-26 01:23:18 -08:00
|
|
|
func (env *ShellEnvironment) IsWsl() bool {
|
2022-01-23 12:37:51 -08:00
|
|
|
defer env.trace(time.Now(), "IsWsl")
|
2021-02-14 23:26:52 -08:00
|
|
|
// one way to check
|
2022-01-23 12:37:51 -08:00
|
|
|
// version := env.FileContent("/proc/version")
|
2021-02-14 23:26:52 -08:00
|
|
|
// return strings.Contains(version, "microsoft")
|
|
|
|
// using env variable
|
2022-01-23 12:37:51 -08:00
|
|
|
return env.Getenv("WSL_DISTRO_NAME") != ""
|
2021-02-14 23:26:52 -08:00
|
|
|
}
|
2021-05-22 07:50:34 -07:00
|
|
|
|
2022-01-26 01:23:18 -08:00
|
|
|
func (env *ShellEnvironment) IsWsl2() bool {
|
2022-01-23 12:37:51 -08:00
|
|
|
defer env.trace(time.Now(), "IsWsl2")
|
|
|
|
if !env.IsWsl() {
|
2021-12-22 08:28:15 -08:00
|
|
|
return false
|
|
|
|
}
|
2022-01-23 12:37:51 -08:00
|
|
|
uname := env.FileContent("/proc/sys/kernel/osrelease")
|
2021-12-22 08:28:15 -08:00
|
|
|
return strings.Contains(uname, "WSL2")
|
|
|
|
}
|
|
|
|
|
2022-01-26 01:23:18 -08:00
|
|
|
func (env *ShellEnvironment) TerminalWidth() (int, error) {
|
2022-01-23 12:37:51 -08:00
|
|
|
defer env.trace(time.Now(), "TerminalWidth")
|
2022-03-12 13:04:08 -08:00
|
|
|
if env.CmdFlags.TerminalWidth != 0 {
|
|
|
|
return env.CmdFlags.TerminalWidth, nil
|
2022-02-06 02:01:46 -08:00
|
|
|
}
|
2021-05-22 07:50:34 -07:00
|
|
|
width, err := terminal.Width()
|
2021-10-20 02:55:53 -07:00
|
|
|
if err != nil {
|
2022-02-06 02:01:46 -08:00
|
|
|
env.log(Error, "TerminalWidth", err.Error())
|
2021-10-20 02:55:53 -07:00
|
|
|
}
|
2021-05-22 07:50:34 -07:00
|
|
|
return int(width), err
|
|
|
|
}
|
2021-09-09 22:25:12 -07:00
|
|
|
|
2022-01-26 01:23:18 -08:00
|
|
|
func (env *ShellEnvironment) Platform() string {
|
2022-01-18 11:19:16 -08:00
|
|
|
const key = "environment_platform"
|
2022-01-23 12:37:51 -08:00
|
|
|
if val, found := env.Cache().Get(key); found {
|
2022-01-18 11:19:16 -08:00
|
|
|
return val
|
|
|
|
}
|
|
|
|
var platform string
|
2022-01-18 13:40:50 -08:00
|
|
|
defer func() {
|
2022-01-23 12:37:51 -08:00
|
|
|
env.Cache().Set(key, platform, -1)
|
2022-01-18 13:40:50 -08:00
|
|
|
}()
|
2022-01-23 12:37:51 -08:00
|
|
|
if wsl := env.Getenv("WSL_DISTRO_NAME"); len(wsl) != 0 {
|
2022-01-18 11:19:16 -08:00
|
|
|
platform = strings.ToLower(wsl)
|
2022-02-17 03:41:50 -08:00
|
|
|
return strings.Split(platform, "-")[0]
|
2022-01-18 00:48:47 -08:00
|
|
|
}
|
2022-01-18 11:19:16 -08:00
|
|
|
platform, _, _, _ = host.PlatformInformation()
|
2022-02-05 08:43:49 -08:00
|
|
|
if platform == "arch" {
|
|
|
|
// validate for Manjaro
|
2022-02-06 01:32:16 -08:00
|
|
|
lsbInfo := env.FileContent("/etc/lsb-release")
|
|
|
|
if strings.Contains(strings.ToLower(lsbInfo), "manjaro") {
|
2022-02-05 08:43:49 -08:00
|
|
|
platform = "manjaro"
|
|
|
|
}
|
|
|
|
}
|
2022-01-18 11:19:16 -08:00
|
|
|
return platform
|
2021-09-09 22:25:12 -07:00
|
|
|
}
|
2021-10-02 22:27:18 -07:00
|
|
|
|
2022-01-26 01:23:18 -08:00
|
|
|
func (env *ShellEnvironment) CachePath() string {
|
2022-01-23 12:37:51 -08:00
|
|
|
defer env.trace(time.Now(), "CachePath")
|
2021-10-02 22:27:18 -07:00
|
|
|
// get XDG_CACHE_HOME if present
|
2022-01-23 12:37:51 -08:00
|
|
|
if cachePath := returnOrBuildCachePath(env.Getenv("XDG_CACHE_HOME")); len(cachePath) != 0 {
|
2021-10-02 22:27:18 -07:00
|
|
|
return cachePath
|
|
|
|
}
|
|
|
|
// HOME cache folder
|
2022-01-23 12:37:51 -08:00
|
|
|
if cachePath := returnOrBuildCachePath(env.Home() + "/.cache"); len(cachePath) != 0 {
|
2021-10-02 22:27:18 -07:00
|
|
|
return cachePath
|
|
|
|
}
|
2022-01-23 12:37:51 -08:00
|
|
|
return env.Home()
|
2021-10-02 22:27:18 -07:00
|
|
|
}
|
2021-11-24 04:47:30 -08:00
|
|
|
|
2022-01-26 01:23:18 -08:00
|
|
|
func (env *ShellEnvironment) WindowsRegistryKeyValue(path string) (*WindowsRegistryValue, error) {
|
2021-12-04 13:11:25 -08:00
|
|
|
return nil, errors.New("not implemented")
|
2021-11-24 04:47:30 -08:00
|
|
|
}
|
2021-12-22 08:28:15 -08:00
|
|
|
|
2022-01-26 01:23:18 -08:00
|
|
|
func (env *ShellEnvironment) InWSLSharedDrive() bool {
|
2022-01-23 12:37:51 -08:00
|
|
|
return env.IsWsl() && strings.HasPrefix(env.Pwd(), "/mnt/")
|
2021-12-22 08:28:15 -08:00
|
|
|
}
|
|
|
|
|
2022-01-26 01:23:18 -08:00
|
|
|
func (env *ShellEnvironment) ConvertToWindowsPath(path string) string {
|
2022-01-23 12:37:51 -08:00
|
|
|
windowsPath, err := env.RunCommand("wslpath", "-w", path)
|
2021-12-22 08:28:15 -08:00
|
|
|
if err == nil {
|
|
|
|
return windowsPath
|
|
|
|
}
|
|
|
|
return path
|
|
|
|
}
|
|
|
|
|
2022-01-26 01:23:18 -08:00
|
|
|
func (env *ShellEnvironment) ConvertToLinuxPath(path string) string {
|
2022-01-23 12:37:51 -08:00
|
|
|
linuxPath, err := env.RunCommand("wslpath", "-u", path)
|
2021-12-22 08:28:15 -08:00
|
|
|
if err == nil {
|
|
|
|
return linuxPath
|
|
|
|
}
|
|
|
|
return path
|
|
|
|
}
|
2021-12-26 08:17:44 -08:00
|
|
|
|
2022-01-26 01:23:18 -08:00
|
|
|
func (env *ShellEnvironment) WifiNetwork() (*WifiInfo, error) {
|
2021-12-26 08:17:44 -08:00
|
|
|
return nil, errors.New("not implemented")
|
|
|
|
}
|
2022-02-07 09:50:52 -08:00
|
|
|
|
|
|
|
func (env *ShellEnvironment) LookWinAppPath(file string) (string, error) {
|
|
|
|
return "", errors.New("not relevant")
|
|
|
|
}
|