config

package
v0.9.0-beta.1 Latest Latest
Warning

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

Go to latest
Published: Mar 12, 2025 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package config provides a centralized configuration management system for the Nexlayer CLI. It supports loading configuration from multiple sources (files, environment variables, flags) and provides a unified interface for accessing configuration values.

Package config provides a centralized configuration management system for the Nexlayer CLI. It supports loading configuration from multiple sources (files, environment variables, flags) and provides a unified interface for accessing configuration values.

Index

Constants

View Source
const DefaultConfigDir = ".config/nexlayer"

DefaultConfigDir is the default directory for the configuration file

View Source
const DefaultConfigName = "config"

DefaultConfigName is the default name for the configuration file

View Source
const DefaultConfigType = "yaml"

DefaultConfigType is the default type for the configuration file

Variables

View Source
var DefaultProviderKey = ProviderKey{}

DefaultProviderKey is the default context key for the configuration provider

Functions

func GetAPIURL

func GetAPIURL() string

GetAPIURL returns the API URL from the configuration

func GetConfigDir

func GetConfigDir() (string, error)

GetConfigDir returns the directory where the configuration file is located

func GetDefaultNamespace

func GetDefaultNamespace() string

GetDefaultNamespace returns the default namespace from the configuration

func GetToken

func GetToken() string

GetToken returns the authentication token from the configuration

func InitConfig

func InitConfig(configPath string) error

InitConfig initializes the configuration with default values and paths

func SaveConfig

func SaveConfig() error

SaveConfig saves the current configuration to disk

func SetAPIURL

func SetAPIURL(url string)

SetAPIURL sets the API URL in the configuration

func SetDefaultManager

func SetDefaultManager(manager *Manager)

SetDefaultManager sets the default configuration manager

func SetDefaultNamespace

func SetDefaultNamespace(namespace string)

SetDefaultNamespace sets the default namespace in the configuration

func SetToken

func SetToken(token string)

SetToken sets the authentication token in the configuration

func WithContext

func WithContext(ctx context.Context) context.Context

WithContext returns a new context with the configuration provider

Types

type Manager

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

Manager handles configuration loading and access

func DefaultManager

func DefaultManager() *Manager

DefaultManager creates a new configuration manager with the default Viper provider

func GetDefaultManager

func GetDefaultManager() *Manager

GetDefaultManager returns the default configuration manager

func NewManager

func NewManager(provider Provider) *Manager

NewManager creates a new configuration manager with the given provider

func (*Manager) GetAPIURL

func (m *Manager) GetAPIURL() string

GetAPIURL returns the API URL from the configuration

func (*Manager) GetConfigDir

func (m *Manager) GetConfigDir() (string, error)

GetConfigDir returns the directory where the configuration file is located

func (*Manager) GetDefaultNamespace

func (m *Manager) GetDefaultNamespace() string

GetDefaultNamespace returns the default namespace from the configuration

func (*Manager) GetToken

func (m *Manager) GetToken() string

GetToken returns the authentication token from the configuration

func (*Manager) InitConfig

func (m *Manager) InitConfig(configPath string) error

InitConfig initializes the configuration with default values and paths

func (*Manager) Provider

func (m *Manager) Provider() Provider

Provider returns the configuration provider

func (*Manager) SaveConfig

func (m *Manager) SaveConfig() error

SaveConfig saves the current configuration to disk

func (*Manager) SetAPIURL

func (m *Manager) SetAPIURL(url string)

SetAPIURL sets the API URL in the configuration

func (*Manager) SetDefaultNamespace

func (m *Manager) SetDefaultNamespace(namespace string)

SetDefaultNamespace sets the default namespace in the configuration

func (*Manager) SetToken

func (m *Manager) SetToken(token string)

SetToken sets the authentication token in the configuration

func (*Manager) WithContext

func (m *Manager) WithContext(ctx context.Context) context.Context

WithContext returns a new context with the configuration provider

type Provider

type Provider interface {
	// Get retrieves a configuration value by key
	Get(key string) interface{}

	// GetString retrieves a string configuration value
	GetString(key string) string

	// GetInt retrieves an integer configuration value
	GetInt(key string) int

	// GetBool retrieves a boolean configuration value
	GetBool(key string) bool

	// GetFloat64 retrieves a float64 configuration value
	GetFloat64(key string) float64

	// GetTime retrieves a time.Time configuration value
	GetTime(key string) time.Time

	// GetDuration retrieves a time.Duration configuration value
	GetDuration(key string) time.Duration

	// GetStringSlice retrieves a string slice configuration value
	GetStringSlice(key string) []string

	// GetStringMap retrieves a map of string configuration values
	GetStringMap(key string) map[string]interface{}

	// GetStringMapString retrieves a map of string configuration values
	GetStringMapString(key string) map[string]string

	// IsSet checks if a configuration value is set
	IsSet(key string) bool

	// SetDefault sets a default value for a configuration key
	SetDefault(key string, value interface{})

	// Set sets a configuration value
	Set(key string, value interface{})

	// AllSettings returns all settings as a map
	AllSettings() map[string]interface{}

	// ConfigFileUsed returns the config file used for loading configuration
	ConfigFileUsed() string
}

Provider defines the interface for accessing configuration values

func FromContext

func FromContext(ctx context.Context) (Provider, bool)

FromContext retrieves the configuration provider from the context

func GetConfigProvider

func GetConfigProvider() Provider

GetConfigProvider returns the configuration provider

func MustFromContext

func MustFromContext(ctx context.Context) Provider

MustFromContext retrieves the configuration provider from the context or panics

type ProviderKey

type ProviderKey struct{}

ProviderKey is the context key for storing the configuration provider

type ViperProvider

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

ViperProvider is a Provider implementation that uses Viper

func NewViperProvider

func NewViperProvider() *ViperProvider

NewViperProvider creates a new ViperProvider

func (*ViperProvider) AddConfigPath

func (p *ViperProvider) AddConfigPath(path string)

AddConfigPath adds a path for Viper to search for the config file in

func (*ViperProvider) AllSettings

func (p *ViperProvider) AllSettings() map[string]interface{}

AllSettings returns all settings as a map

func (*ViperProvider) AutomaticEnv

func (p *ViperProvider) AutomaticEnv()

AutomaticEnv tells Viper to check for environment variables

func (*ViperProvider) BindPFlag

func (p *ViperProvider) BindPFlag(key string, flag *pflag.Flag) error

BindPFlag binds a flag to a configuration key

func (*ViperProvider) ConfigFileUsed

func (p *ViperProvider) ConfigFileUsed() string

ConfigFileUsed returns the config file used for loading configuration

func (*ViperProvider) Get

func (p *ViperProvider) Get(key string) interface{}

Get retrieves a configuration value by key

func (*ViperProvider) GetBool

func (p *ViperProvider) GetBool(key string) bool

GetBool retrieves a boolean configuration value

func (*ViperProvider) GetDuration

func (p *ViperProvider) GetDuration(key string) time.Duration

GetDuration retrieves a time.Duration configuration value

func (*ViperProvider) GetFloat64

func (p *ViperProvider) GetFloat64(key string) float64

GetFloat64 retrieves a float64 configuration value

func (*ViperProvider) GetInt

func (p *ViperProvider) GetInt(key string) int

GetInt retrieves an integer configuration value

func (*ViperProvider) GetString

func (p *ViperProvider) GetString(key string) string

GetString retrieves a string configuration value

func (*ViperProvider) GetStringMap

func (p *ViperProvider) GetStringMap(key string) map[string]interface{}

GetStringMap retrieves a map of string configuration values

func (*ViperProvider) GetStringMapString

func (p *ViperProvider) GetStringMapString(key string) map[string]string

GetStringMapString retrieves a map of string configuration values

func (*ViperProvider) GetStringSlice

func (p *ViperProvider) GetStringSlice(key string) []string

GetStringSlice retrieves a string slice configuration value

func (*ViperProvider) GetTime

func (p *ViperProvider) GetTime(key string) time.Time

GetTime retrieves a time.Time configuration value

func (*ViperProvider) IsSet

func (p *ViperProvider) IsSet(key string) bool

IsSet checks if a configuration value is set

func (*ViperProvider) ReadInConfig

func (p *ViperProvider) ReadInConfig() error

ReadInConfig reads in the config file

func (*ViperProvider) Set

func (p *ViperProvider) Set(key string, value interface{})

Set sets a configuration value

func (*ViperProvider) SetConfigFile

func (p *ViperProvider) SetConfigFile(file string)

SetConfigFile sets the config file explicitly

func (*ViperProvider) SetConfigName

func (p *ViperProvider) SetConfigName(name string)

SetConfigName sets the name of the config file

func (*ViperProvider) SetConfigType

func (p *ViperProvider) SetConfigType(configType string)

SetConfigType sets the type of the config file

func (*ViperProvider) SetDefault

func (p *ViperProvider) SetDefault(key string, value interface{})

SetDefault sets a default value for a configuration key

func (*ViperProvider) SetEnvPrefix

func (p *ViperProvider) SetEnvPrefix(prefix string)

SetEnvPrefix sets the prefix for environment variables

func (*ViperProvider) WriteConfig

func (p *ViperProvider) WriteConfig() error

WriteConfig writes the current configuration to the config file

Jump to

Keyboard shortcuts

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