itd/api/resources.go
Arsen Musayelyan b757af7fed
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
Switch to autogenerated DRPC framework
2023-01-02 22:30:17 -08:00

52 lines
1.2 KiB
Go

package api
import (
"context"
"go.arsenm.dev/infinitime"
"go.arsenm.dev/itd/internal/rpc"
)
type ResourceOperation uint8
const (
ResourceOperationRemoveObsolete = infinitime.ResourceOperationRemoveObsolete
ResourceOperationUpload = infinitime.ResourceOperationUpload
)
type ResourceLoadProgress struct {
Operation ResourceOperation
Name string
Total int64
Sent int64
Err error
}
// LoadResources loads resources onto the watch from the given
// file path to the resources zip
func (c *FSClient) LoadResources(ctx context.Context, path string) (<-chan ResourceLoadProgress, error) {
progCh := make(chan ResourceLoadProgress, 2)
rc, err := c.client.LoadResources(ctx, &rpc.PathRequest{Path: path})
if err != nil {
return nil, err
}
go fsRecvToChannel[rpc.ResourceLoadProgress](rc, progCh, func(evt *rpc.ResourceLoadProgress, err error) ResourceLoadProgress {
return ResourceLoadProgress{
Operation: ResourceOperation(evt.Operation),
Name: evt.Name,
Sent: evt.Sent,
Total: evt.Total,
Err: err,
}
})
return progCh, nil
}
type StreamClient[T any] interface {
Recv() (*T, error)
Context() context.Context
}