105 lines
2.3 KiB
Go
105 lines
2.3 KiB
Go
|
package app
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
|
||
|
"forge.cadoles.com/arcad/edge/pkg/app"
|
||
|
"forge.cadoles.com/arcad/edge/pkg/bundle"
|
||
|
appModule "forge.cadoles.com/arcad/edge/pkg/module/app"
|
||
|
"github.com/pkg/errors"
|
||
|
"gitlab.com/wpetit/goweb/logger"
|
||
|
)
|
||
|
|
||
|
type GetURLFunc func(context.Context, *app.Manifest) (string, error)
|
||
|
|
||
|
type AppRepository struct {
|
||
|
getURL GetURLFunc
|
||
|
bundles []string
|
||
|
}
|
||
|
|
||
|
// Get implements app.Repository
|
||
|
func (r *AppRepository) Get(ctx context.Context, id app.ID) (*app.Manifest, error) {
|
||
|
manifest, err := r.findManifest(ctx, id)
|
||
|
if err != nil {
|
||
|
return nil, errors.WithStack(err)
|
||
|
}
|
||
|
|
||
|
return manifest, nil
|
||
|
}
|
||
|
|
||
|
// GetURL implements app.Repository
|
||
|
func (r *AppRepository) GetURL(ctx context.Context, id app.ID) (string, error) {
|
||
|
manifest, err := r.findManifest(ctx, id)
|
||
|
if err != nil {
|
||
|
return "", errors.WithStack(err)
|
||
|
}
|
||
|
|
||
|
url, err := r.getURL(ctx, manifest)
|
||
|
if err != nil {
|
||
|
return "", errors.WithStack(err)
|
||
|
}
|
||
|
|
||
|
return url, nil
|
||
|
}
|
||
|
|
||
|
// List implements app.Repository
|
||
|
func (r *AppRepository) List(ctx context.Context) ([]*app.Manifest, error) {
|
||
|
manifests := make([]*app.Manifest, 0)
|
||
|
|
||
|
for _, path := range r.bundles {
|
||
|
bundleCtx := logger.With(ctx, logger.F("path", path))
|
||
|
|
||
|
bundle, err := bundle.FromPath(path)
|
||
|
if err != nil {
|
||
|
logger.Error(bundleCtx, "could not load bundle", logger.E(errors.WithStack(err)))
|
||
|
|
||
|
continue
|
||
|
}
|
||
|
|
||
|
manifest, err := app.LoadManifest(bundle)
|
||
|
if err != nil {
|
||
|
logger.Error(bundleCtx, "could not load manifest", logger.E(errors.WithStack(err)))
|
||
|
|
||
|
continue
|
||
|
}
|
||
|
|
||
|
manifests = append(manifests, manifest)
|
||
|
}
|
||
|
|
||
|
return manifests, nil
|
||
|
}
|
||
|
|
||
|
func (r *AppRepository) findManifest(ctx context.Context, id app.ID) (*app.Manifest, error) {
|
||
|
for _, path := range r.bundles {
|
||
|
bundleCtx := logger.With(ctx, logger.F("path", path))
|
||
|
|
||
|
bundle, err := bundle.FromPath(path)
|
||
|
if err != nil {
|
||
|
logger.Error(bundleCtx, "could not load bundle", logger.E(errors.WithStack(err)))
|
||
|
|
||
|
continue
|
||
|
}
|
||
|
|
||
|
manifest, err := app.LoadManifest(bundle)
|
||
|
if err != nil {
|
||
|
logger.Error(bundleCtx, "could not load manifest", logger.E(errors.WithStack(err)))
|
||
|
|
||
|
continue
|
||
|
}
|
||
|
|
||
|
if manifest.ID != id {
|
||
|
continue
|
||
|
}
|
||
|
|
||
|
return manifest, nil
|
||
|
}
|
||
|
|
||
|
return nil, errors.WithStack(appModule.ErrNotFound)
|
||
|
}
|
||
|
|
||
|
func NewAppRepository(getURL GetURLFunc, bundles ...string) *AppRepository {
|
||
|
return &AppRepository{getURL, bundles}
|
||
|
}
|
||
|
|
||
|
var _ appModule.Repository = &AppRepository{}
|