2021-09-09 11:12:25 -07:00
|
|
|
//go:build !windows
|
2019-03-13 04:14:30 -07:00
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
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
|
|
|
)
|
|
|
|
|
|
|
|
func (env *environment) isRunningAsRoot() bool {
|
2021-11-16 22:16:43 -08:00
|
|
|
defer env.trace(time.Now(), "isRunningAsRoot")
|
2019-03-13 04:14:30 -07:00
|
|
|
return os.Geteuid() == 0
|
|
|
|
}
|
2020-10-12 07:01:08 -07:00
|
|
|
|
|
|
|
func (env *environment) homeDir() string {
|
|
|
|
return os.Getenv("HOME")
|
|
|
|
}
|
2020-11-04 23:56:12 -08:00
|
|
|
|
2020-11-12 00:43:32 -08:00
|
|
|
func (env *environment) getWindowTitle(imageName, 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
|
|
|
|
|
|
|
func (env *environment) isWsl() bool {
|
2021-11-16 22:16:43 -08:00
|
|
|
defer env.trace(time.Now(), "isWsl")
|
2021-02-14 23:26:52 -08:00
|
|
|
// one way to check
|
|
|
|
// version := env.getFileContent("/proc/version")
|
|
|
|
// return strings.Contains(version, "microsoft")
|
|
|
|
// using env variable
|
|
|
|
return env.getenv("WSL_DISTRO_NAME") != ""
|
|
|
|
}
|
2021-05-22 07:50:34 -07:00
|
|
|
|
2021-12-22 08:28:15 -08:00
|
|
|
func (env *environment) isWsl2() bool {
|
|
|
|
defer env.trace(time.Now(), "isWsl2")
|
|
|
|
if !env.isWsl() {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
uname := env.getFileContent("/proc/sys/kernel/osrelease")
|
|
|
|
return strings.Contains(uname, "WSL2")
|
|
|
|
}
|
|
|
|
|
2021-05-22 07:50:34 -07:00
|
|
|
func (env *environment) getTerminalWidth() (int, error) {
|
2021-11-16 22:16:43 -08:00
|
|
|
defer env.trace(time.Now(), "getTerminalWidth")
|
2021-05-22 07:50:34 -07:00
|
|
|
width, err := terminal.Width()
|
2021-10-20 02:55:53 -07:00
|
|
|
if err != nil {
|
2021-11-16 22:16:43 -08:00
|
|
|
env.log(Error, "runCommand", 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
|
|
|
|
|
|
|
func (env *environment) getPlatform() string {
|
2022-01-18 11:19:16 -08:00
|
|
|
const key = "environment_platform"
|
|
|
|
if val, found := env.cache().get(key); found {
|
|
|
|
return val
|
|
|
|
}
|
|
|
|
var platform string
|
|
|
|
defer env.cache().set(key, platform, -1)
|
2022-01-18 00:48:47 -08:00
|
|
|
if wsl := env.getenv("WSL_DISTRO_NAME"); len(wsl) != 0 {
|
2022-01-18 11:19:16 -08:00
|
|
|
platform = strings.ToLower(wsl)
|
|
|
|
return platform
|
2022-01-18 00:48:47 -08:00
|
|
|
}
|
2022-01-18 11:19:16 -08:00
|
|
|
platform, _, _, _ = host.PlatformInformation()
|
|
|
|
return platform
|
2021-09-09 22:25:12 -07:00
|
|
|
}
|
2021-10-02 22:27:18 -07:00
|
|
|
|
|
|
|
func (env *environment) getCachePath() string {
|
2021-11-16 22:16:43 -08:00
|
|
|
defer env.trace(time.Now(), "getCachePath")
|
2021-10-02 22:27:18 -07:00
|
|
|
// get XDG_CACHE_HOME if present
|
|
|
|
if cachePath := returnOrBuildCachePath(env.getenv("XDG_CACHE_HOME")); len(cachePath) != 0 {
|
|
|
|
return cachePath
|
|
|
|
}
|
|
|
|
// HOME cache folder
|
|
|
|
if cachePath := returnOrBuildCachePath(env.homeDir() + "/.cache"); len(cachePath) != 0 {
|
|
|
|
return cachePath
|
|
|
|
}
|
|
|
|
return env.homeDir()
|
|
|
|
}
|
2021-11-24 04:47:30 -08:00
|
|
|
|
2021-12-04 13:11:25 -08:00
|
|
|
func (env *environment) getWindowsRegistryKeyValue(path string) (*windowsRegistryValue, error) {
|
|
|
|
return nil, errors.New("not implemented")
|
2021-11-24 04:47:30 -08:00
|
|
|
}
|
2021-12-22 08:28:15 -08:00
|
|
|
|
|
|
|
func (env *environment) inWSLSharedDrive() bool {
|
|
|
|
return env.isWsl() && strings.HasPrefix(env.getcwd(), "/mnt/")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (env *environment) convertToWindowsPath(path string) string {
|
|
|
|
windowsPath, err := env.runCommand("wslpath", "-w", path)
|
|
|
|
if err == nil {
|
|
|
|
return windowsPath
|
|
|
|
}
|
|
|
|
return path
|
|
|
|
}
|
|
|
|
|
|
|
|
func (env *environment) convertToLinuxPath(path string) string {
|
|
|
|
linuxPath, err := env.runCommand("wslpath", "-u", path)
|
|
|
|
if err == nil {
|
|
|
|
return linuxPath
|
|
|
|
}
|
|
|
|
return path
|
|
|
|
}
|
2021-12-26 08:17:44 -08:00
|
|
|
|
|
|
|
func (env *environment) getWifiNetwork() (*wifiInfo, error) {
|
|
|
|
return nil, errors.New("not implemented")
|
|
|
|
}
|