updates to FeState to store variables -- implement python venv and gitbranch in festate for prompt

This commit is contained in:
sawka 2023-04-11 23:54:18 -07:00
parent b92c160333
commit 4e93eff86a
6 changed files with 48 additions and 36 deletions

View File

@ -1197,8 +1197,8 @@ func crShowCommand(ctx context.Context, pk *scpacket.FeCommandPacketType, ids re
baseDisplayName := msh.GetDisplayName() baseDisplayName := msh.GetDisplayName()
displayName := rptr.GetDisplayName(baseDisplayName) displayName := rptr.GetDisplayName(baseDisplayName)
cwdStr := "-" cwdStr := "-"
if ri.FeState.Cwd != "" { if ri.FeState["cwd"] != "" {
cwdStr = ri.FeState.Cwd cwdStr = ri.FeState["cwd"]
} }
buf.WriteString(fmt.Sprintf("%-30s %-50s\n", displayName, cwdStr)) buf.WriteString(fmt.Sprintf("%-30s %-50s\n", displayName, cwdStr))
} }
@ -1217,8 +1217,8 @@ func crShowCommand(ctx context.Context, pk *scpacket.FeCommandPacketType, ids re
continue continue
} }
cwdStr := "-" cwdStr := "-"
if feState.Cwd != "" { if feState["cwd"] != "" {
cwdStr = feState.Cwd cwdStr = feState["cwd"]
} }
buf.WriteString(fmt.Sprintf("%-30s %-50s (default)\n", msh.GetDisplayName(), cwdStr)) buf.WriteString(fmt.Sprintf("%-30s %-50s (default)\n", msh.GetDisplayName(), cwdStr))
} }
@ -1303,7 +1303,7 @@ func makeStaticCmd(ctx context.Context, metaCmd string, ids resolvedIds, cmdStr
cmd.StatePtr = *ids.Remote.StatePtr cmd.StatePtr = *ids.Remote.StatePtr
} }
if ids.Remote.FeState != nil { if ids.Remote.FeState != nil {
cmd.FeState = *ids.Remote.FeState cmd.FeState = ids.Remote.FeState
} }
err := sstore.CreateCmdPtyFile(ctx, cmd.ScreenId, cmd.CmdId, cmd.TermOpts.MaxPtySize) err := sstore.CreateCmdPtyFile(ctx, cmd.ScreenId, cmd.CmdId, cmd.TermOpts.MaxPtySize)
if err != nil { if err != nil {
@ -1455,7 +1455,7 @@ func doCompGen(ctx context.Context, pk *scpacket.FeCommandPacketType, prefix str
cgPacket.ReqId = uuid.New().String() cgPacket.ReqId = uuid.New().String()
cgPacket.CompType = compType cgPacket.CompType = compType
cgPacket.Prefix = prefix cgPacket.Prefix = prefix
cgPacket.Cwd = ids.Remote.FeState.Cwd cgPacket.Cwd = ids.Remote.FeState["cwd"]
resp, err := ids.Remote.MShell.PacketRpc(ctx, cgPacket) resp, err := ids.Remote.MShell.PacketRpc(ctx, cgPacket)
if err != nil { if err != nil {
return nil, false, err return nil, false, err
@ -1495,7 +1495,7 @@ func CompGenCommand(ctx context.Context, pk *scpacket.FeCommandPacketType) (ssto
rptr := ids.Remote.RemotePtr rptr := ids.Remote.RemotePtr
compCtx.RemotePtr = &rptr compCtx.RemotePtr = &rptr
if ids.Remote.FeState != nil { if ids.Remote.FeState != nil {
compCtx.Cwd = ids.Remote.FeState.Cwd compCtx.Cwd = ids.Remote.FeState["cwd"]
} }
} }
compCtx.ForDisplay = showComps compCtx.ForDisplay = showComps
@ -1949,7 +1949,7 @@ func RemoteResetCommand(ctx context.Context, pk *scpacket.FeCommandPacketType) (
return nil, fmt.Errorf("invalid initpk received from remote (no remote state)") return nil, fmt.Errorf("invalid initpk received from remote (no remote state)")
} }
feState := sstore.FeStateFromShellState(initPk.State) feState := sstore.FeStateFromShellState(initPk.State)
remoteInst, err := sstore.UpdateRemoteState(ctx, ids.SessionId, ids.ScreenId, ids.Remote.RemotePtr, *feState, initPk.State, nil) remoteInst, err := sstore.UpdateRemoteState(ctx, ids.SessionId, ids.ScreenId, ids.Remote.RemotePtr, feState, initPk.State, nil)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -2657,8 +2657,8 @@ func LineShowCommand(ctx context.Context, pk *scpacket.FeCommandPacketType) (sst
buf.WriteString(fmt.Sprintf(" %-15s %s\n", "cmdid", cmd.CmdId)) buf.WriteString(fmt.Sprintf(" %-15s %s\n", "cmdid", cmd.CmdId))
buf.WriteString(fmt.Sprintf(" %-15s %s\n", "remote", cmd.Remote.MakeFullRemoteRef())) buf.WriteString(fmt.Sprintf(" %-15s %s\n", "remote", cmd.Remote.MakeFullRemoteRef()))
buf.WriteString(fmt.Sprintf(" %-15s %s\n", "status", cmd.Status)) buf.WriteString(fmt.Sprintf(" %-15s %s\n", "status", cmd.Status))
if cmd.FeState.Cwd != "" { if cmd.FeState["cwd"] != "" {
buf.WriteString(fmt.Sprintf(" %-15s %s\n", "cwd", cmd.FeState.Cwd)) buf.WriteString(fmt.Sprintf(" %-15s %s\n", "cwd", cmd.FeState["cwd"]))
} }
buf.WriteString(fmt.Sprintf(" %-15s %s\n", "termopts", formatTermOpts(cmd.TermOpts))) buf.WriteString(fmt.Sprintf(" %-15s %s\n", "termopts", formatTermOpts(cmd.TermOpts)))
if cmd.TermOpts != cmd.OrigTermOpts { if cmd.TermOpts != cmd.OrigTermOpts {

View File

@ -34,7 +34,7 @@ type ResolvedRemote struct {
RState remote.RemoteRuntimeState RState remote.RemoteRuntimeState
RemoteCopy *sstore.RemoteType RemoteCopy *sstore.RemoteType
StatePtr *sstore.ShellStatePtr StatePtr *sstore.ShellStatePtr
FeState *sstore.FeStateType FeState map[string]string
} }
type ResolveItem = sstore.ResolveItem type ResolveItem = sstore.ResolveItem
@ -481,7 +481,7 @@ func resolveRemoteFromPtr(ctx context.Context, rptr *sstore.RemotePtrType, sessi
rtn.FeState = msh.GetDefaultFeState() rtn.FeState = msh.GetDefaultFeState()
} else { } else {
rtn.StatePtr = &sstore.ShellStatePtr{BaseHash: ri.StateBaseHash, DiffHashArr: ri.StateDiffHashArr} rtn.StatePtr = &sstore.ShellStatePtr{BaseHash: ri.StateBaseHash, DiffHashArr: ri.StateDiffHashArr}
rtn.FeState = &ri.FeState rtn.FeState = ri.FeState
} }
} }
} }

View File

@ -23,6 +23,7 @@ type BareMetaCmdDecl struct {
var BareMetaCmds = []BareMetaCmdDecl{ var BareMetaCmds = []BareMetaCmdDecl{
BareMetaCmdDecl{"cr", "cr"}, BareMetaCmdDecl{"cr", "cr"},
BareMetaCmdDecl{"connect", "cr"},
BareMetaCmdDecl{"clear", "clear"}, BareMetaCmdDecl{"clear", "clear"},
BareMetaCmdDecl{"reset", "reset"}, BareMetaCmdDecl{"reset", "reset"},
} }
@ -155,6 +156,8 @@ func setBracketArgs(argMap map[string]string, bracketStr string) error {
return nil return nil
} }
var literalRtnStateCommands = []string{".", "source", "unset", "cd", "alias", "unalias", "deactivate"}
// detects: export, declare, ., source, X=1, unset // detects: export, declare, ., source, X=1, unset
func IsReturnStateCommand(cmdStr string) bool { func IsReturnStateCommand(cmdStr string) bool {
cmdReader := strings.NewReader(cmdStr) cmdReader := strings.NewReader(cmdStr)
@ -171,7 +174,7 @@ func IsReturnStateCommand(cmdStr string) bool {
if len(callExpr.Args) > 0 && len(callExpr.Args[0].Parts) > 0 { if len(callExpr.Args) > 0 && len(callExpr.Args[0].Parts) > 0 {
lit, ok := callExpr.Args[0].Parts[0].(*syntax.Lit) lit, ok := callExpr.Args[0].Parts[0].(*syntax.Lit)
if ok { if ok {
if lit.Value == "." || lit.Value == "source" || lit.Value == "unset" || lit.Value == "cd" || lit.Value == "alias" || lit.Value == "unalias" { if utilfn.ContainsStr(literalRtnStateCommands, lit.Value) {
return true return true
} }
} }

View File

@ -132,7 +132,7 @@ type RemoteRuntimeState struct {
RemoteAlias string `json:"remotealias,omitempty"` RemoteAlias string `json:"remotealias,omitempty"`
RemoteCanonicalName string `json:"remotecanonicalname"` RemoteCanonicalName string `json:"remotecanonicalname"`
RemoteVars map[string]string `json:"remotevars"` RemoteVars map[string]string `json:"remotevars"`
DefaultFeState *sstore.FeStateType `json:"defaultfestate"` DefaultFeState map[string]string `json:"defaultfestate"`
Status string `json:"status"` Status string `json:"status"`
ConnectTimeout int `json:"connecttimeout,omitempty"` ConnectTimeout int `json:"connecttimeout,omitempty"`
ErrorStr string `json:"errorstr,omitempty"` ErrorStr string `json:"errorstr,omitempty"`
@ -177,7 +177,7 @@ func (msh *MShellProc) GetDefaultStatePtr() *sstore.ShellStatePtr {
return &sstore.ShellStatePtr{BaseHash: msh.CurrentState} return &sstore.ShellStatePtr{BaseHash: msh.CurrentState}
} }
func (msh *MShellProc) GetDefaultFeState() *sstore.FeStateType { func (msh *MShellProc) GetDefaultFeState() map[string]string {
state := msh.GetDefaultState() state := msh.GetDefaultState()
return sstore.FeStateFromShellState(state) return sstore.FeStateFromShellState(state)
} }
@ -1484,7 +1484,7 @@ func RunCommand(ctx context.Context, sessionId string, screenId string, remotePt
CmdStr: runPacket.Command, CmdStr: runPacket.Command,
RawCmdStr: runPacket.Command, RawCmdStr: runPacket.Command,
Remote: remotePtr, Remote: remotePtr,
FeState: *sstore.FeStateFromShellState(currentState), FeState: sstore.FeStateFromShellState(currentState),
StatePtr: *statePtr, StatePtr: *statePtr,
TermOpts: makeTermOpts(runPacket), TermOpts: makeTermOpts(runPacket),
Status: status, Status: status,
@ -1652,7 +1652,7 @@ func (msh *MShellProc) handleCmdDonePacket(donePk *packet.CmdDonePacketType) {
var statePtr *sstore.ShellStatePtr var statePtr *sstore.ShellStatePtr
if donePk.FinalState != nil && rct != nil { if donePk.FinalState != nil && rct != nil {
feState := sstore.FeStateFromShellState(donePk.FinalState) feState := sstore.FeStateFromShellState(donePk.FinalState)
remoteInst, err := sstore.UpdateRemoteState(context.Background(), rct.SessionId, rct.ScreenId, rct.RemotePtr, *feState, donePk.FinalState, nil) remoteInst, err := sstore.UpdateRemoteState(context.Background(), rct.SessionId, rct.ScreenId, rct.RemotePtr, feState, donePk.FinalState, nil)
if err != nil { if err != nil {
msh.WriteToPtyBuffer("*error trying to update remotestate: %v\n", err) msh.WriteToPtyBuffer("*error trying to update remotestate: %v\n", err)
// fall-through (nothing to do) // fall-through (nothing to do)
@ -1667,7 +1667,7 @@ func (msh *MShellProc) handleCmdDonePacket(donePk *packet.CmdDonePacketType) {
msh.WriteToPtyBuffer("*error trying to update remotestate: %v\n", err) msh.WriteToPtyBuffer("*error trying to update remotestate: %v\n", err)
// fall-through (nothing to do) // fall-through (nothing to do)
} else { } else {
remoteInst, err := sstore.UpdateRemoteState(context.Background(), rct.SessionId, rct.ScreenId, rct.RemotePtr, *feState, nil, donePk.FinalStateDiff) remoteInst, err := sstore.UpdateRemoteState(context.Background(), rct.SessionId, rct.ScreenId, rct.RemotePtr, feState, nil, donePk.FinalStateDiff)
if err != nil { if err != nil {
msh.WriteToPtyBuffer("*error trying to update remotestate: %v\n", err) msh.WriteToPtyBuffer("*error trying to update remotestate: %v\n", err)
// fall-through (nothing to do) // fall-through (nothing to do)
@ -1995,7 +1995,7 @@ func (msh *MShellProc) getFullState(stateDiff *packet.ShellStateDiff) (*packet.S
} }
// internal func, first tries the StateMap, otherwise will fallback on sstore.GetFullState // internal func, first tries the StateMap, otherwise will fallback on sstore.GetFullState
func (msh *MShellProc) getFeStateFromDiff(stateDiff *packet.ShellStateDiff) (*sstore.FeStateType, error) { func (msh *MShellProc) getFeStateFromDiff(stateDiff *packet.ShellStateDiff) (map[string]string, error) {
baseState := msh.GetStateByHash(stateDiff.BaseHash) baseState := msh.GetStateByHash(stateDiff.BaseHash)
if baseState != nil && len(stateDiff.DiffHashArr) == 0 { if baseState != nil && len(stateDiff.DiffHashArr) == 0 {
newState, err := shexec.ApplyShellStateDiff(*baseState, *stateDiff) newState, err := shexec.ApplyShellStateDiff(*baseState, *stateDiff)

View File

@ -31,6 +31,8 @@ type SingleConnDBGetter struct {
SingleConnLock *sync.Mutex SingleConnLock *sync.Mutex
} }
type FeStateType map[string]string
type TxWrap = txwrap.TxWrap type TxWrap = txwrap.TxWrap
var dbWrap *SingleConnDBGetter var dbWrap *SingleConnDBGetter

View File

@ -22,6 +22,7 @@ import (
"github.com/sawka/txwrap" "github.com/sawka/txwrap"
"github.com/scripthaus-dev/mshell/pkg/base" "github.com/scripthaus-dev/mshell/pkg/base"
"github.com/scripthaus-dev/mshell/pkg/packet" "github.com/scripthaus-dev/mshell/pkg/packet"
"github.com/scripthaus-dev/mshell/pkg/shexec"
"github.com/scripthaus-dev/sh2-server/pkg/dbutil" "github.com/scripthaus-dev/sh2-server/pkg/dbutil"
"github.com/scripthaus-dev/sh2-server/pkg/scbase" "github.com/scripthaus-dev/sh2-server/pkg/scbase"
@ -563,15 +564,15 @@ func (ssptr *ShellStatePtr) IsEmpty() bool {
} }
type RemoteInstance struct { type RemoteInstance struct {
RIId string `json:"riid"` RIId string `json:"riid"`
Name string `json:"name"` Name string `json:"name"`
SessionId string `json:"sessionid"` SessionId string `json:"sessionid"`
ScreenId string `json:"screenid"` ScreenId string `json:"screenid"`
RemoteOwnerId string `json:"remoteownerid"` RemoteOwnerId string `json:"remoteownerid"`
RemoteId string `json:"remoteid"` RemoteId string `json:"remoteid"`
FeState FeStateType `json:"festate"` FeState map[string]string `json:"festate"`
StateBaseHash string `json:"-"` StateBaseHash string `json:"-"`
StateDiffHashArr []string `json:"-"` StateDiffHashArr []string `json:"-"`
// only for updates // only for updates
Remove bool `json:"remove,omitempty"` Remove bool `json:"remove,omitempty"`
@ -611,16 +612,22 @@ func (sd *StateDiff) ToMap() map[string]interface{} {
return rtn return rtn
} }
type FeStateType struct { func FeStateFromShellState(state *packet.ShellState) map[string]string {
Cwd string `json:"cwd"`
// maybe later we can add some vars
}
func FeStateFromShellState(state *packet.ShellState) *FeStateType {
if state == nil { if state == nil {
return nil return nil
} }
return &FeStateType{Cwd: state.Cwd} rtn := make(map[string]string)
rtn["cwd"] = state.Cwd
envMap := shexec.EnvMapFromState(state)
if envMap["VIRTUAL_ENV"] != "" {
rtn["VIRTUAL_ENV"] = envMap["VIRTUAL_ENV"]
}
for key, val := range envMap {
if strings.HasPrefix(key, "PROMPTVAR_") {
rtn[key] = val
}
}
return rtn
} }
func (ri *RemoteInstance) FromMap(m map[string]interface{}) bool { func (ri *RemoteInstance) FromMap(m map[string]interface{}) bool {
@ -874,7 +881,7 @@ type CmdType struct {
Remote RemotePtrType `json:"remote"` Remote RemotePtrType `json:"remote"`
CmdStr string `json:"cmdstr"` CmdStr string `json:"cmdstr"`
RawCmdStr string `json:"rawcmdstr"` RawCmdStr string `json:"rawcmdstr"`
FeState FeStateType `json:"festate"` FeState map[string]string `json:"festate"`
StatePtr ShellStatePtr `json:"state"` StatePtr ShellStatePtr `json:"state"`
TermOpts TermOpts `json:"termopts"` TermOpts TermOpts `json:"termopts"`
OrigTermOpts TermOpts `json:"origtermopts"` OrigTermOpts TermOpts `json:"origtermopts"`