From a63e656f69c78cabff151c62c0d8d1b4d488fb8c Mon Sep 17 00:00:00 2001 From: Tom Wiesing Date: Wed, 21 Sep 2022 13:03:24 +0200 Subject: [PATCH] internal/component: Update documentation and linting --- internal/component/component.go | 4 +- internal/wisski/component.go | 109 ++++++++++++++++++-------------- 2 files changed, 64 insertions(+), 49 deletions(-) diff --git a/internal/component/component.go b/internal/component/component.go index 5937089..1a27d03 100644 --- a/internal/component/component.go +++ b/internal/component/component.go @@ -32,6 +32,7 @@ type ComponentBase struct { Core // the core of the associated distillery } +//lint:ignore U1000 used to implement the private methods of [Component] func (cb *ComponentBase) getBase() *ComponentBase { return cb } @@ -42,7 +43,8 @@ func (cb *ComponentBase) getBase() *ComponentBase { // // dis is the distillery to initialize components for // field is a pointer to the appropriate struct field within the distillery components -// init is called with a new non-nil component to initialize it. It may be nil, to indicate no initialization is required. +// init is called with a new non-nil component to initialize it. +// It may be nil, to indicate no additional initialization is required. // // makeComponent returns the new or existing component instance func Initialize[C Component](core Core, field *lazy.Lazy[C], init func(C)) C { diff --git a/internal/wisski/component.go b/internal/wisski/component.go index 716af49..22ef65d 100644 --- a/internal/wisski/component.go +++ b/internal/wisski/component.go @@ -31,60 +31,18 @@ type components struct { instances lazy.Lazy[*instances.Instances] } -func (dis *Distillery) Components() []component.Component { - return []component.Component{ - dis.Web(), - dis.Control(), - dis.SSH(), - dis.Triplestore(), - dis.SQL(), - dis.Instances(), - } -} - -// Backupable returns all the components that can be backuped up. -func (dis *Distillery) Backupable() []component.Backupable { - return getComponents[component.Backupable](dis) -} - -// Installables returns all components that can be installed -func (dis *Distillery) Installables() []component.Installable { - return getComponents[component.Installable](dis) -} - -// Installables returns all components that can be installed -func (dis *Distillery) Updateable() []component.Updatable { - return getComponents[component.Updatable](dis) -} - -// Provisionable returns all components which can be provisioned -func (dis *Distillery) Provisionable() []component.Provisionable { - return getComponents[component.Provisionable](dis) -} - -func getComponents[C component.Component](dis *Distillery) (result []C) { - all := dis.Components() - - result = make([]C, 0, len(all)) - for _, c := range all { - sc, ok := c.(C) - if !ok { - continue - } - result = append(result, sc) - } - - return -} +// +// Individual Components +// func (dis *Distillery) Web() *web.Web { return component.Initialize(dis.Core, &dis.components.web, nil) } func (d *Distillery) Control() *control.Control { - return component.Initialize(d.Core, &d.components.control, func(ddis *control.Control) { - ddis.ResolverFile = core.PrefixConfig - ddis.Instances = d.Instances() + return component.Initialize(d.Core, &d.components.control, func(control *control.Control) { + control.ResolverFile = core.PrefixConfig + control.Instances = d.Instances() }) } @@ -114,3 +72,58 @@ func (dis *Distillery) Instances() *instances.Instances { instances.TS = dis.Triplestore() }) } + +// +// ALL COMPONENTS +// + +func (dis *Distillery) Components() []component.Component { + return []component.Component{ + dis.Web(), + dis.Control(), + dis.SSH(), + dis.Triplestore(), + dis.SQL(), + dis.Instances(), + } +} + +// +// COMPONENT SUBTYPE GETTERS +// + +// Backupable returns all the components that can be backuped up. +func (dis *Distillery) Backupable() []component.Backupable { + return getComponentSubtype[component.Backupable](dis) +} + +// Installables returns all components that can be installed +func (dis *Distillery) Installables() []component.Installable { + return getComponentSubtype[component.Installable](dis) +} + +// Installables returns all components that can be installed +func (dis *Distillery) Updateable() []component.Updatable { + return getComponentSubtype[component.Updatable](dis) +} + +// Provisionable returns all components which can be provisioned +func (dis *Distillery) Provisionable() []component.Provisionable { + return getComponentSubtype[component.Provisionable](dis) +} + +// getComponentSubtype gets all components of type T +func getComponentSubtype[T component.Component](dis *Distillery) (components []T) { + all := dis.Components() + + components = make([]T, 0, len(all)) + for _, c := range all { + sc, ok := c.(T) + if !ok { + continue + } + components = append(components, sc) + } + + return +}