oh-my-posh/src/environment/shell_unix.go

167 lines
4.2 KiB
Go
Raw Normal View History

//go:build !windows
2019-03-13 04:14:30 -07: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"
"strings"
"syscall"
"time"
"github.com/shirou/gopsutil/v3/host"
terminal "github.com/wayneashleyberry/terminal-dimensions"
2019-03-13 04:14:30 -07: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
}
func (env *ShellEnvironment) Home() string {
return os.Getenv("HOME")
}
2020-11-04 23:56:12 -08:00
func (env *ShellEnvironment) QueryWindowTitles(processName, windowTitleRegex string) (string, error) {
return "", &NotImplemented{}
2020-11-04 23:56:12 -08:00
}
func (env *ShellEnvironment) IsWsl() bool {
2022-05-07 07:43:24 -07:00
defer env.Trace(time.Now(), "IsWsl")
// one way to check
// version := env.FileContent("/proc/version")
// return strings.Contains(version, "microsoft")
// using env variable
return env.Getenv("WSL_DISTRO_NAME") != ""
}
func (env *ShellEnvironment) IsWsl2() bool {
2022-05-07 07:43:24 -07:00
defer env.Trace(time.Now(), "IsWsl2")
if !env.IsWsl() {
return false
}
uname := env.FileContent("/proc/sys/kernel/osrelease")
return strings.Contains(uname, "WSL2")
}
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
}
width, err := terminal.Width()
if err != nil {
env.Log(Error, "TerminalWidth", err.Error())
}
return int(width), err
}
2021-09-09 22:25:12 -07:00
func (env *ShellEnvironment) Platform() string {
const key = "environment_platform"
if val, found := env.Cache().Get(key); found {
return val
}
var platform string
defer func() {
env.Cache().Set(key, platform, -1)
}()
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
}
platform, _, _, _ = host.PlatformInformation()
if platform == "arch" {
// validate for Manjaro
lsbInfo := env.FileContent("/etc/lsb-release")
if strings.Contains(strings.ToLower(lsbInfo), "manjaro") {
platform = "manjaro"
}
}
return platform
2021-09-09 22:25:12 -07:00
}
func (env *ShellEnvironment) CachePath() string {
2022-05-07 07:43:24 -07:00
defer env.Trace(time.Now(), "CachePath")
// 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.Home() + "/.cache"); len(cachePath) != 0 {
return cachePath
}
return env.Home()
}
2021-11-24 04:47:30 -08:00
func (env *ShellEnvironment) WindowsRegistryKeyValue(path string) (*WindowsRegistryValue, error) {
return nil, &NotImplemented{}
2021-11-24 04:47:30 -08:00
}
func (env *ShellEnvironment) InWSLSharedDrive() bool {
if !env.IsWsl2() {
return false
}
windowsPath := env.ConvertToWindowsPath(env.Pwd())
return !strings.HasPrefix(windowsPath, `//wsl.localhost/`) && !strings.HasPrefix(windowsPath, `//wsl$/`)
}
func (env *ShellEnvironment) ConvertToWindowsPath(path string) string {
windowsPath, err := env.RunCommand("wslpath", "-m", path)
if err == nil {
return windowsPath
}
return path
}
func (env *ShellEnvironment) ConvertToLinuxPath(path string) string {
if linuxPath, err := env.RunCommand("wslpath", "-u", path); err == nil {
return linuxPath
}
return path
}
func (env *ShellEnvironment) LookWinAppPath(file string) (string, error) {
return "", errors.New("not relevant")
}
func (env *ShellEnvironment) DirIsWritable(path string) bool {
defer env.Trace(time.Now(), "DirIsWritable", path)
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
}
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{}
}