diff --git a/pkg/cmdrunner/cmdrunner.go b/pkg/cmdrunner/cmdrunner.go index 6729db5b..f462326f 100644 --- a/pkg/cmdrunner/cmdrunner.go +++ b/pkg/cmdrunner/cmdrunner.go @@ -1012,6 +1012,55 @@ func RemoteCommand(ctx context.Context, pk *scpacket.FeCommandPacketType) (sstor return nil, fmt.Errorf("/remote requires a subcommand: %s", formatStrs([]string{"show"}, "or", false)) } +func crShowCommand(ctx context.Context, pk *scpacket.FeCommandPacketType, ids resolvedIds) (sstore.UpdatePacket, error) { + var buf bytes.Buffer + riArr, err := sstore.GetRIsForWindow(ctx, ids.SessionId, ids.WindowId) + if err != nil { + return nil, fmt.Errorf("cannot get remote instances: %w", err) + } + rmap := remote.GetRemoteMap() + for _, ri := range riArr { + rptr := sstore.RemotePtrType{RemoteId: ri.RemoteId, Name: ri.Name} + msh := rmap[ri.RemoteId] + if msh == nil { + continue + } + baseDisplayName := msh.GetDisplayName() + displayName := rptr.GetDisplayName(baseDisplayName) + cwdStr := "-" + if ri.FeState.Cwd != "" { + cwdStr = ri.FeState.Cwd + } + buf.WriteString(fmt.Sprintf("%-30s %-50s\n", displayName, cwdStr)) + } + riBaseMap := make(map[string]bool) + for _, ri := range riArr { + if ri.Name == "" { + riBaseMap[ri.RemoteId] = true + } + } + for remoteId, msh := range rmap { + if riBaseMap[remoteId] { + continue + } + feState := msh.GetDefaultFeState() + if feState == nil { + continue + } + cwdStr := "-" + if feState.Cwd != "" { + cwdStr = feState.Cwd + } + buf.WriteString(fmt.Sprintf("%-30s %-50s (default)\n", msh.GetDisplayName(), cwdStr)) + } + update := sstore.ModelUpdate{ + Info: &sstore.InfoMsgType{ + InfoLines: splitLinesForInfo(buf.String()), + }, + } + return update, nil +} + func CrCommand(ctx context.Context, pk *scpacket.FeCommandPacketType) (sstore.UpdatePacket, error) { ids, err := resolveUiIds(ctx, pk, R_Session|R_Window) if err != nil { @@ -1019,7 +1068,7 @@ func CrCommand(ctx context.Context, pk *scpacket.FeCommandPacketType) (sstore.Up } newRemote := firstArg(pk) if newRemote == "" { - return nil, nil + return crShowCommand(ctx, pk, ids) } remoteName, rptr, rstate, err := resolveRemote(ctx, newRemote, ids.SessionId, ids.WindowId) if err != nil { diff --git a/pkg/remote/remote.go b/pkg/remote/remote.go index 1267ede8..662f14a6 100644 --- a/pkg/remote/remote.go +++ b/pkg/remote/remote.go @@ -202,17 +202,11 @@ func (state RemoteRuntimeState) GetBaseDisplayName() string { } func (state RemoteRuntimeState) GetDisplayName(rptr *sstore.RemotePtrType) string { - name := state.GetBaseDisplayName() + baseDisplayName := state.GetBaseDisplayName() if rptr == nil { - return name + return baseDisplayName } - if rptr.Name != "" { - name = name + ":" + rptr.Name - } - if rptr.OwnerId != "" { - name = "@" + rptr.OwnerId + ":" + name - } - return name + return rptr.GetDisplayName(baseDisplayName) } func LoadRemotes(ctx context.Context) error { @@ -387,6 +381,16 @@ func GetRemoteById(remoteId string) *MShellProc { return GlobalStore.Map[remoteId] } +func GetRemoteMap() map[string]*MShellProc { + GlobalStore.Lock.Lock() + defer GlobalStore.Lock.Unlock() + rtn := make(map[string]*MShellProc) + for remoteId, msh := range GlobalStore.Map { + rtn[remoteId] = msh + } + return rtn +} + func GetLocalRemote() *MShellProc { GlobalStore.Lock.Lock() defer GlobalStore.Lock.Unlock() @@ -1887,3 +1891,8 @@ func (msh *MShellProc) TryAutoConnect() error { } return nil } + +func (msh *MShellProc) GetDisplayName() string { + rcopy := msh.GetRemoteCopy() + return rcopy.GetName() +} diff --git a/pkg/sstore/dbops.go b/pkg/sstore/dbops.go index 5e81c979..d73d35ce 100644 --- a/pkg/sstore/dbops.go +++ b/pkg/sstore/dbops.go @@ -1776,3 +1776,22 @@ func PurgeLineById(ctx context.Context, sessionId string, lineId string) error { }) return txErr } + +func GetRIsForWindow(ctx context.Context, sessionId string, windowId string) ([]*RemoteInstance, error) { + var rtn []*RemoteInstance + txErr := WithTx(ctx, func(tx *TxWrap) error { + query := `SELECT * FROM remote_instance WHERE sessionid = ? AND (windowid = '' OR windowid = ?)` + riMaps := tx.SelectMaps(query, sessionId, windowId) + for _, m := range riMaps { + ri := RIFromMap(m) + if ri != nil { + rtn = append(rtn, ri) + } + } + return nil + }) + if txErr != nil { + return nil, txErr + } + return rtn, nil +} diff --git a/pkg/sstore/sstore.go b/pkg/sstore/sstore.go index fa82889d..c6493a6c 100644 --- a/pkg/sstore/sstore.go +++ b/pkg/sstore/sstore.go @@ -210,6 +210,20 @@ func (r RemotePtrType) IsSessionScope() bool { return strings.HasPrefix(r.Name, "*") } +func (rptr *RemotePtrType) GetDisplayName(baseDisplayName string) string { + name := baseDisplayName + if rptr == nil { + return name + } + if rptr.Name != "" { + name = name + ":" + rptr.Name + } + if rptr.OwnerId != "" { + name = "@" + rptr.OwnerId + ":" + name + } + return name +} + func (r RemotePtrType) Validate() error { if r.OwnerId != "" { if _, err := uuid.Parse(r.OwnerId); err != nil {