config

package
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Mar 27, 2025 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Overview

Package config provides configuration management for the opsy CLI application.

The package handles:

  • Loading configuration from YAML files
  • Environment variable binding
  • Configuration validation
  • Default values
  • Directory structure setup
  • Logging setup

Configuration Structure:

Configuration {
  UI:        UIConfiguration        // UI theme and styling
  Logging:   LoggingConfiguration   // Log file path and level
  Anthropic: AnthropicConfiguration // API settings for Anthropic
  Tools:     ToolsConfiguration     // Global tool settings and exec configuration
}

Usage:

manager := config.New()
if err := manager.LoadConfig(); err != nil {
    log.Fatal(err)
}
config := manager.GetConfig()

Environment Variables:

  • ANTHROPIC_API_KEY: API key for Anthropic
  • OPSY_UI_THEME: UI theme name
  • OPSY_LOGGING_LEVEL: Log level (debug, info, warn, error)
  • OPSY_ANTHROPIC_MODEL: Model name
  • OPSY_ANTHROPIC_TEMPERATURE: Temperature value
  • OPSY_ANTHROPIC_MAX_TOKENS: Maximum tokens for completion
  • OPSY_TOOLS_TIMEOUT: Global timeout for tools in seconds
  • OPSY_TOOLS_EXEC_TIMEOUT: Timeout for exec tool in seconds
  • OPSY_TOOLS_EXEC_SHELL: Shell to use for command execution

Directory Structure:

~/.opsy/
├── config.yaml  // Configuration file
├── log.log     // Default log file
├── cache/      // Cache directory for temporary files
└── tools/      // Tool-specific data and configurations

The package uses the following error constants for error handling:

  • ErrCreateDirs: Returned when directory creation fails
  • ErrCreateConfigDir: Returned when config directory creation fails
  • ErrCreateCacheDir: Returned when cache directory creation fails
  • ErrReadConfig: Returned when config file cannot be read
  • ErrWriteConfig: Returned when config file cannot be written
  • ErrUnmarshalConfig: Returned when config parsing fails
  • ErrValidateConfig: Returned when configuration validation fails
  • ErrMissingAPIKey: Returned when Anthropic API key is missing
  • ErrInvalidTemp: Returned when temperature is not between 0 and 1
  • ErrInvalidMaxTokens: Returned when max tokens is not positive
  • ErrInvalidLogLevel: Returned when log level is invalid
  • ErrInvalidTheme: Returned when UI theme is invalid
  • ErrInvalidShell: Returned when exec shell is invalid or not found
  • ErrOpenLogFile: Returned when log file cannot be opened

Validation:

The package performs extensive validation of the configuration:

  • Anthropic API key must be provided
  • Temperature must be between 0 and 1
  • Max tokens must be positive
  • Log level must be one of: debug, info, warn, error
  • UI theme must be a valid theme name
  • Exec shell must be a valid and executable shell path

Thread Safety:

The configuration is safe for concurrent access after loading. The GetConfig method returns a copy of the configuration to prevent race conditions.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrCreateConfigDir is returned when the config directory cannot be created.
	ErrCreateConfigDir = errors.New("failed to create config directory")
	// ErrCreateCacheDir is returned when the cache directory cannot be created.
	ErrCreateCacheDir = errors.New("failed to create cache directory")
	// ErrCreateDirs is returned when the directories cannot be created.
	ErrCreateDirs = errors.New("failed to create directories")
	// ErrReadConfig is returned when the config file cannot be read.
	ErrReadConfig = errors.New("failed to read config")
	// ErrUnmarshalConfig is returned when the config file cannot be unmarshalled.
	ErrUnmarshalConfig = errors.New("failed to unmarshal config")
	// ErrMissingAPIKey is returned when the Anthropic API key is missing.
	ErrMissingAPIKey = errors.New("anthropic API key is required")
	// ErrInvalidTemp is returned when the Anthropic temperature is invalid.
	ErrInvalidTemp = errors.New("anthropic temperature must be between 0 and 1")
	// ErrInvalidMaxTokens is returned when the Anthropic max tokens are invalid.
	ErrInvalidMaxTokens = errors.New("anthropic max tokens must be greater than 0")
	// ErrInvalidLogLevel is returned when the logging level is invalid.
	ErrInvalidLogLevel = errors.New("invalid logging level")
	// ErrInvalidTheme is returned when the theme is invalid.
	ErrInvalidTheme = errors.New("invalid theme")
	// ErrOpenLogFile is returned when the log file cannot be opened.
	ErrOpenLogFile = errors.New("failed to open log file")
	// ErrWriteConfig is returned when the config file cannot be written.
	ErrWriteConfig = errors.New("failed to write config")
	// ErrValidateConfig is returned when the config is invalid.
	ErrValidateConfig = errors.New("invalid config")
	// ErrInvalidShell is returned when the shell is invalid.
	ErrInvalidShell = errors.New("invalid exec shell")
)

Functions

This section is empty.

Types

type AnthropicConfiguration

type AnthropicConfiguration struct {
	// APIKey is the API key for the Anthropic API.
	APIKey string `mapstructure:"api_key" yaml:"api_key"`
	// Model is the model to use for the Anthropic API.
	Model string `yaml:"model"`
	// Temperature is the temperature to use for the Anthropic API.
	Temperature float64 `yaml:"temperature"`
	// MaxTokens is the maximum number of tokens to use for the Anthropic API.
	MaxTokens int64 `mapstructure:"max_tokens" yaml:"max_tokens"`
}

AnthropicConfiguration is the configuration for the Anthropic API.

type Config

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

ConfigManager is the configuration manager for the opsy CLI.

func New

func New() *Config

New creates a new config instance.

func (*Config) GetConfig

func (c *Config) GetConfig() Configuration

GetConfig returns the current configuration.

func (*Config) GetLogger

func (c *Config) GetLogger() (*slog.Logger, error)

GetLogger returns a logger that writes to the log file.

func (*Config) LoadConfig

func (c *Config) LoadConfig() error

LoadConfig loads the configuration from the config file.

type Configuration

type Configuration struct {
	// UI is the configuration for the UI.
	UI UIConfiguration `yaml:"ui"`
	// Logging is the configuration for the logging.
	Logging LoggingConfiguration `yaml:"logging"`
	// Anthropic is the configuration for the Anthropic API.
	Anthropic AnthropicConfiguration `yaml:"anthropic"`
	// Tools is the configuration for the tools.
	Tools ToolsConfiguration `yaml:"tools"`
}

Configuration is the configuration for the opsy CLI.

type Configurer

type Configurer interface {
	// LoadConfig loads the configuration from the config file.
	LoadConfig() error
	// GetConfig returns the current configuration.
	GetConfig() Configuration
	// GetLogger returns the default logger.
	GetLogger() (*slog.Logger, error)
}

Configurer is an interface for managing configuration.

type ExecToolConfiguration

type ExecToolConfiguration struct {
	// Timeout is the maximum duration in seconds for a tool to execute.
	Timeout int64 `yaml:"timeout"`
	// Shell is the shell to use for the exec tool.
	Shell string `yaml:"shell"`
}

ExecToolConfiguration is the configuration for the exec tool.

type LoggingConfiguration

type LoggingConfiguration struct {
	// Path is the path to the log file.
	Path string `yaml:"path"`
	// Level is the logging level.
	Level string `yaml:"level"`
}

LoggingConfiguration is the configuration for the logging.

type ToolsConfiguration

type ToolsConfiguration struct {
	// Timeout is the maximum duration in seconds for a tool to execute.
	Timeout int64 `yaml:"timeout"`
	// Exec is the configuration for the exec tool.
	Exec ExecToolConfiguration `yaml:"exec"`
}

ToolsConfiguration is the configuration for the tools.

type UIConfiguration

type UIConfiguration struct {
	// Theme is the theme for the UI.
	Theme string `yaml:"theme"`
}

UIConfiguration is the configuration for the UI.

Jump to

Keyboard shortcuts

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