Skip to content

Commit

Permalink
Refactoring: Rename Repository to Adapter and AdapterPort
Browse files Browse the repository at this point in the history
  • Loading branch information
quentinguidee committed Sep 10, 2023
1 parent 66f7da5 commit e5c67c0
Show file tree
Hide file tree
Showing 36 changed files with 458 additions and 459 deletions.
30 changes: 30 additions & 0 deletions adapter/event_in_memory.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package adapter

import (
"github.com/google/uuid"
"github.com/vertex-center/vertex/types"
)

type EventInMemoryAdapter struct {
listeners *map[uuid.UUID]types.Listener
}

func NewEventInMemoryAdapter() types.EventAdapterPort {
return &EventInMemoryAdapter{
listeners: &map[uuid.UUID]types.Listener{},
}
}

func (a *EventInMemoryAdapter) AddListener(l types.Listener) {
(*a.listeners)[l.GetUUID()] = l
}

func (a *EventInMemoryAdapter) RemoveListener(l types.Listener) {
delete(*a.listeners, l.GetUUID())
}

func (a *EventInMemoryAdapter) Send(e interface{}) {
for _, l := range *a.listeners {
l.OnEvent(e)
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package repository
package adapter

import (
"testing"
Expand All @@ -9,37 +9,37 @@ import (
"github.com/stretchr/testify/suite"
)

type EventInMemoryRepositoryTestSuite struct {
type EventInMemoryAdapterTestSuite struct {
suite.Suite

repo EventInMemoryRepository
adapter EventInMemoryAdapter
}

func TestEventInMemoryRepositoryTestSuite(t *testing.T) {
suite.Run(t, new(EventInMemoryRepositoryTestSuite))
suite.Run(t, new(EventInMemoryAdapterTestSuite))
}

func (suite *EventInMemoryRepositoryTestSuite) SetupSuite() {
suite.repo = NewEventInMemoryRepository()
func (suite *EventInMemoryAdapterTestSuite) SetupSuite() {
suite.adapter = *NewEventInMemoryAdapter().(*EventInMemoryAdapter)
}

func (suite *EventInMemoryRepositoryTestSuite) TestEvents() {
func (suite *EventInMemoryAdapterTestSuite) TestEvents() {
listener := MockListener{
uuid: uuid.New(),
}

// Add a listener
suite.repo.AddListener(&listener)
assert.Equal(suite.T(), 1, len(*suite.repo.listeners))
suite.adapter.AddListener(&listener)
assert.Equal(suite.T(), 1, len(*suite.adapter.listeners))

// Fire event
listener.On("OnEvent").Return(nil)
suite.repo.Send(MockEvent{})
suite.adapter.Send(MockEvent{})
listener.AssertCalled(suite.T(), "OnEvent")

// Remove listener
suite.repo.RemoveListener(&listener)
assert.Equal(suite.T(), 0, len(*suite.repo.listeners))
suite.adapter.RemoveListener(&listener)
assert.Equal(suite.T(), 0, len(*suite.adapter.listeners))
}

type MockEvent struct{}
Expand Down
64 changes: 32 additions & 32 deletions repository/instance_fs.go → adapter/instance_fs.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package repository
package adapter

import (
"bufio"
Expand All @@ -23,72 +23,72 @@ var (
ErrContainerNotFound = errors.New("container not found")
)

type InstanceFSRepository struct {
type InstanceFSAdapter struct {
instancesPath string
instances map[uuid.UUID]*types.Instance
}

func NewInstanceFSRepository() InstanceFSRepository {
r := InstanceFSRepository{
func NewInstanceFSAdapter() types.InstanceAdapterPort {
adapter := &InstanceFSAdapter{
instancesPath: path.Join(storage.Path, "instances"),
instances: map[uuid.UUID]*types.Instance{},
}

err := os.MkdirAll(r.instancesPath, os.ModePerm)
err := os.MkdirAll(adapter.instancesPath, os.ModePerm)
if err != nil && !os.IsExist(err) {
log.Default.Error(err,
vlog.String("message", "failed to create directory"),
vlog.String("path", r.instancesPath),
vlog.String("path", adapter.instancesPath),
)
os.Exit(1)
}

return r
return adapter
}

func (r *InstanceFSRepository) Get(uuid uuid.UUID) (*types.Instance, error) {
instance, ok := r.instances[uuid]
func (a *InstanceFSAdapter) Get(uuid uuid.UUID) (*types.Instance, error) {
instance, ok := a.instances[uuid]
if !ok {
return nil, ErrInstanceNotFound
}
return instance, nil
}

func (r *InstanceFSRepository) GetAll() map[uuid.UUID]*types.Instance {
return r.instances
func (a *InstanceFSAdapter) GetAll() map[uuid.UUID]*types.Instance {
return a.instances
}

func (r *InstanceFSRepository) GetPath(uuid uuid.UUID) string {
return path.Join(r.instancesPath, uuid.String())
func (a *InstanceFSAdapter) GetPath(uuid uuid.UUID) string {
return path.Join(a.instancesPath, uuid.String())
}

func (r *InstanceFSRepository) Delete(uuid uuid.UUID) error {
err := os.RemoveAll(r.GetPath(uuid))
func (a *InstanceFSAdapter) Delete(uuid uuid.UUID) error {
err := os.RemoveAll(a.GetPath(uuid))
if err != nil {
return fmt.Errorf("failed to delete server: %v", err)
}

delete(r.instances, uuid)
delete(a.instances, uuid)

return nil
}

func (r *InstanceFSRepository) Exists(uuid uuid.UUID) bool {
return r.instances[uuid] != nil
func (a *InstanceFSAdapter) Exists(uuid uuid.UUID) bool {
return a.instances[uuid] != nil
}

func (r *InstanceFSRepository) Set(uuid uuid.UUID, instance types.Instance) error {
if r.Exists(uuid) {
func (a *InstanceFSAdapter) Set(uuid uuid.UUID, instance types.Instance) error {
if a.Exists(uuid) {
return ErrInstanceAlreadyExists
}

r.instances[uuid] = &instance
a.instances[uuid] = &instance

return nil
}

func (r *InstanceFSRepository) SaveSettings(i *types.Instance) error {
settingsPath := path.Join(r.GetPath(i.UUID), ".vertex", "instance_settings.json")
func (a *InstanceFSAdapter) SaveSettings(i *types.Instance) error {
settingsPath := path.Join(a.GetPath(i.UUID), ".vertex", "instance_settings.json")

settingsBytes, err := json.MarshalIndent(i.InstanceSettings, "", "\t")
if err != nil {
Expand All @@ -103,8 +103,8 @@ func (r *InstanceFSRepository) SaveSettings(i *types.Instance) error {
return nil
}

func (r *InstanceFSRepository) LoadSettings(i *types.Instance) error {
settingsPath := path.Join(r.GetPath(i.UUID), ".vertex", "instance_settings.json")
func (a *InstanceFSAdapter) LoadSettings(i *types.Instance) error {
settingsPath := path.Join(a.GetPath(i.UUID), ".vertex", "instance_settings.json")
settingsBytes, err := os.ReadFile(settingsPath)

if errors.Is(err, os.ErrNotExist) {
Expand All @@ -121,7 +121,7 @@ func (r *InstanceFSRepository) LoadSettings(i *types.Instance) error {
return nil
}

func (r *InstanceFSRepository) ReadService(instancePath string) (types.Service, error) {
func (a *InstanceFSAdapter) ReadService(instancePath string) (types.Service, error) {
data, err := os.ReadFile(path.Join(instancePath, ".vertex", "service.yml"))
if err != nil {
log.Default.Warn("'.vertex/service.yml' file not found",
Expand All @@ -134,8 +134,8 @@ func (r *InstanceFSRepository) ReadService(instancePath string) (types.Service,
return service, err
}

func (r *InstanceFSRepository) SaveEnv(i *types.Instance, variables map[string]string) error {
filepath := path.Join(r.GetPath(i.UUID), ".env")
func (a *InstanceFSAdapter) SaveEnv(i *types.Instance, variables map[string]string) error {
filepath := path.Join(a.GetPath(i.UUID), ".env")

file, err := os.OpenFile(filepath, os.O_WRONLY|os.O_CREATE, os.ModePerm)
if err != nil {
Expand All @@ -154,8 +154,8 @@ func (r *InstanceFSRepository) SaveEnv(i *types.Instance, variables map[string]s
return nil
}

func (r *InstanceFSRepository) LoadEnv(i *types.Instance) error {
filepath := path.Join(r.GetPath(i.UUID), ".env")
func (a *InstanceFSAdapter) LoadEnv(i *types.Instance) error {
filepath := path.Join(a.GetPath(i.UUID), ".env")

file, err := os.Open(filepath)
if os.IsNotExist(err) {
Expand All @@ -179,8 +179,8 @@ func (r *InstanceFSRepository) LoadEnv(i *types.Instance) error {
return nil
}

func (r *InstanceFSRepository) Reload(load func(uuid uuid.UUID)) {
entries, err := os.ReadDir(r.instancesPath)
func (a *InstanceFSAdapter) Reload(load func(uuid uuid.UUID)) {
entries, err := os.ReadDir(a.instancesPath)
if err != nil {
log.Default.Error(err)
os.Exit(1)
Expand Down
42 changes: 21 additions & 21 deletions repository/instance_logs_fs.go → adapter/instance_logs_fs.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package repository
package adapter

import (
"errors"
Expand Down Expand Up @@ -28,19 +28,19 @@ type InstanceLogger struct {
currentLine int
}

type InstanceLogsFSRepository struct {
type InstanceLogsFSAdapter struct {
loggers map[uuid.UUID]*InstanceLogger
}

func NewInstanceLogsFSRepository() InstanceLogsFSRepository {
r := InstanceLogsFSRepository{
func NewInstanceLogsFSAdapter() types.InstanceLogsAdapterPort {
r := &InstanceLogsFSAdapter{
loggers: map[uuid.UUID]*InstanceLogger{},
}
r.startCron()
return r
}

func (r *InstanceLogsFSRepository) Open(uuid uuid.UUID) error {
func (a *InstanceLogsFSAdapter) Open(uuid uuid.UUID) error {
dir := path.Join(storage.Path, "instances", uuid.String(), ".vertex", "logs")
err := os.MkdirAll(dir, os.ModePerm)
if err != nil {
Expand All @@ -60,20 +60,20 @@ func (r *InstanceLogsFSRepository) Open(uuid uuid.UUID) error {
}
l.file = file

r.loggers[uuid] = &l
a.loggers[uuid] = &l
return nil
}

func (r *InstanceLogsFSRepository) Close(uuid uuid.UUID) error {
l, err := r.getLogger(uuid)
func (a *InstanceLogsFSAdapter) Close(uuid uuid.UUID) error {
l, err := a.getLogger(uuid)
if err != nil {
return err
}
return l.Close()
}

func (r *InstanceLogsFSRepository) Push(uuid uuid.UUID, line types.LogLine) {
l, err := r.getLogger(uuid)
func (a *InstanceLogsFSAdapter) Push(uuid uuid.UUID, line types.LogLine) {
l, err := a.getLogger(uuid)
if err != nil {
log.Default.Error(err)
return
Expand All @@ -90,19 +90,19 @@ func (r *InstanceLogsFSRepository) Push(uuid uuid.UUID, line types.LogLine) {
}
}

func (r *InstanceLogsFSRepository) CloseAll() error {
func (a *InstanceLogsFSAdapter) CloseAll() error {
var errs []error
for id := range r.loggers {
err := r.Close(id)
for id := range a.loggers {
err := a.Close(id)
if err != nil {
errs = append(errs, err)
}
}
return errors.Join(errs...)
}

func (r *InstanceLogsFSRepository) LoadBuffer(uuid uuid.UUID) ([]types.LogLine, error) {
l, err := r.getLogger(uuid)
func (a *InstanceLogsFSAdapter) LoadBuffer(uuid uuid.UUID) ([]types.LogLine, error) {
l, err := a.getLogger(uuid)
if err != nil {
return nil, err
}
Expand All @@ -113,24 +113,24 @@ func (l *InstanceLogger) Close() error {
return l.file.Close()
}

func (r *InstanceLogsFSRepository) getLogger(uuid uuid.UUID) (*InstanceLogger, error) {
l, ok := r.loggers[uuid]
func (a *InstanceLogsFSAdapter) getLogger(uuid uuid.UUID) (*InstanceLogger, error) {
l, ok := a.loggers[uuid]
if !ok {
return nil, ErrLoggerNotFound
}
return l, nil
}

func (r *InstanceLogsFSRepository) startCron() {
func (a *InstanceLogsFSAdapter) startCron() {
s := gocron.NewScheduler(time.Local)
_, err := s.Every(1).Day().At("00:00").Do(func() {
for id := range r.loggers {
err := r.Close(id)
for id := range a.loggers {
err := a.Close(id)
if err != nil {
log.Default.Error(err)
continue
}
err = r.Open(id)
err = a.Open(id)
if err != nil {
log.Default.Error(err)
}
Expand Down
Loading

0 comments on commit e5c67c0

Please sign in to comment.