waveterm/pkg/comp/simplecomp.go

101 lines
3.1 KiB
Go
Raw Normal View History

2022-11-10 05:38:28 +01:00
package comp
import (
"context"
"fmt"
2022-11-10 22:52:51 +01:00
"sync"
2022-11-10 05:38:28 +01:00
"github.com/google/uuid"
"github.com/scripthaus-dev/mshell/pkg/packet"
"github.com/scripthaus-dev/sh2-server/pkg/remote"
2022-11-10 22:52:51 +01:00
"github.com/scripthaus-dev/sh2-server/pkg/utilfn"
2022-11-10 05:38:28 +01:00
)
2022-11-10 22:52:51 +01:00
var globalLock = &sync.Mutex{}
var simpleCompMap = map[string]SimpleCompGenFnType{
2022-11-11 01:00:51 +01:00
CGTypeCommand: simpleCompCommand,
CGTypeFile: simpleCompFile,
CGTypeDir: simpleCompDir,
CGTypeVariable: simpleCompVar,
2022-11-10 22:52:51 +01:00
}
type SimpleCompGenFnType = func(ctx context.Context, prefix string, compCtx CompContext, args []interface{}) (*CompReturn, error)
func RegisterSimpleCompFn(compType string, fn SimpleCompGenFnType) {
globalLock.Lock()
defer globalLock.Unlock()
if _, ok := simpleCompMap[compType]; ok {
panic(fmt.Sprintf("simpleCompFn %q already registered", compType))
}
simpleCompMap[compType] = fn
}
func getSimpleCompFn(compType string) SimpleCompGenFnType {
globalLock.Lock()
defer globalLock.Unlock()
return simpleCompMap[compType]
}
func DoSimpleComp(ctx context.Context, compType string, prefix string, compCtx CompContext, args []interface{}) (*CompReturn, error) {
compFn := getSimpleCompFn(compType)
if compFn == nil {
return nil, fmt.Errorf("no simple comp fn for %q", compType)
}
2022-11-11 03:51:20 +01:00
crtn, err := compFn(ctx, prefix, compCtx, args)
if err != nil {
return nil, err
}
crtn.CompType = compType
return crtn, nil
2022-11-10 22:52:51 +01:00
}
2022-11-10 05:38:28 +01:00
func compsToCompReturn(comps []string, hasMore bool) *CompReturn {
var rtn CompReturn
rtn.HasMore = hasMore
for _, comp := range comps {
rtn.Entries = append(rtn.Entries, CompEntry{Word: comp})
}
return &rtn
}
2022-11-10 22:52:51 +01:00
func doCompGen(ctx context.Context, prefix string, compType string, compCtx CompContext) (*CompReturn, error) {
2022-11-10 05:38:28 +01:00
if !packet.IsValidCompGenType(compType) {
return nil, fmt.Errorf("/_compgen invalid type '%s'", compType)
2022-11-10 05:38:28 +01:00
}
msh := remote.GetRemoteById(compCtx.RemotePtr.RemoteId)
if msh == nil {
2022-11-10 22:52:51 +01:00
return nil, fmt.Errorf("invalid remote '%s', not found", compCtx.RemotePtr)
2022-11-10 05:38:28 +01:00
}
cgPacket := packet.MakeCompGenPacket()
cgPacket.ReqId = uuid.New().String()
cgPacket.CompType = compType
cgPacket.Prefix = prefix
cgPacket.Cwd = compCtx.State.Cwd
resp, err := msh.PacketRpc(ctx, cgPacket)
if err != nil {
2022-11-10 22:52:51 +01:00
return nil, err
2022-11-10 05:38:28 +01:00
}
if err = resp.Err(); err != nil {
2022-11-10 22:52:51 +01:00
return nil, err
2022-11-10 05:38:28 +01:00
}
2022-11-10 22:52:51 +01:00
comps := utilfn.GetStrArr(resp.Data, "comps")
hasMore := utilfn.GetBool(resp.Data, "hasmore")
return compsToCompReturn(comps, hasMore), nil
2022-11-10 05:38:28 +01:00
}
2022-11-10 22:52:51 +01:00
func simpleCompFile(ctx context.Context, prefix string, compCtx CompContext, args []interface{}) (*CompReturn, error) {
2022-11-11 01:00:51 +01:00
return doCompGen(ctx, prefix, CGTypeFile, compCtx)
2022-11-10 22:52:51 +01:00
}
func simpleCompDir(ctx context.Context, prefix string, compCtx CompContext, args []interface{}) (*CompReturn, error) {
2022-11-11 01:00:51 +01:00
return doCompGen(ctx, prefix, CGTypeDir, compCtx)
2022-11-10 22:52:51 +01:00
}
func simpleCompVar(ctx context.Context, prefix string, compCtx CompContext, args []interface{}) (*CompReturn, error) {
2022-11-11 01:00:51 +01:00
return doCompGen(ctx, prefix, CGTypeVariable, compCtx)
2022-11-10 22:52:51 +01:00
}
func simpleCompCommand(ctx context.Context, prefix string, compCtx CompContext, args []interface{}) (*CompReturn, error) {
2022-11-11 01:00:51 +01:00
return doCompGen(ctx, prefix, CGTypeCommand, compCtx)
2022-11-10 05:38:28 +01:00
}