171 lines
3.4 KiB
Go
171 lines
3.4 KiB
Go
package services
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
|
|
"github.com/hashicorp/hcl/v2"
|
|
|
|
"code.icb4dc0.de/buildr/buildr/modules/state"
|
|
|
|
"github.com/docker/docker/client"
|
|
|
|
"code.icb4dc0.de/buildr/buildr/internal/ignore"
|
|
"code.icb4dc0.de/buildr/buildr/internal/vault"
|
|
"code.icb4dc0.de/buildr/buildr/modules"
|
|
)
|
|
|
|
type CollectionOption interface {
|
|
ApplyToCollection(svc *Collection) error
|
|
}
|
|
|
|
type collectionOptionFunc func(svc *Collection) error
|
|
|
|
func (f collectionOptionFunc) ApplyToCollection(svc *Collection) error {
|
|
return f(svc)
|
|
}
|
|
|
|
func WithTypeRegistry(registry *modules.TypeRegistry) CollectionOption {
|
|
return collectionOptionFunc(func(svc *Collection) error {
|
|
svc.registry = registry
|
|
return nil
|
|
})
|
|
}
|
|
|
|
func WithVault(v *vault.Vault) CollectionOption {
|
|
return collectionOptionFunc(func(svc *Collection) error {
|
|
svc.vault = v
|
|
return nil
|
|
})
|
|
}
|
|
|
|
func WithIgnorer(ignorer *ignore.Ignorer) CollectionOption {
|
|
return collectionOptionFunc(func(svc *Collection) error {
|
|
svc.ignorer = ignorer
|
|
return nil
|
|
})
|
|
}
|
|
|
|
func WithDB(db *state.DB) CollectionOption {
|
|
return collectionOptionFunc(func(svc *Collection) error {
|
|
svc.stateDB = db
|
|
return nil
|
|
})
|
|
}
|
|
|
|
func WithStateStore(store state.Store) CollectionOption {
|
|
return collectionOptionFunc(func(svc *Collection) error {
|
|
svc.stateStore = store
|
|
return nil
|
|
})
|
|
}
|
|
|
|
func WithCache(cache state.Cache) CollectionOption {
|
|
return collectionOptionFunc(func(svc *Collection) error {
|
|
svc.cache = cache
|
|
return nil
|
|
})
|
|
}
|
|
|
|
func WithDiagnosticsWriter(writer hcl.DiagnosticWriter) CollectionOption {
|
|
return collectionOptionFunc(func(svc *Collection) error {
|
|
svc.diagsWriter = writer
|
|
return nil
|
|
})
|
|
}
|
|
|
|
func WithDockerClientFromEnv(ctx context.Context) CollectionOption {
|
|
return collectionOptionFunc(func(svc *Collection) error {
|
|
cli, err := client.NewClientWithOpts(
|
|
client.WithHostFromEnv(),
|
|
client.WithVersionFromEnv(),
|
|
client.WithTLSClientConfigFromEnv(),
|
|
)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
cli.NegotiateAPIVersion(ctx)
|
|
|
|
svc.dockerClient = cli
|
|
|
|
return nil
|
|
})
|
|
}
|
|
|
|
func NewCollection(opts ...CollectionOption) (*Collection, error) {
|
|
svc := new(Collection)
|
|
|
|
if err := svc.With(opts...); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return svc, nil
|
|
}
|
|
|
|
type Collection struct {
|
|
registry *modules.TypeRegistry
|
|
vault *vault.Vault
|
|
dockerClient *client.Client
|
|
ignorer *ignore.Ignorer
|
|
stateStore state.Store
|
|
stateDB *state.DB
|
|
cache state.Cache
|
|
diagsWriter hcl.DiagnosticWriter
|
|
}
|
|
|
|
func (c *Collection) With(opts ...CollectionOption) error {
|
|
for i := range opts {
|
|
if err := opts[i].ApplyToCollection(c); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (c *Collection) StateStore() state.Store {
|
|
return c.stateStore
|
|
}
|
|
|
|
func (c *Collection) Cache() state.Cache {
|
|
return c.cache
|
|
}
|
|
|
|
func (c *Collection) TypeRegistry() *modules.TypeRegistry {
|
|
return c.registry
|
|
}
|
|
|
|
func (c *Collection) DockerClient() *client.Client {
|
|
return c.dockerClient
|
|
}
|
|
|
|
func (c *Collection) Vault() *vault.Vault {
|
|
return c.vault
|
|
}
|
|
|
|
func (c *Collection) Ignorer() *ignore.Ignorer {
|
|
return c.ignorer
|
|
}
|
|
|
|
func (c *Collection) DiagsWriter() hcl.DiagnosticWriter {
|
|
return c.diagsWriter
|
|
}
|
|
|
|
func (c *Collection) PluginsRepo() state.Plugins {
|
|
return c.stateDB.Plugins
|
|
}
|
|
|
|
func (c *Collection) Close() error {
|
|
var err error
|
|
if c.dockerClient != nil {
|
|
err = c.dockerClient.Close()
|
|
}
|
|
|
|
if c.stateDB != nil {
|
|
err = errors.Join(c.stateDB.Close())
|
|
}
|
|
|
|
return err
|
|
}
|