app

package
v0.0.21 Latest Latest
Warning

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

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

Documentation

Overview

Package app provides the application context and dependency management for the starmap CLI. It follows idiomatic Go patterns for CLI applications by centralizing configuration, dependency injection, and lifecycle management.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Context

func Context() (context.Context, context.CancelFunc)

Context creates a new context with signal handling for the application. This is a convenience wrapper around ContextWithSignals that uses context.Background() as the parent.

func ContextWithSignals

func ContextWithSignals(parent context.Context) (context.Context, context.CancelFunc)

ContextWithSignals creates a context that is cancelled when the application receives an interrupt or termination signal. This enables graceful shutdown.

func ExitOnError

func ExitOnError(err error)

ExitOnError is a helper that prints an error and exits with status 1. This is meant to be used in main.go for top-level error handling.

func NewLogger

func NewLogger(config *Config) zerolog.Logger

NewLogger creates a configured logger based on the application configuration. Log level precedence (highest to lowest):

  1. --log-level flag (explicit always wins)
  2. -v/--verbose flag (shortcut for debug)
  3. -q/--quiet flag (shortcut for warn)
  4. LOG_LEVEL environment variable
  5. Default (info)

Types

type App

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

App represents the starmap application with all its dependencies. It provides a centralized place for configuration, logging, and the starmap instance, following the dependency injection pattern.

func New

func New(version, commit, date, builtBy string, opts ...Option) (*App, error)

New creates a new App instance with the given version information. The app is initialized with default configuration that can be customized using functional options.

func (*App) BuiltBy

func (a *App) BuiltBy() string

BuiltBy returns the build system identifier.

func (*App) Catalog

func (a *App) Catalog() (catalogs.Catalog, error)

Catalog returns a deep copy of the current catalog from the starmap instance. This is a convenience method that handles the starmap initialization and catalog retrieval in one call.

Thread Safety: This method is thread-safe because sm.Catalog() acquires a read lock and returns a deep copy (see starmap.go:71-76). Each caller receives an independent catalog instance with no shared mutable state.

Performance: ~350-400ns per call with 9-10 allocations (single copy).

Per docs/ARCHITECTURE.md § Thread Safety section, this ALWAYS returns a deep copy (provided by sm.Catalog()).

func (*App) Commit

func (a *App) Commit() string

Commit returns the git commit hash.

func (*App) Config

func (a *App) Config() *Config

Config returns the application configuration.

func (*App) Date

func (a *App) Date() string

Date returns the build date.

func (*App) Execute

func (a *App) Execute(ctx context.Context, args []string) error

Execute runs the starmap CLI application with the given arguments. This is the main entry point called from main.go.

func (*App) Logger

func (a *App) Logger() *zerolog.Logger

Logger returns the application logger.

func (*App) NewAuthCommand added in v0.0.17

func (a *App) NewAuthCommand() *cobra.Command

NewAuthCommand returns a new auth command with app dependencies.

func (*App) NewDepsCommand added in v0.0.17

func (a *App) NewDepsCommand() *cobra.Command

NewDepsCommand returns a new deps command with app dependencies.

func (*App) NewEmbedCommand added in v0.0.17

func (a *App) NewEmbedCommand() *cobra.Command

NewEmbedCommand returns a new embed command with app dependencies.

func (*App) NewFetchCommand added in v0.0.17

func (a *App) NewFetchCommand() *cobra.Command

NewFetchCommand returns a new fetch command with app dependencies.

func (*App) NewGenerateCommand added in v0.0.17

func (a *App) NewGenerateCommand() *cobra.Command

NewGenerateCommand returns a new generate command with app dependencies.

func (*App) NewInstallCommand added in v0.0.17

func (a *App) NewInstallCommand() *cobra.Command

NewInstallCommand returns a new install command with app dependencies.

func (*App) NewListCommand added in v0.0.17

func (a *App) NewListCommand() *cobra.Command

NewListCommand returns a new list command with app dependencies.

func (*App) NewManCommand added in v0.0.17

func (a *App) NewManCommand() *cobra.Command

NewManCommand returns a new man command.

func (*App) NewServeCommand added in v0.0.17

func (a *App) NewServeCommand() *cobra.Command

NewServeCommand returns a new serve command with app dependencies.

func (*App) NewUninstallCommand added in v0.0.17

func (a *App) NewUninstallCommand() *cobra.Command

NewUninstallCommand returns a new uninstall command with app dependencies.

func (*App) NewUpdateCommand added in v0.0.17

func (a *App) NewUpdateCommand() *cobra.Command

NewUpdateCommand returns a new update command with app dependencies.

func (*App) NewValidateCommand added in v0.0.17

func (a *App) NewValidateCommand() *cobra.Command

NewValidateCommand returns a new validate command with app dependencies.

func (*App) NewVersionCommand added in v0.0.17

func (a *App) NewVersionCommand() *cobra.Command

NewVersionCommand returns a new version command.

func (*App) OutputFormat

func (a *App) OutputFormat() string

OutputFormat returns the configured output format.

func (*App) Shutdown

func (a *App) Shutdown(ctx context.Context) error

Shutdown performs graceful shutdown of the application. It stops any running background tasks and cleans up resources. The context controls the shutdown timeout - shutdown will abort if context is cancelled.

func (*App) Starmap

func (a *App) Starmap(opts ...starmap.Option) (starmap.Client, 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).

This consolidates the previous Starmap() and StarmapWithOptions() methods into a single, more idiomatic Go interface following the variadic options pattern.

func (*App) Version

func (a *App) Version() string

Version returns the version information.

type Config

type Config struct {
	// Global flags
	Verbose bool
	Quiet   bool
	NoColor bool
	Format  string

	// Config file
	ConfigFile string

	// Starmap configuration
	LocalPath          string
	UseEmbeddedCatalog bool
	AutoUpdatesEnabled bool
	AutoUpdateInterval time.Duration
	RemoteServerURL    string
	RemoteServerAPIKey string
	RemoteServerOnly   bool

	// Logging configuration
	LogLevel  string
	LogFormat string
	LogOutput string
}

Config holds the application configuration loaded from various sources including config files, environment variables, and .env files.

func LoadConfig

func LoadConfig() (*Config, error)

LoadConfig loads configuration from all sources in order of precedence: 1. Command-line flags (handled by cobra) 2. Environment variables 3. .env files 4. Config file (~/.starmap.yaml) 5. Defaults.

func (*Config) UpdateFromFlags

func (c *Config) UpdateFromFlags(verbose, quiet, noColor bool, format, logLevel string)

UpdateFromFlags updates config values from parsed command flags. This should be called after cobra parses flags to ensure flag values take precedence over config file and env vars.

type Option

type Option func(*App) error

Option is a functional option for configuring the App.

func WithClient

func WithClient(sm starmap.Client) Option

WithClient sets a custom starmap instance (useful for testing).

func WithConfig

func WithConfig(config *Config) Option

WithConfig sets a custom configuration.

func WithLogger

func WithLogger(logger *zerolog.Logger) Option

WithLogger sets a custom logger.

Jump to

Keyboard shortcuts

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