core

package module
v0.0.7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 16, 2025 License: Apache-2.0 Imports: 15 Imported by: 0

README

go-core

A polymorph adapter system

Adapter lifecycle

  • Load adapter configuration
  • Load item configuration
  • Set context
  • Set adapter dependencies (Depender)
  • Set item dependencies (Depender)
  • Set adapter struct dependencies
  • Set item struct dependencies
  • Validate dependencies
  • Hydrate

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	NewExecutorAdapter      = NewAdapterAs[Executor]
	NewBuilderAdapter       = NewAdapterAs[Builder]
	NewCreaterAdapter       = NewAdapterAs[Creater]
	NewUpdaterAdapter       = NewAdapterAs[Updater]
	NewDeleterAdapter       = NewAdapterAs[Deleter]
	NewReloaderAdapter      = NewAdapterAs[Reloader]
	NewLifecycleAdapter     = NewAdapterAs[Lifecycle]
	NewStarterAdapter       = NewAdapterAs[Starter]
	NewStopperAdapter       = NewAdapterAs[Stopper]
	NewRunnerAdapter        = NewAdapterAs[Runner]
	NewListerAdapter        = NewAdapterAs[Lister]
	NewDescriberAdapter     = NewAdapterAs[Describer]
	NewBrowserAdapter       = NewAdapterAs[Browser]
	NewAuthenticatorAdapter = NewAdapterAs[Authenticator]
	NewConfigurerAdapter    = NewAdapterAs[Configurer]
	NewUploaderAdapter      = NewAdapterAs[Uploader]
	NewDownloaderAdapter    = NewAdapterAs[Downloader]
	NewTransfererAdapter    = NewAdapterAs[Transferer]
	NewFilterAdapter        = NewAdapterAs[Filter]
	NewPrunerAdapter        = NewAdapterAs[Pruner]
)

Functions

func Adapters

func Adapters() map[string]Adapter

Adapters returns a shallow copy of the cached adapters of the default registry. (Mostly for debugging / introspection.)

func IsRegistered

func IsRegistered(adapterID string) bool

func LoadAllAdapters

func LoadAllAdapters[T any](adapterID string) ([]T, error)

LoadAllAdapters loads all configured items for adapterID from the default registry.

func LoadAllAdaptersFrom added in v0.0.5

func LoadAllAdaptersFrom[T any](r *Registry, adapterID string) ([]T, error)

LoadAllAdaptersFrom loads all configured items for adapterID from the given registry and returns them as []T, skipping items that fail type assertion or construction.

func NewAdapterAs

func NewAdapterAs[T any](adapterID string, args ...string) (T, error)

NewAdapterAs constructs an adapter from the default registry and asserts it implements T.

func NewAdapterAsFrom added in v0.0.5

func NewAdapterAsFrom[T any](r *Registry, adapterID string, args ...string) (T, error)

NewAdapterAsFrom constructs an adapter from the given registry and asserts it implements T.

func Register

func Register(adapterID string, f ZeroFactory)

Register adds an Adapter constructor to the global registry.

fn MUST return a Adapter that is fully zero-initialised. There should be no heavy lifting

func init() {
	core.Register("adapter-id", func() Adapter {
		return &GCloud{} // zero cost constructor
	})
}

Later the framework will do:

a := registry["adapter-id"]()   // clone via factory

if c, ok := a.(Configurable); ok {
	cfg := c.ConfigPtr()     // returns pointer to struct
	loadJSON(cfg)            // unmarshal adapter-level config
}

if ic, ok := a.(ItemConfigurable); ok {
	itemCfg := ic.ItemConfigPtr() // pointer to per-item struct
	loadItemJSON(itemID, itemCfg) // unmarshals one item
}

and run the adapter

func SetDefaultSearchMap added in v0.0.5

func SetDefaultSearchMap(sm *SearchMap)

(Optional) Lower-level convenience if you already built a SearchMap yourself.

func SetLogLevel added in v0.0.5

func SetLogLevel(level LogLevel)

SetLogLevel changes the verbosity for the default std logger.

func SetLogger added in v0.0.5

func SetLogger(l Logger)

SetLogger overrides the global Logger. Passing nil restores the default logger.

Types

type Adapter

type Adapter any

Adapter is the marker type for all adapters.

func NewAdapter

func NewAdapter(adapterID string, args ...string) (Adapter, error)

type Authenticator

type Authenticator interface {
	Login(ctx context.Context) error
}

Authentication

type Browser

type Browser interface {
	Lister
	Describer
}

type Builder

type Builder interface {
	Build(ctx context.Context, in ...string) error // create or first-time apply
}

type Configurable

type Configurable interface {
	ConfigPtr() any
}

Configurable is called with configurations that match the adapter ID

type Configurer

type Configurer interface {
	Configure(ctx context.Context) error // idempotent environment setup
}

type Contextual

type Contextual interface {
	SetContext(path string) // idempotent environment setup
}

type Creater

type Creater interface {
	Create(ctx context.Context, in ...string) error // create or first-time apply
}

type Deleter

type Deleter interface {
	Delete(ctx context.Context, in ...string) error // remove everything
}

type DepRef

type DepRef struct {
	Adapter string   `json:"adapter"`        // required
	Name    string   `json:"name,omitempty"` // fallback on config name
	Args    []string `json:"args,omitempty"` // extra CLI-style args

}

type Depender

type Depender interface {
	AddDependency(name string, adapter Adapter)
}

type Describer

type Describer interface {
	Describe(ctx context.Context, name string) (string, error)
}

type Downloader

type Downloader interface {
	Download(ctx context.Context, in ...string) error
}

type Executor added in v0.0.4

type Executor interface {
	Run(ctx context.Context, name string, args ...string) error
	Output(ctx context.Context, name string, args ...string) ([]byte, error)
}

Executor is a generic "do one unit of work" role. It is intentionally minimal so it can represent pipeline steps, tasks, jobs, commands, etc. in higher-level systems.

type FileSystem added in v0.0.5

type FileSystem interface {
	WalkDir(root string, fn fs.WalkDirFunc) error
	ReadFile(name string) ([]byte, error)
}

type Filter

type Filter interface {
	Filter(ctx context.Context, filter string) ([]string, error)
}

type FilterOf

type FilterOf[T any] interface {
	Filter(ctx context.Context, filter string) ([]T, error)
}

type Hydrater

type Hydrater interface {
	Hydrate(ctx context.Context) error
}

type ItemConfigurable

type ItemConfigurable interface {
	ItemConfigPtr(name string) any
}

ItemConfigurable is called for named configurations (name string) Adapters should capture the name here if they're needed. If both a named configuration and adapter configuration exists, both will be called.

type Lifecycle

type Lifecycle interface {
	Creater
	Updater
	Deleter
}

Every adapter must reconcile its resource.

type Lister

type Lister interface {
	List(ctx context.Context) ([]string, error)
}

type ListerOf

type ListerOf[T any] interface {
	List(ctx context.Context) ([]T, error)
}

Optional

type LogLevel added in v0.0.5

type LogLevel int

LogLevel controls which messages are emitted by the default logger.

const (
	LogDebug LogLevel = iota
	LogInfo
	LogWarn
	LogError
)

func CurrentLogLevel added in v0.0.5

func CurrentLogLevel() LogLevel

CurrentLogLevel returns the current default log level.

type Logger added in v0.0.5

type Logger interface {
	Debug(v ...any)
	Debugf(format string, args ...any)
	Info(v ...any)
	Infof(format string, args ...any)
	Warn(v ...any)
	Warnf(format string, args ...any)
	Error(v ...any)
	Errorf(format string, args ...any)
}

Logger is the common logging interface used by core and adapters.

The non-formatting methods (Debug/Info/Warn/Error) behave like log.Println: they append a single newline. The *f methods ensure exactly one trailing newline even if the format already contains one.

func Log added in v0.0.5

func Log() Logger

Log returns the current global Logger.

type MetaHeader

type MetaHeader struct {
	Name         string            `json:"name"` // fallback on filename
	APIVersion   string            `json:"api_version"`
	Adapter      string            `json:"adapter,omitempty"`
	Dependencies map[string]DepRef `json:"dependencies"`
	RawSpec      json.RawMessage   `json:"spec"`    // adapter-specific payload
	Context      string            `json:"context"` // project path (rel or abs)
}

The Context is managed by the system to ensure those paths are adjusted accordingly when the system runs in a container (with volume mounts). Adapters still need to use the value manually to use the context.

func LoadAll

func LoadAll(adapterID string) ([]*MetaHeader, error)

LoadAll is a convenience function that uses the default registry's SearchMap.

type Pruner

type Pruner interface {
	// PruneAll(ctx context.Context) error
	Prune(ctx context.Context, filter string) error
}

type Registry added in v0.0.5

type Registry struct {
	// contains filtered or unexported fields
}

func DefaultRegistry added in v0.0.5

func DefaultRegistry() *Registry

DefaultRegistry returns the package-global registry used by the helper funcs.

func (*Registry) IsRegistered added in v0.0.5

func (r *Registry) IsRegistered(adapterID string) bool

IsRegistered reports whether an adapterID has a registered factory.

func (*Registry) NewAdapter added in v0.0.5

func (r *Registry) NewAdapter(adapterID string, args ...string) (Adapter, error)

func (*Registry) Register added in v0.0.5

func (r *Registry) Register(adapterID string, f ZeroFactory)

Register adds an Adapter constructor to the registry.

func (*Registry) SetSearchMap added in v0.0.5

func (r *Registry) SetSearchMap(sm *SearchMap)

SetSearchMap sets the SearchMap used by this registry. In typical CLI usage it's set once at startup; we don't worry about races here.

func (*Registry) SetSearchPath added in v0.0.5

func (r *Registry) SetSearchPath(root string) (*SearchMap, error)

SetSearchPath constructs a SearchMap rooted at the given path and installs it into this registry. It returns the created SearchMap.

type Reloader added in v0.0.3

type Reloader interface {
	Reload(ctx context.Context, in ...string) error // reload / rollout replace
}

type Runner

type Runner interface {
	Starter
	Stopper
}

type SearchMap

type SearchMap struct {
	Short map[string][]string // basename (no .json) -> []absolute paths
	Full  map[string]string   // relative/key (no .json) -> absolute path
	// contains filtered or unexported fields
}

func NewSearchMap

func NewSearchMap(root string) (*SearchMap, error)

Thin wrapper using osFS.

func NewSearchMapWithFS added in v0.0.5

func NewSearchMapWithFS(root string, fsys FileSystem) (*SearchMap, error)

func SetDefaultSearchPath added in v0.0.5

func SetDefaultSearchPath(root string) (*SearchMap, error)

Convenience: configure the default registry's search path.

func (*SearchMap) Load

func (sm *SearchMap) Load(name string, verbose bool) (*MetaHeader, error)

Load locates, reads, unmarshals and post-processes a MetaHeader. Should ensure MetaHeader.Name is set.

func (*SearchMap) LoadAll

func (sm *SearchMap) LoadAll(adapterID string) ([]*MetaHeader, error)

LoadAll walks through every indexed config, loads it, and returns those whose Adapter matches adapterID (or all if adapterID=="").

func (*SearchMap) Resolve

func (sm *SearchMap) Resolve(name string) (string, error)

Resolve finds the one absolute path for name. name can be either the short key ("dev") or full key ("env/dev").

type Starter

type Starter interface {
	Start(ctx context.Context, in ...string) error // scale >0 / run
}

Optional run-time control.

type Stopper

type Stopper interface {
	Stop(ctx context.Context, in ...string) error // scale 0 / halt
}

type Transferer

type Transferer interface {
	Uploader
	Downloader
}

type Updater

type Updater interface {
	Update(ctx context.Context, in ...string) error // idempotent patch / rebuild
}

type Uploader

type Uploader interface {
	Upload(ctx context.Context, in ...string) error
}

type ZeroFactory

type ZeroFactory func() Adapter

ZeroFactory should construct a zero-cost, zero-valued adapter.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL