51 lines
1.0 KiB
Go
51 lines
1.0 KiB
Go
|
package memory
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
|
||
|
"forge.cadoles.com/arcad/edge/pkg/app"
|
||
|
module "forge.cadoles.com/arcad/edge/pkg/module/app"
|
||
|
"github.com/pkg/errors"
|
||
|
)
|
||
|
|
||
|
type GetURLFunc func(context.Context, app.ID) (string, error)
|
||
|
|
||
|
type Repository struct {
|
||
|
getURL GetURLFunc
|
||
|
apps []*app.Manifest
|
||
|
}
|
||
|
|
||
|
// GetURL implements app.Repository
|
||
|
func (r *Repository) GetURL(ctx context.Context, id app.ID) (string, error) {
|
||
|
url, err := r.getURL(ctx, id)
|
||
|
if err != nil {
|
||
|
return "", errors.WithStack(err)
|
||
|
}
|
||
|
|
||
|
return url, nil
|
||
|
}
|
||
|
|
||
|
// Get implements app.Repository
|
||
|
func (r *Repository) Get(ctx context.Context, id app.ID) (*app.Manifest, error) {
|
||
|
for _, app := range r.apps {
|
||
|
if app.ID != id {
|
||
|
continue
|
||
|
}
|
||
|
|
||
|
return app, nil
|
||
|
}
|
||
|
|
||
|
return nil, module.ErrNotFound
|
||
|
}
|
||
|
|
||
|
// List implements app.Repository
|
||
|
func (r *Repository) List(ctx context.Context) ([]*app.Manifest, error) {
|
||
|
return r.apps, nil
|
||
|
}
|
||
|
|
||
|
func NewRepository(getURL GetURLFunc, manifests ...*app.Manifest) *Repository {
|
||
|
return &Repository{getURL, manifests}
|
||
|
}
|
||
|
|
||
|
var _ module.Repository = &Repository{}
|