config

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Aug 6, 2025 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package config provides configuration management for the Proxmox TUI application.

This package handles loading configuration from multiple sources with proper precedence ordering:

  1. Command-line flags (highest priority)
  2. Environment variables
  3. Configuration files (YAML format)
  4. Default values (lowest priority)

The package follows XDG Base Directory Specification for configuration and cache file locations, providing a clean and predictable user experience.

Configuration Sources:

Environment Variables:

  • PROXMOX_ADDR: Proxmox server URL
  • PROXMOX_USER: Username for authentication
  • PROXMOX_PASSWORD: Password for password-based auth
  • PROXMOX_TOKEN_ID: API token ID for token-based auth
  • PROXMOX_TOKEN_SECRET: API token secret
  • PROXMOX_REALM: Authentication realm (default: "pam")
  • PROXMOX_INSECURE: Skip TLS verification ("true"/"false")
  • PROXMOX_DEBUG: Enable debug logging ("true"/"false")
  • PROXMOX_CACHE_DIR: Custom cache directory path

Configuration File Format (YAML):

addr: "https://pve.example.com:8006"
user: "root"
password: "secret"
realm: "pam"
insecure: false
debug: true
cache_dir: "/custom/cache/path"

XDG Directory Support:

The package automatically determines appropriate directories for configuration and cache files based on XDG specifications:

  • Config: $XDG_CONFIG_HOME/proxmox-tui or ~/.config/proxmox-tui
  • Cache: $XDG_CACHE_HOME/proxmox-tui or ~/.cache/proxmox-tui

Authentication Methods:

The package supports both password and API token authentication:

  • Password: Requires user + password + realm
  • API Token: Requires user + token_id + token_secret + realm

Example usage:

// Load configuration with automatic source detection
config := NewConfig()
config.ParseFlags()

// Merge with config file if specified
if configPath != "" {
	err := config.MergeWithFile(configPath)
	if err != nil {
		log.Fatal("Failed to load config file:", err)
	}
}

// Set defaults and validate
config.SetDefaults()
if err := config.Validate(); err != nil {
	log.Fatal("Invalid configuration:", err)
}

Package config provides file operations for configuration management.

This file contains file-related functions that were previously part of the main config.go file.

Package config provides profile management functionality.

This file contains profile-related types and functions that were previously part of the main config.go file.

Index

Constants

This section is empty.

Variables

View Source
var DebugEnabled bool

DebugEnabled is a global flag to enable debug logging throughout the application.

This variable is set during configuration parsing and used by various components to determine whether to emit debug-level log messages.

Functions

func CreateDefaultConfigFile added in v0.9.0

func CreateDefaultConfigFile() (string, error)

CreateDefaultConfigFile creates a default configuration file and returns its path.

func FindDefaultConfigPath added in v0.8.1

func FindDefaultConfigPath() (string, bool)

FindDefaultConfigPath finds the default configuration file path.

func FindSOPSRule added in v1.0.0

func FindSOPSRule(startDir string) bool

FindSOPSRule checks if a SOPS rule file exists in the given directory or its parents.

func GetDefaultConfigPath added in v0.2.0

func GetDefaultConfigPath() string

GetDefaultConfigPath returns the default configuration file path.

func IsSOPSEncrypted added in v1.0.0

func IsSOPSEncrypted(path string, data []byte) bool

IsSOPSEncrypted checks if a file is SOPS-encrypted.

func ParseConfigFlags

func ParseConfigFlags()

func ValidateKeyBindings added in v0.8.0

func ValidateKeyBindings(kb KeyBindings) error

ValidateKeyBindings checks if all key specifications are valid.

Types

type Config

type Config struct {
	Profiles       map[string]ProfileConfig `yaml:"profiles"`
	DefaultProfile string                   `yaml:"default_profile"`
	// The following fields are global settings, not per-profile
	Debug       bool        `yaml:"debug"`
	CacheDir    string      `yaml:"cache_dir"`
	KeyBindings KeyBindings `yaml:"key_bindings"`
	Theme       ThemeConfig `yaml:"theme"`
	// Deprecated: legacy single-profile fields for migration
	Addr        string `yaml:"addr"`
	User        string `yaml:"user"`
	Password    string `yaml:"password"`
	TokenID     string `yaml:"token_id"`
	TokenSecret string `yaml:"token_secret"`
	Realm       string `yaml:"realm"`
	ApiPath     string `yaml:"api_path"`
	Insecure    bool   `yaml:"insecure"`
	SSHUser     string `yaml:"ssh_user"`
}

Config represents the complete application configuration, including multiple profiles.

func NewConfig

func NewConfig() *Config

NewConfig creates a new Config instance populated with values from environment variables.

This function reads all supported environment variables and creates a Config with those values. Environment variables that are not set will result in zero values for the corresponding fields.

Environment variables read:

  • PROXMOX_ADDR: Server URL
  • PROXMOX_USER: Username
  • PROXMOX_PASSWORD: Password for password auth
  • PROXMOX_TOKEN_ID: Token ID for token auth
  • PROXMOX_TOKEN_SECRET: Token secret for token auth
  • PROXMOX_REALM: Authentication realm (default: "pam")
  • PROXMOX_API_PATH: API base path (default: "/api2/json")
  • PROXMOX_INSECURE: Skip TLS verification ("true"/"false")
  • PROXMOX_SSH_USER: SSH username
  • PROXMOX_DEBUG: Enable debug logging ("true"/"false")
  • PROXMOX_CACHE_DIR: Custom cache directory

The returned Config should typically be further configured with command-line flags and/or configuration files before validation.

Returns a new Config instance with environment variable values.

Example usage:

config := NewConfig()
config.ParseFlags()
config.SetDefaults()
if err := config.Validate(); err != nil {
	log.Fatal("Invalid config:", err)
}

func (*Config) ApplyProfile added in v1.0.0

func (c *Config) ApplyProfile(profileName string) error

ApplyProfile applies the settings from a named profile to the main config.

func (*Config) GetAPIToken

func (c *Config) GetAPIToken() string

GetAPIToken returns the full API token string in the format required by Proxmox Format: PVEAPIToken=USER@REALM!TOKENID=SECRET.

func (*Config) GetActiveProfile added in v1.0.0

func (c *Config) GetActiveProfile() string

GetActiveProfile returns the name of the currently active profile.

func (*Config) GetAddr

func (c *Config) GetAddr() string

GetAddr returns the configured server address.

func (*Config) GetInsecure

func (c *Config) GetInsecure() bool

GetInsecure returns the configured insecure flag.

func (*Config) GetPassword

func (c *Config) GetPassword() string

GetPassword returns the configured password.

func (*Config) GetProfileNames added in v1.0.0

func (c *Config) GetProfileNames() []string

GetProfileNames returns a list of available profile names.

func (*Config) GetRealm

func (c *Config) GetRealm() string

GetRealm returns the configured realm.

func (*Config) GetTokenID

func (c *Config) GetTokenID() string

GetTokenID returns the configured token ID.

func (*Config) GetTokenSecret

func (c *Config) GetTokenSecret() string

GetTokenSecret returns the configured token secret.

func (*Config) GetUser

func (c *Config) GetUser() string

GetUser returns the configured username.

func (*Config) HasProfiles added in v1.0.0

func (c *Config) HasProfiles() bool

HasProfiles returns true if the configuration has any profiles defined.

func (*Config) IsUsingTokenAuth

func (c *Config) IsUsingTokenAuth() bool

IsUsingTokenAuth returns true if the configuration is set up for API token authentication.

func (*Config) MergeWithFile

func (c *Config) MergeWithFile(path string) error

func (*Config) MigrateLegacyToProfiles added in v1.0.0

func (c *Config) MigrateLegacyToProfiles() bool

MigrateLegacyToProfiles migrates legacy configuration fields to the new profile system.

func (*Config) SetDefaults

func (c *Config) SetDefaults()

SetDefaults sets default values for unspecified configuration options.

func (*Config) Validate

func (c *Config) Validate() error

type KeyBindings added in v0.8.0

type KeyBindings struct {
	SwitchView        string `yaml:"switch_view"` // Switch between pages
	SwitchViewReverse string `yaml:"switch_view_reverse"`
	NodesPage         string `yaml:"nodes_page"`   // Jump to Nodes page
	GuestsPage        string `yaml:"guests_page"`  // Jump to Guests page
	TasksPage         string `yaml:"tasks_page"`   // Jump to Tasks page
	Menu              string `yaml:"menu"`         // Open context menu
	GlobalMenu        string `yaml:"global_menu"`  // Open global context menu
	Shell             string `yaml:"shell"`        // Open shell session
	VNC               string `yaml:"vnc"`          // Open VNC console
	Refresh           string `yaml:"refresh"`      // Manual refresh
	AutoRefresh       string `yaml:"auto_refresh"` // Toggle auto-refresh
	Search            string `yaml:"search"`       // Activate search
	Help              string `yaml:"help"`         // Toggle help modal
	Quit              string `yaml:"quit"`         // Quit application
}

KeyBindings defines customizable key mappings for common actions. Each field represents a single keyboard key that triggers the action. Only single characters and function keys (e.g. "F1") are supported.

func DefaultKeyBindings added in v0.8.0

func DefaultKeyBindings() KeyBindings

DefaultKeyBindings returns a KeyBindings struct with the default key mappings.

type ProfileConfig added in v1.0.0

type ProfileConfig struct {
	Addr        string `yaml:"addr"`
	User        string `yaml:"user"`
	Password    string `yaml:"password"`
	TokenID     string `yaml:"token_id"`
	TokenSecret string `yaml:"token_secret"`
	Realm       string `yaml:"realm"`
	ApiPath     string `yaml:"api_path"`
	Insecure    bool   `yaml:"insecure"`
	SSHUser     string `yaml:"ssh_user"`
}

ProfileConfig holds a single connection profile's settings.

func (*ProfileConfig) Validate added in v1.0.0

func (p *ProfileConfig) Validate() error

Validate validates a single profile configuration.

type ThemeConfig added in v1.0.0

type ThemeConfig struct {
	// Name specifies the built-in theme to use as a base (e.g., "default", "catppuccin-mocha").
	// If empty, defaults to "default".
	Name string `yaml:"name"`
	// Colors specifies the color overrides for theme elements.
	// Users can use any tcell-supported color value (ANSI name, W3C name, or hex code).
	Colors map[string]string `yaml:"colors"`
}

ThemeConfig defines theme-related configuration options.

Jump to

Keyboard shortcuts

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