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"
|
2022-05-15 22:27:50 -07:00
|
|
|
"syscall"
|
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-05-07 07:43:24 -07: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) {
|
2022-09-12 03:55:57 -07:00
|
|
|
return "", &NotImplemented{}
|
2020-11-04 23:56:12 -08:00
|
|
|
}
|
2021-02-14 23:26:52 -08:00
|
|
|
|
2022-01-26 01:23:18 -08:00
|
|
|
func (env *ShellEnvironment) IsWsl() bool {
|
2022-05-07 07:43:24 -07: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-05-07 07:43:24 -07:00
|
|
|
defer env.Trace(time.Now(), "IsWsl2")
|
2022-01-23 12:37:51 -08:00
|
|
|
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-05-07 07:43:24 -07: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-05-12 11:12:25 -07: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-03-18 12:40:40 -07:00
|
|
|
platform = strings.Split(strings.ToLower(wsl), "-")[0]
|
|
|
|
return platform
|
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-05-07 07:43:24 -07: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) {
|
2022-09-12 03:55:57 -07:00
|
|
|
return nil, &NotImplemented{}
|
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-10-29 06:33:25 -07:00
|
|
|
if !env.IsWsl2() {
|
2022-08-29 00:43:43 -07:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
windowsPath := env.ConvertToWindowsPath(env.Pwd())
|
2022-08-31 22:44:27 -07:00
|
|
|
return !strings.HasPrefix(windowsPath, `//wsl.localhost/`) && !strings.HasPrefix(windowsPath, `//wsl$/`)
|
2021-12-22 08:28:15 -08:00
|
|
|
}
|
|
|
|
|
2022-01-26 01:23:18 -08:00
|
|
|
func (env *ShellEnvironment) ConvertToWindowsPath(path string) string {
|
2022-08-29 06:55:58 -07:00
|
|
|
windowsPath, err := env.RunCommand("wslpath", "-m", 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-07-13 23:00:18 -07:00
|
|
|
if linuxPath, err := env.RunCommand("wslpath", "-u", path); err == nil {
|
2021-12-22 08:28:15 -08:00
|
|
|
return linuxPath
|
|
|
|
}
|
|
|
|
return path
|
|
|
|
}
|
2021-12-26 08:17:44 -08:00
|
|
|
|
2022-02-07 09:50:52 -08:00
|
|
|
func (env *ShellEnvironment) LookWinAppPath(file string) (string, error) {
|
|
|
|
return "", errors.New("not relevant")
|
|
|
|
}
|
2022-05-15 22:27:50 -07:00
|
|
|
|
|
|
|
func (env *ShellEnvironment) DirIsWritable(path string) bool {
|
2022-10-13 11:20:02 -07:00
|
|
|
defer env.Trace(time.Now(), "DirIsWritable", path)
|
2022-05-15 22:27:50 -07:00
|
|
|
info, err := os.Stat(path)
|
|
|
|
if err != nil {
|
|
|
|
env.Log(Error, "DirIsWritable", err.Error())
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if !info.IsDir() {
|
|
|
|
env.Log(Error, "DirIsWritable", "Path isn't a directory")
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the user bit is enabled in file permission
|
|
|
|
if info.Mode().Perm()&(1<<(uint(7))) == 0 {
|
|
|
|
env.Log(Error, "DirIsWritable", "Write permission bit is not set on this file for user")
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
var stat syscall.Stat_t
|
|
|
|
if err = syscall.Stat(path, &stat); err != nil {
|
|
|
|
env.Log(Error, "DirIsWritable", err.Error())
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if uint32(os.Geteuid()) != stat.Uid {
|
|
|
|
env.Log(Error, "DirIsWritable", "User doesn't have permission to write to this directory")
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
2022-09-12 03:55:57 -07:00
|
|
|
|
|
|
|
func (env *ShellEnvironment) Connection(connectionType ConnectionType) (*Connection, error) {
|
|
|
|
// added to disable the linting error, we can implement this later
|
|
|
|
if len(env.networks) == 0 {
|
|
|
|
return nil, &NotImplemented{}
|
|
|
|
}
|
|
|
|
return nil, &NotImplemented{}
|
|
|
|
}
|