Skip to content

Commit

Permalink
feat(extensions): implement extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
Th3Shadowbroker committed Feb 26, 2025
1 parent 4ac5a88 commit c57c3b4
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 0 deletions.
12 changes: 12 additions & 0 deletions ext/extension.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package ext

type Extension interface {
Info() *Info
Configure()
Enable()
Disable()
}

type Info struct {
Name string
}
9 changes: 9 additions & 0 deletions ext/lifecycle.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package ext

const (
LifecycleConfigure = LifecyclePhase(0)
LifecycleEnable = LifecyclePhase(1)
LifecycleDisable = LifecyclePhase(2)
)

type LifecyclePhase int
49 changes: 49 additions & 0 deletions ext/registry.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package ext

import "fmt"

type Registry struct {
extensions map[string]Extension
}

func NewRegistry() *Registry {
return &Registry{
extensions: make(map[string]Extension),
}
}

func (r *Registry) Register(ext Extension) error {
extensionName := ext.Info().Name
if r.IsRegistered(ext) {
return fmt.Errorf("theres already an extension with the name '%s'", extensionName)
}

r.extensions[extensionName] = ext
return nil
}

func (r *Registry) IsRegistered(ext Extension) bool {
extensionName := ext.Info().Name
_, ok := r.extensions[extensionName]
return ok
}

func (r *Registry) RunLifecyclePhase(phase LifecyclePhase) {
for _, ext := range r.extensions {
switch phase {

case LifecycleConfigure:
ext.Configure()

case LifecycleEnable:
ext.Enable()

case LifecycleDisable:
ext.Disable()

default:
panic(fmt.Errorf("unknown lifecycle phase with id '%d'", int(phase)))

}
}
}

0 comments on commit c57c3b4

Please sign in to comment.