preferences

package
v11.1.0 Latest Latest
Warning

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

Go to latest
Published: Aug 10, 2024 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

View Source
const (
	PrefServer      = "mqtt.server"
	PrefUser        = "mqtt.user"
	PrefPassword    = "mqtt.password"
	PrefTopicPrefix = "mqtt.topicprefix"
)

Variables

View Source
var (
	AppVersion = gitVersion
)
View Source
var ErrUnknownPref = errors.New("unknown preference")

Functions

func Save

func Save(prefs any) error

func SaveApp

func SaveApp(app string, prefs AppPreferences) error

SaveApp will save the given preferences for the given app to file. If the preferences cannot be saved, a non-nil error will be returned.

Types

type AgentPreferences

type AgentPreferences struct {
	MQTTServer      *Preference `toml:"mqtt.server"`
	MQTTUser        *Preference `toml:"mqtt.user,omitempty"`
	MQTTPassword    *Preference `toml:"mqtt.password,omitempty"`
	MQTTTopicPrefix *Preference `toml:"mqtt.topicprefix"`
}

func Load

func Load() (*AgentPreferences, error)

func (*AgentPreferences) GetDescription

func (p *AgentPreferences) GetDescription(key string) string

func (*AgentPreferences) GetValue

func (p *AgentPreferences) GetValue(key string) (value any, found bool)

func (*AgentPreferences) IsSecret

func (p *AgentPreferences) IsSecret(key string) bool

func (*AgentPreferences) Keys

func (p *AgentPreferences) Keys() []string

func (*AgentPreferences) Password

func (p *AgentPreferences) Password() string

func (*AgentPreferences) Server

func (p *AgentPreferences) Server() string

func (*AgentPreferences) SetValue

func (p *AgentPreferences) SetValue(key string, value any) error

func (*AgentPreferences) TopicPrefix

func (p *AgentPreferences) TopicPrefix() string

func (*AgentPreferences) User

func (p *AgentPreferences) User() string

type App

type App interface {
	Name() string
	DefaultPreferences() AppPreferences
}

type AppPreferences

type AppPreferences map[string]*Preference

AppPreferences is a structure that can be used to represent app preferences. As app preferences vary between apps, a map of Preference values is used.

func LoadApp

func LoadApp(app App) (AppPreferences, error)

LoadApp will load the given app preferences from file. If the preferences cannot be loaded, a non-nil error will be returned. Apps should take special care to handle os.ErrNotExists verses other returned errors. In the former case, it would be wise to treat as not an error and revert to using default preferences.

func (AppPreferences) GetDescription

func (p AppPreferences) GetDescription(key string) string

GetDescription returns the description of the preference with the given key name.

func (AppPreferences) GetValue

func (p AppPreferences) GetValue(key string) (value any, found bool)

GetValue returns the value of the preference with the given key name and a bool to indicate whether it was found or not. If the preference was not found, the value will be nil.

func (AppPreferences) IsSecret

func (p AppPreferences) IsSecret(key string) bool

IsSecret returns a boolean to indicate whether the preference with the given key name should be masked or obscured when displaying to the user.

func (AppPreferences) Keys

func (p AppPreferences) Keys() []string

Keys returns all key names for all known preferences.

func (AppPreferences) SetValue

func (p AppPreferences) SetValue(key string, value any) error

SetValue sets the preference with the given key name to the given value. It currently returns a nil error but may in the future return a non-nil error if the preference could not be set.

type Preference

type Preference struct {
	// Value is the actual preference value.
	Value any `toml:"value"`
	// Description is a string that describes the preference, and may be used
	// for display purposes.
	Description string `toml:"description,omitempty"`
	// Secret is a flag that indicates whether this preference represents a
	// secret. The value has no effect on the preference encoding in the TOML,
	// only on how to display the preference to the user (masked or plaintext).
	Secret bool `toml:"-"`
}

Preference represents a single preference in a preferences file.

Jump to

Keyboard shortcuts

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