waveterm/pkg/service/objectservice/objectservice.go

66 lines
1.6 KiB
Go
Raw Normal View History

2024-05-27 08:05:11 +02:00
// Copyright 2024, Command Line Inc.
// SPDX-License-Identifier: Apache-2.0
package objectservice
import (
"context"
"fmt"
"strings"
"time"
"github.com/wavetermdev/thenextwave/pkg/waveobj"
"github.com/wavetermdev/thenextwave/pkg/wstore"
)
type ObjectService struct{}
const DefaultTimeout = 2 * time.Second
func parseORef(oref string) (*waveobj.ORef, error) {
fields := strings.Split(oref, ":")
if len(fields) != 2 {
return nil, fmt.Errorf("invalid object reference: %q", oref)
}
return &waveobj.ORef{OType: fields[0], OID: fields[1]}, nil
}
func (svc *ObjectService) GetClientObject() (any, error) {
ctx, cancelFn := context.WithTimeout(context.Background(), DefaultTimeout)
defer cancelFn()
client, err := wstore.DBGetSingleton[*wstore.Client](ctx)
if err != nil {
return nil, fmt.Errorf("error getting client: %w", err)
}
return waveobj.ToJsonMap(client)
}
2024-05-27 08:05:11 +02:00
func (svc *ObjectService) GetObject(orefStr string) (any, error) {
oref, err := parseORef(orefStr)
if err != nil {
return nil, err
}
ctx, cancelFn := context.WithTimeout(context.Background(), DefaultTimeout)
defer cancelFn()
obj, err := wstore.DBGetORef(ctx, *oref)
if err != nil {
return nil, fmt.Errorf("error getting object: %w", err)
}
return waveobj.ToJsonMap(obj)
2024-05-27 08:05:11 +02:00
}
func (svc *ObjectService) GetObjects(orefStrArr []string) (any, error) {
ctx, cancelFn := context.WithTimeout(context.Background(), DefaultTimeout)
defer cancelFn()
var orefArr []waveobj.ORef
for _, orefStr := range orefStrArr {
orefObj, err := parseORef(orefStr)
if err != nil {
return nil, err
}
orefArr = append(orefArr, *orefObj)
}
return wstore.DBSelectORefs(ctx, orefArr)
}