2024-05-14 22:34:41 +02:00
|
|
|
// Copyright 2024, Command Line Inc.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
2024-05-12 18:52:12 +02:00
|
|
|
package wavebase
|
|
|
|
|
|
|
|
import (
|
2024-05-16 09:29:58 +02:00
|
|
|
"context"
|
2024-05-12 18:52:12 +02:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io/fs"
|
2024-05-16 09:29:58 +02:00
|
|
|
"log"
|
2024-05-12 18:52:12 +02:00
|
|
|
"os"
|
2024-05-16 09:29:58 +02:00
|
|
|
"os/exec"
|
2024-05-21 00:28:47 +02:00
|
|
|
"path/filepath"
|
2024-05-16 09:29:58 +02:00
|
|
|
"runtime"
|
2024-05-12 18:52:12 +02:00
|
|
|
"strings"
|
|
|
|
"sync"
|
2024-05-16 09:29:58 +02:00
|
|
|
"time"
|
2024-05-21 00:28:47 +02:00
|
|
|
|
2024-06-15 23:59:14 +02:00
|
|
|
"github.com/alexflint/go-filemutex"
|
2024-05-12 18:52:12 +02:00
|
|
|
)
|
|
|
|
|
2024-08-01 08:47:33 +02:00
|
|
|
// set by main-server.go
|
|
|
|
var WaveVersion = "0.0.0"
|
|
|
|
|
2024-05-12 18:52:12 +02:00
|
|
|
const DefaultWaveHome = "~/.w2"
|
2024-07-18 03:42:49 +02:00
|
|
|
const DevWaveHome = "~/.w2-dev"
|
2024-05-12 18:52:12 +02:00
|
|
|
const WaveHomeVarName = "WAVETERM_HOME"
|
|
|
|
const WaveDevVarName = "WAVETERM_DEV"
|
2024-05-21 00:28:47 +02:00
|
|
|
const WaveLockFile = "waveterm.lock"
|
2024-07-18 00:24:43 +02:00
|
|
|
const DomainSocketBaseName = "wave.sock"
|
2024-07-26 01:41:34 +02:00
|
|
|
const JwtSecret = "waveterm" // TODO generate and store this
|
2024-05-12 18:52:12 +02:00
|
|
|
|
|
|
|
var baseLock = &sync.Mutex{}
|
|
|
|
var ensureDirCache = map[string]bool{}
|
|
|
|
|
|
|
|
func IsDevMode() bool {
|
|
|
|
pdev := os.Getenv(WaveDevVarName)
|
|
|
|
return pdev != ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetHomeDir() string {
|
2024-06-15 23:59:14 +02:00
|
|
|
homeVar, err := os.UserHomeDir()
|
|
|
|
if err != nil {
|
2024-05-12 18:52:12 +02:00
|
|
|
return "/"
|
|
|
|
}
|
|
|
|
return homeVar
|
|
|
|
}
|
|
|
|
|
|
|
|
func ExpandHomeDir(pathStr string) string {
|
|
|
|
if pathStr != "~" && !strings.HasPrefix(pathStr, "~/") {
|
|
|
|
return pathStr
|
|
|
|
}
|
|
|
|
homeDir := GetHomeDir()
|
|
|
|
if pathStr == "~" {
|
|
|
|
return homeDir
|
|
|
|
}
|
2024-06-15 23:59:14 +02:00
|
|
|
return filepath.Join(homeDir, pathStr[2:])
|
2024-05-12 18:52:12 +02:00
|
|
|
}
|
|
|
|
|
2024-05-16 09:29:58 +02:00
|
|
|
func ReplaceHomeDir(pathStr string) string {
|
|
|
|
homeDir := GetHomeDir()
|
|
|
|
if pathStr == homeDir {
|
|
|
|
return "~"
|
|
|
|
}
|
|
|
|
if strings.HasPrefix(pathStr, homeDir+"/") {
|
|
|
|
return "~" + pathStr[len(homeDir):]
|
|
|
|
}
|
|
|
|
return pathStr
|
|
|
|
}
|
|
|
|
|
2024-07-18 00:24:43 +02:00
|
|
|
func GetDomainSocketName() string {
|
|
|
|
return filepath.Join(GetWaveHomeDir(), DomainSocketBaseName)
|
|
|
|
}
|
|
|
|
|
2024-05-12 18:52:12 +02:00
|
|
|
func GetWaveHomeDir() string {
|
|
|
|
homeVar := os.Getenv(WaveHomeVarName)
|
|
|
|
if homeVar != "" {
|
|
|
|
return ExpandHomeDir(homeVar)
|
|
|
|
}
|
2024-07-18 03:42:49 +02:00
|
|
|
if IsDevMode() {
|
|
|
|
return ExpandHomeDir(DevWaveHome)
|
|
|
|
}
|
2024-05-12 18:52:12 +02:00
|
|
|
return ExpandHomeDir(DefaultWaveHome)
|
|
|
|
}
|
|
|
|
|
|
|
|
func EnsureWaveHomeDir() error {
|
|
|
|
return CacheEnsureDir(GetWaveHomeDir(), "wavehome", 0700, "wave home directory")
|
|
|
|
}
|
|
|
|
|
|
|
|
func CacheEnsureDir(dirName string, cacheKey string, perm os.FileMode, dirDesc string) error {
|
|
|
|
baseLock.Lock()
|
|
|
|
ok := ensureDirCache[cacheKey]
|
|
|
|
baseLock.Unlock()
|
|
|
|
if ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
err := TryMkdirs(dirName, perm, dirDesc)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
baseLock.Lock()
|
|
|
|
ensureDirCache[cacheKey] = true
|
|
|
|
baseLock.Unlock()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func TryMkdirs(dirName string, perm os.FileMode, dirDesc string) error {
|
|
|
|
info, err := os.Stat(dirName)
|
|
|
|
if errors.Is(err, fs.ErrNotExist) {
|
|
|
|
err = os.MkdirAll(dirName, perm)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("cannot make %s %q: %w", dirDesc, dirName, err)
|
|
|
|
}
|
|
|
|
info, err = os.Stat(dirName)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error trying to stat %s: %w", dirDesc, err)
|
|
|
|
}
|
|
|
|
if !info.IsDir() {
|
|
|
|
return fmt.Errorf("%s %q must be a directory", dirDesc, dirName)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2024-05-16 09:29:58 +02:00
|
|
|
|
|
|
|
var osLangOnce = &sync.Once{}
|
|
|
|
var osLang string
|
|
|
|
|
|
|
|
func determineLang() string {
|
|
|
|
ctx, cancelFn := context.WithTimeout(context.Background(), 2*time.Second)
|
|
|
|
defer cancelFn()
|
|
|
|
if runtime.GOOS == "darwin" {
|
|
|
|
out, err := exec.CommandContext(ctx, "defaults", "read", "-g", "AppleLocale").CombinedOutput()
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("error executing 'defaults read -g AppleLocale': %v\n", err)
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
strOut := string(out)
|
|
|
|
truncOut := strings.Split(strOut, "@")[0]
|
|
|
|
return strings.TrimSpace(truncOut) + ".UTF-8"
|
2024-06-22 09:41:49 +02:00
|
|
|
} else if runtime.GOOS == "win32" {
|
|
|
|
out, err := exec.CommandContext(ctx, "Get-Culture", "|", "select", "-exp", "Name").CombinedOutput()
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("error executing 'Get-Culture | select -exp Name': %v\n", err)
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return strings.TrimSpace(string(out)) + ".UTF-8"
|
2024-05-16 09:29:58 +02:00
|
|
|
} else {
|
|
|
|
// this is specifically to get the wavesrv LANG so waveshell
|
|
|
|
// on a remote uses the same LANG
|
|
|
|
return os.Getenv("LANG")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func DetermineLang() string {
|
|
|
|
osLangOnce.Do(func() {
|
|
|
|
osLang = determineLang()
|
|
|
|
})
|
|
|
|
return osLang
|
|
|
|
}
|
2024-05-21 00:28:47 +02:00
|
|
|
|
2024-06-22 09:41:49 +02:00
|
|
|
func DetermineLocale() string {
|
|
|
|
truncated := strings.Split(DetermineLang(), ".")[0]
|
|
|
|
if truncated == "" {
|
|
|
|
return "C"
|
|
|
|
}
|
|
|
|
return strings.Replace(truncated, "_", "-", -1)
|
|
|
|
}
|
|
|
|
|
2024-06-15 23:59:14 +02:00
|
|
|
func AcquireWaveLock() (*filemutex.FileMutex, error) {
|
2024-05-21 00:28:47 +02:00
|
|
|
homeDir := GetWaveHomeDir()
|
|
|
|
lockFileName := filepath.Join(homeDir, WaveLockFile)
|
|
|
|
log.Printf("[base] acquiring lock on %s\n", lockFileName)
|
2024-06-15 23:59:14 +02:00
|
|
|
m, err := filemutex.New(lockFileName)
|
2024-05-21 00:28:47 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-06-15 23:59:14 +02:00
|
|
|
|
|
|
|
err = m.TryLock()
|
|
|
|
return m, err
|
2024-05-21 00:28:47 +02:00
|
|
|
}
|