di

package
v5.22.0 Latest Latest
Warning

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

Go to latest
Published: Jan 24, 2026 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Overview

Package di exposes shared dependency injection helpers for KSail commands.

This package provides a runtime container for registering and resolving dependencies used by Cobra commands, enabling testable and modular command implementations through dependency injection patterns.

Key Components

Runtime: A container that manages dependency registration and lifecycle. Module: A function that registers one or more dependencies with the injector. Injector: The underlying DI container (aliased from samber/do).

Usage

Create a runtime with default dependencies:

runtime := di.NewRuntime()

Use the runtime in a Cobra command:

cmd.RunE = di.RunEWithRuntime(runtime, func(cmd *cobra.Command, injector di.Injector) error {
	timer, err := di.ResolveTimer(injector)
	// ... use dependencies
})

Or use a decorator to automatically resolve dependencies:

cmd.RunE = di.RunEWithRuntime(runtime, di.WithTimer(
	func(cmd *cobra.Command, injector di.Injector, timer timer.Timer) error {
		// timer is already resolved
	}
))

Testing

In tests, create a runtime with mock dependencies:

mockTimer := timer.NewMockTimer(t)
runtime := di.New(func(i di.Injector) error {
	do.Provide(i, func(do.Injector) (timer.Timer, error) {
		return mockTimer, nil
	})
	return nil
})

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ResolveClusterProvisionerFactory

func ResolveClusterProvisionerFactory(
	injector Injector,
) (clusterprovisioner.Factory, error)

ResolveClusterProvisionerFactory retrieves the cluster provisioner factory dependency from the injector with consistent error handling.

func ResolveTimer

func ResolveTimer(injector Injector) (timer.Timer, error)

ResolveTimer retrieves the timer dependency from the injector with consistent error handling.

func RunEWithRuntime

func RunEWithRuntime(
	runtimeContainer *Runtime,
	handler func(cmd *cobra.Command, injector Injector) error,
) func(*cobra.Command, []string) error

RunEWithRuntime returns a cobra RunE function that resolves dependencies using the provided runtime container. The handler is executed with the active command and resolved injector when the command runs.

func WithTimer

func WithTimer(
	handler func(cmd *cobra.Command, injector Injector, tmr timer.Timer) error,
) func(cmd *cobra.Command, injector Injector) error

WithTimer decorates a handler to automatically resolve the timer dependency. This higher-order function simplifies command handlers that need timer access.

Types

type Injector

type Injector = do.Injector

Injector is an alias for the underlying DI container implementation. This abstraction allows the codebase to remain independent of the specific DI library used.

type MockInjector

type MockInjector struct {
	mock.Mock
}

MockInjector is an autogenerated mock type for the Injector type

func NewMockInjector

func NewMockInjector(t interface {
	mock.TestingT
	Cleanup(func())
}) *MockInjector

NewMockInjector creates a new instance of MockInjector. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations. The first argument is typically a *testing.T value.

func (*MockInjector) Ancestors

func (_mock *MockInjector) Ancestors() []*do.Scope

Ancestors provides a mock function for the type MockInjector

func (*MockInjector) ChildByID

func (_mock *MockInjector) ChildByID(s string) (*do.Scope, bool)

ChildByID provides a mock function for the type MockInjector

func (*MockInjector) ChildByName

func (_mock *MockInjector) ChildByName(s string) (*do.Scope, bool)

ChildByName provides a mock function for the type MockInjector

func (*MockInjector) Children

func (_mock *MockInjector) Children() []*do.Scope

Children provides a mock function for the type MockInjector

func (*MockInjector) EXPECT

func (_m *MockInjector) EXPECT() *MockInjector_Expecter

func (*MockInjector) HealthCheck

func (_mock *MockInjector) HealthCheck() map[string]error

HealthCheck provides a mock function for the type MockInjector

func (*MockInjector) HealthCheckWithContext

func (_mock *MockInjector) HealthCheckWithContext(context1 context.Context) map[string]error

HealthCheckWithContext provides a mock function for the type MockInjector

func (*MockInjector) ID

func (_mock *MockInjector) ID() string

ID provides a mock function for the type MockInjector

func (*MockInjector) ListInvokedServices

func (_mock *MockInjector) ListInvokedServices() []do.ServiceDescription

ListInvokedServices provides a mock function for the type MockInjector

func (*MockInjector) ListProvidedServices

func (_mock *MockInjector) ListProvidedServices() []do.ServiceDescription

ListProvidedServices provides a mock function for the type MockInjector

func (*MockInjector) Name

func (_mock *MockInjector) Name() string

Name provides a mock function for the type MockInjector

func (*MockInjector) RootScope

func (_mock *MockInjector) RootScope() *do.RootScope

RootScope provides a mock function for the type MockInjector

func (*MockInjector) Scope

func (_mock *MockInjector) Scope(s string, fns ...func(do.Injector)) *do.Scope

Scope provides a mock function for the type MockInjector

func (*MockInjector) Shutdown

func (_mock *MockInjector) Shutdown() *do.ShutdownReport

Shutdown provides a mock function for the type MockInjector

func (*MockInjector) ShutdownWithContext

func (_mock *MockInjector) ShutdownWithContext(context1 context.Context) *do.ShutdownReport

ShutdownWithContext provides a mock function for the type MockInjector

type MockInjector_Ancestors_Call

type MockInjector_Ancestors_Call struct {
	*mock.Call
}

MockInjector_Ancestors_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'Ancestors'

func (*MockInjector_Ancestors_Call) Return

func (*MockInjector_Ancestors_Call) Run

func (*MockInjector_Ancestors_Call) RunAndReturn

func (_c *MockInjector_Ancestors_Call) RunAndReturn(run func() []*do.Scope) *MockInjector_Ancestors_Call

type MockInjector_ChildByID_Call

type MockInjector_ChildByID_Call struct {
	*mock.Call
}

MockInjector_ChildByID_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'ChildByID'

func (*MockInjector_ChildByID_Call) Return

func (*MockInjector_ChildByID_Call) Run

func (*MockInjector_ChildByID_Call) RunAndReturn

func (_c *MockInjector_ChildByID_Call) RunAndReturn(run func(s string) (*do.Scope, bool)) *MockInjector_ChildByID_Call

type MockInjector_ChildByName_Call

type MockInjector_ChildByName_Call struct {
	*mock.Call
}

MockInjector_ChildByName_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'ChildByName'

func (*MockInjector_ChildByName_Call) Return

func (*MockInjector_ChildByName_Call) Run

func (*MockInjector_ChildByName_Call) RunAndReturn

type MockInjector_Children_Call

type MockInjector_Children_Call struct {
	*mock.Call
}

MockInjector_Children_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'Children'

func (*MockInjector_Children_Call) Return

func (*MockInjector_Children_Call) Run

func (*MockInjector_Children_Call) RunAndReturn

func (_c *MockInjector_Children_Call) RunAndReturn(run func() []*do.Scope) *MockInjector_Children_Call

type MockInjector_Expecter

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

func (*MockInjector_Expecter) Ancestors

Ancestors is a helper method to define mock.On call

func (*MockInjector_Expecter) ChildByID

func (_e *MockInjector_Expecter) ChildByID(s interface{}) *MockInjector_ChildByID_Call

ChildByID is a helper method to define mock.On call

  • s string

func (*MockInjector_Expecter) ChildByName

func (_e *MockInjector_Expecter) ChildByName(s interface{}) *MockInjector_ChildByName_Call

ChildByName is a helper method to define mock.On call

  • s string

func (*MockInjector_Expecter) Children

Children is a helper method to define mock.On call

func (*MockInjector_Expecter) HealthCheck

HealthCheck is a helper method to define mock.On call

func (*MockInjector_Expecter) HealthCheckWithContext

func (_e *MockInjector_Expecter) HealthCheckWithContext(context1 interface{}) *MockInjector_HealthCheckWithContext_Call

HealthCheckWithContext is a helper method to define mock.On call

  • context1 context.Context

func (*MockInjector_Expecter) ID

ID is a helper method to define mock.On call

func (*MockInjector_Expecter) ListInvokedServices

ListInvokedServices is a helper method to define mock.On call

func (*MockInjector_Expecter) ListProvidedServices

ListProvidedServices is a helper method to define mock.On call

func (*MockInjector_Expecter) Name

Name is a helper method to define mock.On call

func (*MockInjector_Expecter) RootScope

RootScope is a helper method to define mock.On call

func (*MockInjector_Expecter) Scope

func (_e *MockInjector_Expecter) Scope(s interface{}, fns ...interface{}) *MockInjector_Scope_Call

Scope is a helper method to define mock.On call

  • s string
  • fns ...func(do.Injector)

func (*MockInjector_Expecter) Shutdown

Shutdown is a helper method to define mock.On call

func (*MockInjector_Expecter) ShutdownWithContext

func (_e *MockInjector_Expecter) ShutdownWithContext(context1 interface{}) *MockInjector_ShutdownWithContext_Call

ShutdownWithContext is a helper method to define mock.On call

  • context1 context.Context

type MockInjector_HealthCheckWithContext_Call

type MockInjector_HealthCheckWithContext_Call struct {
	*mock.Call
}

MockInjector_HealthCheckWithContext_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'HealthCheckWithContext'

func (*MockInjector_HealthCheckWithContext_Call) Return

func (*MockInjector_HealthCheckWithContext_Call) Run

func (*MockInjector_HealthCheckWithContext_Call) RunAndReturn

type MockInjector_HealthCheck_Call

type MockInjector_HealthCheck_Call struct {
	*mock.Call
}

MockInjector_HealthCheck_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'HealthCheck'

func (*MockInjector_HealthCheck_Call) Return

func (*MockInjector_HealthCheck_Call) Run

func (*MockInjector_HealthCheck_Call) RunAndReturn

func (_c *MockInjector_HealthCheck_Call) RunAndReturn(run func() map[string]error) *MockInjector_HealthCheck_Call

type MockInjector_ID_Call

type MockInjector_ID_Call struct {
	*mock.Call
}

MockInjector_ID_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'ID'

func (*MockInjector_ID_Call) Return

func (*MockInjector_ID_Call) Run

func (_c *MockInjector_ID_Call) Run(run func()) *MockInjector_ID_Call

func (*MockInjector_ID_Call) RunAndReturn

func (_c *MockInjector_ID_Call) RunAndReturn(run func() string) *MockInjector_ID_Call

type MockInjector_ListInvokedServices_Call

type MockInjector_ListInvokedServices_Call struct {
	*mock.Call
}

MockInjector_ListInvokedServices_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'ListInvokedServices'

func (*MockInjector_ListInvokedServices_Call) Return

func (*MockInjector_ListInvokedServices_Call) Run

func (*MockInjector_ListInvokedServices_Call) RunAndReturn

type MockInjector_ListProvidedServices_Call

type MockInjector_ListProvidedServices_Call struct {
	*mock.Call
}

MockInjector_ListProvidedServices_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'ListProvidedServices'

func (*MockInjector_ListProvidedServices_Call) Return

func (*MockInjector_ListProvidedServices_Call) Run

func (*MockInjector_ListProvidedServices_Call) RunAndReturn

type MockInjector_Name_Call

type MockInjector_Name_Call struct {
	*mock.Call
}

MockInjector_Name_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'Name'

func (*MockInjector_Name_Call) Return

func (*MockInjector_Name_Call) Run

func (_c *MockInjector_Name_Call) Run(run func()) *MockInjector_Name_Call

func (*MockInjector_Name_Call) RunAndReturn

func (_c *MockInjector_Name_Call) RunAndReturn(run func() string) *MockInjector_Name_Call

type MockInjector_RootScope_Call

type MockInjector_RootScope_Call struct {
	*mock.Call
}

MockInjector_RootScope_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'RootScope'

func (*MockInjector_RootScope_Call) Return

func (*MockInjector_RootScope_Call) Run

func (*MockInjector_RootScope_Call) RunAndReturn

type MockInjector_Scope_Call

type MockInjector_Scope_Call struct {
	*mock.Call
}

MockInjector_Scope_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'Scope'

func (*MockInjector_Scope_Call) Return

func (*MockInjector_Scope_Call) Run

func (_c *MockInjector_Scope_Call) Run(run func(s string, fns ...func(do.Injector))) *MockInjector_Scope_Call

func (*MockInjector_Scope_Call) RunAndReturn

func (_c *MockInjector_Scope_Call) RunAndReturn(run func(s string, fns ...func(do.Injector)) *do.Scope) *MockInjector_Scope_Call

type MockInjector_ShutdownWithContext_Call

type MockInjector_ShutdownWithContext_Call struct {
	*mock.Call
}

MockInjector_ShutdownWithContext_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'ShutdownWithContext'

func (*MockInjector_ShutdownWithContext_Call) Return

func (*MockInjector_ShutdownWithContext_Call) Run

func (*MockInjector_ShutdownWithContext_Call) RunAndReturn

type MockInjector_Shutdown_Call

type MockInjector_Shutdown_Call struct {
	*mock.Call
}

MockInjector_Shutdown_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'Shutdown'

func (*MockInjector_Shutdown_Call) Return

func (*MockInjector_Shutdown_Call) Run

func (*MockInjector_Shutdown_Call) RunAndReturn

type MockInjector_clone_Call

type MockInjector_clone_Call struct {
	*mock.Call
}

MockInjector_clone_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'clone'

func (*MockInjector_clone_Call) Return

func (*MockInjector_clone_Call) Run

func (_c *MockInjector_clone_Call) Run(run func(rootScope *do.RootScope, scope *do.Scope)) *MockInjector_clone_Call

func (*MockInjector_clone_Call) RunAndReturn

func (_c *MockInjector_clone_Call) RunAndReturn(run func(rootScope *do.RootScope, scope *do.Scope) *do.Scope) *MockInjector_clone_Call

type MockInjector_onServiceInvoke_Call

type MockInjector_onServiceInvoke_Call struct {
	*mock.Call
}

MockInjector_onServiceInvoke_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'onServiceInvoke'

func (*MockInjector_onServiceInvoke_Call) Return

func (*MockInjector_onServiceInvoke_Call) Run

func (*MockInjector_onServiceInvoke_Call) RunAndReturn

type MockInjector_serviceExistRec_Call

type MockInjector_serviceExistRec_Call struct {
	*mock.Call
}

MockInjector_serviceExistRec_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'serviceExistRec'

func (*MockInjector_serviceExistRec_Call) Return

func (*MockInjector_serviceExistRec_Call) Run

func (*MockInjector_serviceExistRec_Call) RunAndReturn

type MockInjector_serviceExist_Call

type MockInjector_serviceExist_Call struct {
	*mock.Call
}

MockInjector_serviceExist_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'serviceExist'

func (*MockInjector_serviceExist_Call) Return

func (*MockInjector_serviceExist_Call) Run

func (*MockInjector_serviceExist_Call) RunAndReturn

type MockInjector_serviceForEachRec_Call

type MockInjector_serviceForEachRec_Call struct {
	*mock.Call
}

MockInjector_serviceForEachRec_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'serviceForEachRec'

func (*MockInjector_serviceForEachRec_Call) Return

func (*MockInjector_serviceForEachRec_Call) Run

func (*MockInjector_serviceForEachRec_Call) RunAndReturn

type MockInjector_serviceForEach_Call

type MockInjector_serviceForEach_Call struct {
	*mock.Call
}

MockInjector_serviceForEach_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'serviceForEach'

func (*MockInjector_serviceForEach_Call) Return

func (*MockInjector_serviceForEach_Call) Run

func (*MockInjector_serviceForEach_Call) RunAndReturn

func (_c *MockInjector_serviceForEach_Call) RunAndReturn(run func(fn func(string, *do.Scope, any) bool)) *MockInjector_serviceForEach_Call

type MockInjector_serviceGetRec_Call

type MockInjector_serviceGetRec_Call struct {
	*mock.Call
}

MockInjector_serviceGetRec_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'serviceGetRec'

func (*MockInjector_serviceGetRec_Call) Return

func (*MockInjector_serviceGetRec_Call) Run

func (*MockInjector_serviceGetRec_Call) RunAndReturn

type MockInjector_serviceGet_Call

type MockInjector_serviceGet_Call struct {
	*mock.Call
}

MockInjector_serviceGet_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'serviceGet'

func (*MockInjector_serviceGet_Call) Return

func (*MockInjector_serviceGet_Call) Run

func (*MockInjector_serviceGet_Call) RunAndReturn

func (_c *MockInjector_serviceGet_Call) RunAndReturn(run func(s string) (any, bool)) *MockInjector_serviceGet_Call

type MockInjector_serviceHealthCheck_Call

type MockInjector_serviceHealthCheck_Call struct {
	*mock.Call
}

MockInjector_serviceHealthCheck_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'serviceHealthCheck'

func (*MockInjector_serviceHealthCheck_Call) Return

func (*MockInjector_serviceHealthCheck_Call) Run

func (*MockInjector_serviceHealthCheck_Call) RunAndReturn

type MockInjector_serviceSet_Call

type MockInjector_serviceSet_Call struct {
	*mock.Call
}

MockInjector_serviceSet_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'serviceSet'

func (*MockInjector_serviceSet_Call) Return

func (*MockInjector_serviceSet_Call) Run

func (*MockInjector_serviceSet_Call) RunAndReturn

func (_c *MockInjector_serviceSet_Call) RunAndReturn(run func(s string, v any)) *MockInjector_serviceSet_Call

type MockInjector_serviceShutdown_Call

type MockInjector_serviceShutdown_Call struct {
	*mock.Call
}

MockInjector_serviceShutdown_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'serviceShutdown'

func (*MockInjector_serviceShutdown_Call) Return

func (*MockInjector_serviceShutdown_Call) Run

func (*MockInjector_serviceShutdown_Call) RunAndReturn

type Module

type Module func(do.Injector) error

Module registers dependencies with the DI container. Each module is a function that configures one or more dependencies.

type Runtime

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

Runtime owns shared dependency registration for Cobra commands. It maintains a list of base modules that are applied to every invocation.

func New

func New(modules ...Module) *Runtime

New constructs a Runtime with the provided base modules. Modules are applied in the order supplied when invoking commands.

func NewRuntime

func NewRuntime() *Runtime

NewRuntime constructs the shared runtime container used by root command and tests. It registers default implementations for timer and cluster provisioner factory.

func (*Runtime) Invoke

func (r *Runtime) Invoke(
	handler func(Injector) error,
	extraModules ...Module,
) error

Invoke builds a fresh injector, applies base and extra modules, and executes the provided handler function. The injector is automatically shut down after the handler completes.

Jump to

Keyboard shortcuts

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