add a debug flag to log the rc file for bash initialization

This commit is contained in:
sawka
2023-08-14 12:23:33 -07:00
parent 865ac0220f
commit 093e550d50
2 changed files with 30 additions and 1 deletions
+21
View File
@@ -30,6 +30,7 @@ const MShellPathVarName = "MSHELL_PATH"
const MShellHomeVarName = "MSHELL_HOME"
const MShellInstallBinVarName = "MSHELL_INSTALLBIN_PATH"
const SSHCommandVarName = "SSH_COMMAND"
const MShellDebugVarName = "MSHELL_DEBUG"
const SessionsDirBaseName = "sessions"
const MShellVersion = "v0.2.0"
const RemoteIdFile = "remoteid"
@@ -37,6 +38,9 @@ const DefaultMShellInstallBinDir = "/opt/mshell/bin"
const LogFileName = "mshell.log"
const ForceDebugLog = false
const DebugFlag_LogRcFile = "logrc"
const LogRcFileName = "debug.rcfile"
var sessionDirCache = make(map[string]string)
var baseLock = &sync.Mutex{}
var DebugLogEnabled = false
@@ -146,6 +150,23 @@ func (ckey CommandKey) Validate(typeStr string) error {
return nil
}
func HasDebugFlag(envMap map[string]string, flagName string) bool {
msDebug := envMap[MShellDebugVarName]
flags := strings.Split(msDebug, ",")
Logf("hasdebugflag[%s]: %s [%#v]\n", flagName, msDebug, flags)
for _, flag := range flags {
if strings.TrimSpace(flag) == flagName {
return true
}
}
return false
}
func GetDebugRcFileName() string {
msHome := GetMShellHomeDir()
return path.Join(msHome, LogRcFileName)
}
func GetHomeDir() string {
homeVar := os.Getenv(HomeVarName)
if homeVar == "" {
+9 -1
View File
@@ -24,11 +24,11 @@ import (
"time"
"github.com/alessio/shellescape"
"github.com/creack/pty"
"github.com/commandlinedev/apishell/pkg/base"
"github.com/commandlinedev/apishell/pkg/cirfile"
"github.com/commandlinedev/apishell/pkg/mpio"
"github.com/commandlinedev/apishell/pkg/packet"
"github.com/creack/pty"
"golang.org/x/mod/semver"
"golang.org/x/sys/unix"
)
@@ -1081,6 +1081,14 @@ func RunCommandSimple(pk *packet.RunPacketType, sender *packet.PacketSender, fro
trapCmdStr := makeExitTrap(cmd.ReturnState.FdNum)
rcFileStr += trapCmdStr
}
shellVarMap := ShellVarMapFromState(state)
if base.HasDebugFlag(shellVarMap, base.DebugFlag_LogRcFile) {
debugRcFileName := base.GetDebugRcFileName()
err := os.WriteFile(debugRcFileName, []byte(rcFileStr), 0600)
if err != nil {
base.Logf("error writing %s: %v\n", debugRcFileName, err)
}
}
rcFileFdNum, err := AddRunData(pk, rcFileStr, "rcfile")
if err != nil {
return nil, err