application

package
v0.0.25 Latest Latest
Warning

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

Go to latest
Published: Oct 21, 2025 License: AGPL-3.0 Imports: 3 Imported by: 0

Documentation

Overview

Package application provides the application interface for Starmap commands.

The Application interface defines the contract between the application layer and command implementations, enabling dependency injection and testability.

Design Principles:

  • Accept interfaces, return structs (Go proverb)
  • Define interfaces where they're used, not where they're implemented
  • Keep interfaces small and focused

Usage in Commands:

import (
    "context"
    "github.com/agentstation/starmap/internal/cmd/application"
)

func NewCommand(app application.Application) *cobra.Command {
    return &cobra.Command{
        RunE: func(cmd *cobra.Command, args []string) error {
            ctx := cmd.Context() // context.Context from cobra
            catalog, err := app.Catalog()
            if err != nil {
                return err
            }
            // ... use catalog
            return nil
        },
    }
}

Testing with Mocks:

mock := &application.Mock{
    CatalogFunc: func() (catalogs.Catalog, error) {
        return testCatalog, nil
    },
    LoggerFunc: func() *zerolog.Logger {
        logger := zerolog.Nop()
        return &logger
    },
}
cmd := NewCommand(mock)
// ... test command behavior

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Application

type Application interface {
	// Catalog returns a deep copy of the current catalog from the default starmap instance.
	// Per docs/ARCHITECTURE.md § Thread Safety section, this ALWAYS returns a deep copy to prevent data races.
	// This is a convenience method for commands that just need catalog access.
	Catalog() (catalogs.Catalog, error)

	// Starmap returns the starmap instance with optional configuration.
	// When called without options, returns the default cached instance (lazy-initialized, thread-safe).
	// When called with options, creates a new instance with custom configuration (no caching).
	//
	// Examples:
	//   sm, err := app.Starmap()                    // default instance (cached)
	//   sm, err := app.Starmap(opt1, opt2)          // custom instance (new)
	Starmap(opts ...starmap.Option) (starmap.Client, error)

	// Logger returns the configured logger instance.
	// Commands should use this for all logging operations.
	Logger() *zerolog.Logger

	// OutputFormat returns the configured output format (json, yaml, table, etc).
	// Commands that support different output formats should use this.
	OutputFormat() string

	// Version returns the application version string.
	Version() string

	// Commit returns the git commit hash.
	Commit() string

	// Date returns the build date.
	Date() string

	// BuiltBy returns the build system identifier.
	BuiltBy() string
}

Application provides the application interface that commands need. The App struct from cmd/starmap/app automatically implements this interface, providing dependency injection for commands while maintaining testability.

Commands should accept this interface rather than the concrete App type, allowing for easier testing with mock implementations.

Thread Safety: All methods must be safe for concurrent access.

type Mock

type Mock struct {
	CatalogFunc      func() (catalogs.Catalog, error)
	StarmapFunc      func(opts ...starmap.Option) (starmap.Client, error)
	LoggerFunc       func() *zerolog.Logger
	OutputFormatFunc func() string
	VersionFunc      func() string
	CommitFunc       func() string
	DateFunc         func() string
	BuiltByFunc      func() string
}

Mock provides a mock implementation of Application for testing. Each method can be customized by setting the corresponding function field. If a function field is nil, the method returns a default/zero value.

Example Usage:

mock := &application.Mock{
    CatalogFunc: func() (catalogs.Catalog, error) {
        return testCatalog, nil
    },
    LoggerFunc: func() *zerolog.Logger {
        logger := zerolog.Nop()
        return &logger
    },
}
cmd := list.NewCommand(mock)
// ... test command

func (*Mock) BuiltBy

func (m *Mock) BuiltBy() string

BuiltBy returns builtBy using the mock function or "test".

func (*Mock) Catalog

func (m *Mock) Catalog() (catalogs.Catalog, error)

Catalog returns a catalog using the mock function or nil.

func (*Mock) Commit

func (m *Mock) Commit() string

Commit returns commit using the mock function or "unknown".

func (*Mock) Date

func (m *Mock) Date() string

Date returns date using the mock function or "unknown".

func (*Mock) Logger

func (m *Mock) Logger() *zerolog.Logger

Logger returns a logger using the mock function or a no-op logger.

func (*Mock) OutputFormat

func (m *Mock) OutputFormat() string

OutputFormat returns output format using the mock function or "table".

func (*Mock) Starmap

func (m *Mock) Starmap(opts ...starmap.Option) (starmap.Client, error)

Starmap returns a starmap using the mock function or nil.

func (*Mock) Version

func (m *Mock) Version() string

Version returns version using the mock function or "dev".

Jump to

Keyboard shortcuts

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