mirror of
https://github.com/wavetermdev/waveterm.git
synced 2025-01-04 18:59:08 +01:00
63 lines
1.2 KiB
Go
63 lines
1.2 KiB
Go
|
// Copyright 2023, Command Line Inc.
|
||
|
// SPDX-License-Identifier: Apache-2.0
|
||
|
|
||
|
package shellutil
|
||
|
|
||
|
import (
|
||
|
"os"
|
||
|
"os/exec"
|
||
|
"strings"
|
||
|
|
||
|
"github.com/wavetermdev/waveterm/waveshell/pkg/base"
|
||
|
)
|
||
|
|
||
|
const DefaultTermType = "xterm-256color"
|
||
|
const DefaultTermRows = 24
|
||
|
const DefaultTermCols = 80
|
||
|
|
||
|
func MShellEnvVars(termType string) map[string]string {
|
||
|
rtn := make(map[string]string)
|
||
|
if termType != "" {
|
||
|
rtn["TERM"] = termType
|
||
|
}
|
||
|
rtn["WAVESHELL"], _ = os.Executable()
|
||
|
rtn["WAVESHELL_VERSION"] = base.MShellVersion
|
||
|
return rtn
|
||
|
}
|
||
|
|
||
|
func UpdateCmdEnv(cmd *exec.Cmd, envVars map[string]string) {
|
||
|
if len(envVars) == 0 {
|
||
|
return
|
||
|
}
|
||
|
found := make(map[string]bool)
|
||
|
var newEnv []string
|
||
|
for _, envStr := range cmd.Env {
|
||
|
envKey := GetEnvStrKey(envStr)
|
||
|
newEnvVal, ok := envVars[envKey]
|
||
|
if ok {
|
||
|
if newEnvVal == "" {
|
||
|
continue
|
||
|
}
|
||
|
newEnv = append(newEnv, envKey+"="+newEnvVal)
|
||
|
found[envKey] = true
|
||
|
} else {
|
||
|
newEnv = append(newEnv, envStr)
|
||
|
}
|
||
|
}
|
||
|
for envKey, envVal := range envVars {
|
||
|
if found[envKey] {
|
||
|
continue
|
||
|
}
|
||
|
newEnv = append(newEnv, envKey+"="+envVal)
|
||
|
}
|
||
|
cmd.Env = newEnv
|
||
|
}
|
||
|
|
||
|
func GetEnvStrKey(envStr string) string {
|
||
|
eqIdx := strings.Index(envStr, "=")
|
||
|
if eqIdx == -1 {
|
||
|
return envStr
|
||
|
}
|
||
|
return envStr[0:eqIdx]
|
||
|
}
|