mirror of
https://github.com/wavetermdev/backup.git
synced 2026-08-05 13:57:07 -07:00
cr command (bare) shows all remotes
This commit is contained in:
@@ -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 {
|
||||
|
||||
+18
-9
@@ -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()
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user