feat(module,app): basic module to list apps
This commit is contained in:
58
pkg/module/app/memory/module_test.go
Normal file
58
pkg/module/app/memory/module_test.go
Normal file
@ -0,0 +1,58 @@
|
||||
package memory
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"testing"
|
||||
|
||||
"forge.cadoles.com/arcad/edge/pkg/app"
|
||||
"forge.cadoles.com/arcad/edge/pkg/module"
|
||||
appModule "forge.cadoles.com/arcad/edge/pkg/module/app"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
func TestAppModuleWithMemoryRepository(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
server := app.NewServer(
|
||||
module.ContextModuleFactory(),
|
||||
module.ConsoleModuleFactory(),
|
||||
appModule.ModuleFactory(NewRepository(
|
||||
func(ctx context.Context, id app.ID) (string, error) {
|
||||
return fmt.Sprintf("http//%s.example.com", id), nil
|
||||
},
|
||||
&app.Manifest{
|
||||
ID: "dummy1.arcad.app",
|
||||
Version: "0.0.0",
|
||||
Title: "Dummy 1",
|
||||
Description: "Dummy App 1",
|
||||
Tags: []string{"dummy", "first"},
|
||||
},
|
||||
&app.Manifest{
|
||||
ID: "dummy2.arcad.app",
|
||||
Version: "0.0.0",
|
||||
Title: "Dummy 2",
|
||||
Description: "Dummy App 2",
|
||||
Tags: []string{"dummy", "second"},
|
||||
},
|
||||
)),
|
||||
)
|
||||
|
||||
file := "testdata/app.js"
|
||||
|
||||
data, err := ioutil.ReadFile(file)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if err := server.Load(file, string(data)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
defer server.Stop()
|
||||
|
||||
if err := server.Start(); err != nil {
|
||||
t.Fatalf("%+v", errors.WithStack(err))
|
||||
}
|
||||
}
|
50
pkg/module/app/memory/repository.go
Normal file
50
pkg/module/app/memory/repository.go
Normal file
@ -0,0 +1,50 @@
|
||||
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{}
|
17
pkg/module/app/memory/testdata/app.js
vendored
Normal file
17
pkg/module/app/memory/testdata/app.js
vendored
Normal file
@ -0,0 +1,17 @@
|
||||
var ctx = context.new();
|
||||
|
||||
var manifests = app.list(ctx);
|
||||
|
||||
if (manifests.length !== 2) {
|
||||
throw new Error("apps.length: expected '2', got '"+manifests.length+"'");
|
||||
}
|
||||
|
||||
var manifest = app.get(ctx, 'dummy2.arcad.app');
|
||||
|
||||
if (!manifest) {
|
||||
throw new Error("manifest should not be null");
|
||||
}
|
||||
|
||||
if (manifest.id !== "dummy2.arcad.app") {
|
||||
throw new Error("manifest.id: expected 'dummy2.arcad.app', got '"+manifest.id+"'");
|
||||
}
|
Reference in New Issue
Block a user